Eric
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    2
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 李超線段樹 ### 前言 先看[題目](https://cses.fi/problemset/task/2085),此題也就是[斜率優化](https://hackmd.io/GDLXmEqaT32xfzeHmzPxmw)的延伸,差別在於斜率的單調性有無,接下來要講解斜率無單調性的做法 : 李超線段樹。 ------------ ### 目標 給定一個些線($f(x)=ax+b$),求$f(x)$的最小值。 ### 概念 首先我們可以開一顆$x$值域的線段樹,每個區間存一半以上保證有最小值的線。 ### 更新 在加入一條線的時候,跟原本區間存的線比較,如果$f(mid)$比原本的小,就覆蓋,接著把比較大的線往下遞迴,有可能在後面會出現最小值。 要判斷往左還往右遞迴之前,必須要先做前置作業,就是先確定誰的斜率大誰的斜率小,這裡統一$seg[now]$裡放斜率大的。 **狀況一 :** $seg[now]的f(mid)<ins的f(mid)$ 代表mid發生在兩線交點的左邊,mid左邊的最小值一定是發生在$seg[now]$這條線,但是右邊不一定,所以往右邊遞迴確認。 ![](https://i.imgur.com/PQQz4vi.png) **狀況二 :** $ins的f(mid)<seg[now]的f(mid)$ 代表mid發生在兩線交點的右邊,mid右邊的最小值一定是發生在$ins$這條線,但是左邊不一定,所以往左邊遞迴確認。 ![](https://i.imgur.com/OQruZkw.png) 最後($l=r$時)需跟已儲存的線判斷誰比較小,因為會被遞迴的區間都是待確認,所以還要再判斷一次才可更新答案。 ```cpp= void update(line ins,int now=1,int l=1,int r=200000){ if(l==r){ if(ins.f(l)<seg[now].f(l)) seg[now]=ins; //有比較小才更新 return; } int mid=(l+r)/2,L=now*2,R=now*2+1; if(seg[now].m<ins.m) swap(ins,seg[now]); //確定斜率 if(seg[now].f(mid)<ins.f(mid)){ //Situation 1 seg[now]= update(ins,R,mid+1,r); }else{ //Situation 2 swap(ins,seg[now]); //把seg[now]設成成ins,並把原seg[now]拿去給子樹節點更新 update(ins,L,l,mid); } } ``` ### 查詢 把路徑上會遇到的線取$min\{f(x)\}$就好了。 如果$x$屬於的半邊在更新的時候是保證$seg[now]$的線為最小值,就不會繼續遞迴,有最小值的線會存在$seg[now]$,但如果最小值不確定時,就會繼續遞迴下去。但是在查詢的時候不會知道$x$屬於的半邊是有沒有確認過答案,所以不僅要繼續遞迴下去,還要跟$seg[now]$的$f(x)$取$min$。 ```cpp= int query(int x,int now=1,int l=1,int r=200000){ if(l==r) return seg[now].f(x); int mid=(l+r)/2,L=now*2,R=now*2+1,cur=seg[now].f(x); if(x<=mid){ return min(cur,query(x,L,l,mid)); }else{ return min(cur,query(x,R,mid+1,r)); } } ``` ::: spoiler Code ```cpp= #include <bits/stdc++.h> using namespace std; #define int long long #define MAXN 200005 #define inf LLONG_MAX int s[MAXN],f[MAXN],dp[MAXN]; struct line{ int m,b=inf; int y(int x){ return m*x+b; } }seg[MAXN*4]; void update(line ins,int now=1,int l=1,int r=200000){ if(l==r){ if(ins.y(l)<seg[now].y(l)) seg[now]=ins; return; } int mid=(l+r)/2,L=now*2,R=now*2+1; if(seg[now].m<ins.m) swap(ins,seg[now]); if(seg[now].y(mid)<ins.y(mid)){ update(ins,R,mid+1,r); }else{ swap(ins,seg[now]); update(ins,L,l,mid); } } int query(int x,int now=1,int l=1,int r=200000){ if(l==r) return seg[now].y(x); int mid=(l+r)/2,L=now*2,R=now*2+1,cur=seg[now].y(x); if(x<=mid){ return min(cur,query(x,L,l,mid)); }else{ return min(cur,query(x,R,mid+1,r)); } } signed main(){ int n,k; cin >> n >> k; for(int i=1;i<=n;i++) cin >> s[i]; for(int i=1;i<=n;i++) cin >> f[i]; update({k,0}); for(int i=1;i<=n;i++){ int x=s[i]; dp[i]=query(x); line now={f[i],dp[i]}; update(now); } cout << dp[n] << '\n'; } ``` :::

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully