wiwiho
    • 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 進階資料結構與莫隊算法—區間問題概述 ###### tags: `108 師大附中校隊培訓` ###### wiwiho --- ## 什麼是區間問題 ---- 區間指的是序列中一段連續的部分 ---- 有些題目會叫你修改某個區間 或問你某個區間的各種值 (例如最大、最小、數字和、公因數……) 也可能有修改、也有查詢 ---- 給一個長度為 $n$ 的數列 接下來有 $q$ 筆詢問 詢問有兩種類型: 1. 將 $[l, r]$ 這個區間的數字全部加上 $x$。 2. 輸出 $[l, r]$ 這個區間的最大值。 ---- 這是很經典的區間最大值問題 題目會要求你做兩種事 而且它是一下子要修改、一下子要查詢 ---- 暴力修改、暴力查詢? 修改和查詢時間都是 $O(n)$ 乘上詢問筆數就會變成 $O(nq)$ ---- 有點慢? 這種問題非常多元 有很多手法和資料結構 來處理這種問題 --- ## 區間問題的類型 ---- ## 操作(詢問)類型 ### 修改 可能是改數字、把數字加上某個值、把數字開根號……等等 可能一題裡面有多種修改 也可能會很複雜 ### 查詢 數字的和、最大的數字…… ---- ## 範圍類型 區間:可能不只一個數字 單點:只有一個數字 ---- - 區間修改、區間查詢 - 區間修改、單點查詢 - 單點修改、區間查詢 - 區間修改,沒有查詢 - 區間查詢,沒有修改 ---- 不同範圍類型的問題 可以套不同的作法 ---- 根據題目不同 可以選擇不一樣的作法 --- ## 前綴和 ---- 長度 $n$ 的數列 $A=\{A_1,A_2,...,A_n\}$ 令 $S_i=\sum_{k=1}^i A_k$ $S$ 就是 $A$ 的前綴和序列 每一個 $S_i$ 都是 $A$ 的一個前綴和 ---- 給一個長度為 $n$ 的數列 $A=\{A_1,A_2,...,A_n\}$ 有 $q$ 筆詢問 每筆詢問求區間 $[l,r]$ 所有數字的和 ---- 只有查詢區間和 不過如果暴力做 複雜度會變成 $O(nq)$ ---- 用前綴和去想 ---- $$S_r-S_{l-1}=\sum_{k=0}^r A_k-\sum_{k=0}^{l-1}A_k \\=\sum_{k=l}^r A_k$$ ---- $O(n)$ 把所有前綴和算出來 每次查詢都只要 $O(1)$ →複雜度 $O(n+q)$ --- ## 差分序列 給一個長度 $n$ 的數列 $A=\{A_1,A_2,...,A_n\}$ 有 $q_1$ 次修改 每次修改將 $[l,r]$ 範圍中所有數字都加上 $x$ 接著有 $q_2$ 次查詢 請輸出最後 $A_k$ 為多少 $1 \leq n,q_1,q_2 \leq 10^5$ ---- 做完全部修改,再做查詢 ---- 如果在區間修改的時候硬做 複雜度會變成 $O(nq_1)$ ---- 令 $S_i=A_i-A_{i-1}$、$A_0=0$ 也就是 $S_i$ 是 $A_i$ 與它前一項的差 $[l,r]$ 都加上 $x$ 那麼 $A_l - A_{l-1}$ 應該會多 $x$ 而 $A_{r+1}-A_r$ 會少 $x$ ---- 區間修改變成單點修改了 ---- 怎麼推回 $A$? $$\sum_{i=1}^k S_i=(A_1-A_0)+(A_2-A_1)+...+(A_k-A_{k-1}) \\=A_k$$ ---- 算出前綴和就可以得到 $A_k$ 了 這麼做一遍的話是 $O(n)$ 如果是全部修改完再做查詢 就只要 $O(n)$ 算一次 $A$ 再 $O(1)$ 查詢 ---- 一下修改、一下查詢? 每次修改完都要再重新花 $O(n)$ 算一次 $A$ 太久 --- ## 離散化 ---- 計算數字出現了幾次 $$1,1,2,3,5,3,3,4,4,2,2,2,1$$ $1$ 出現了 $3$ 次、$2$ 出現 $4$ 次…… ---- 如果開一個 map 來存每個數字出現了幾次? 不方便做區間問題 (例如大小介於 $[l,r]$ 的數字出現了幾次) ---- 用 vector、陣列? 可能出現很大數字,導致記憶體開不下 ---- 用離散化 把數列中第一小的數字換成 $1$ 第二小換成 $2$……

    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