GTcoding
    • 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: code --- # Fenwick Tree (樹狀數組) [TOC] ## Usage - 前綴和 - 區間更新 & 查詢 - 可擴展到 2D BIT ## Definition Fenwick tree or binary indexed tree is a data structure that can efficiently update elements and calculate prefix sums in a table of numbers. (from Wikipedia) ## Introduction ### lowbit - 二進位最後一個 1 的位置所代表的數值。 `e.g.` $13=0b1101\ \implies\ lowbit=0b0001=1$ `e.g.` $12=0b1100\ \implies\ lowbit=0b0100=4$ - 必為 $2$ 的冪次。 - lowbit(x) = (x & -x) ### BIT ![](https://i.imgur.com/QL8EX68.png) - 節點存 $[i-lowbit(i)+1,i]$ 的和。 - 父節點為 $i-lowbit(i)$。 - 第一行為原數組,第二到第四行依次填入數字。 - 方便起見,此處對此數組的索引為從 $1$ 開始。 - 利用圖中已構造好的樹狀數組,則: ```= prefixSum(13) = prefixSum(0b00001101) = BIT[13] + BIT[12] + BIT[8] = BIT[0b00001101] + BIT[0b00001100] + BIT[0b00001000] ``` ### Sum ![](https://i.imgur.com/uaL96WK.png) 可發現並不是一棵二元樹, Binary 是指「二進位」。 ### Update ![](https://i.imgur.com/8wYLvTi.png) ### 時間複雜度 - 修改 $O(\log n)$ - 查詢 $O(\log n)$ ## 單點修改、區間查詢 BIT ### `建構` #### $O(n\log n)$ 建構 藉由以下的 `更新` 函式, 一個點一個點更新即可, $\implies$ 時間複雜度 $O(n\log n)$。 #### $O(n)$ 建構 可以知道, bit[i] 存的是 $[i-lowbit(i)+1,i]$ 的和, 可以先把陣列以 $O(n)$ 的方式作一次前綴和, 再一個 $O(n)$ 建構 $bit[i]=a[i]-a[i-lowbit(i)]$, $\implies$ 時間複雜度 $O(n)$。 ### `查詢(前綴和)` ```cpp= #define lowbit(x) (x & -x) #define ll long long ll sum(ll* bit, int x) { ll ret = 0; for (; x; x -= lowbit(x)) ret += bit[x]; return ret; } ``` ### `更新` ```cpp= #define lowbit(x) (x & -x) #define ll long long void upd(ll* bit, int x, int v) { // n 是 BIT 的 size for (; x <= n; x += lowbit(x)) bit[x] += v; } ``` ## 區間修改、單點查詢 BIT ### `原理` 通過差分把這個「區間修改、單點查詢」的問題轉化為「單點修改、區間查詢」 要記錄的數組是 $a[1:n]$, 假設: $\begin{cases}d[i]=a[i]−a[i−1]&,i=2\sim n \\d[i]=a[i]&,i=1\end{cases}$ 得到: $\implies a[i]=d[1]+d[2]+\dots+d[i]=\sum\limits_{k=1}^i d[k]$。 所以在 $BIT$ 中實際存儲的是數組 $d[1:n]$ ### `修改` 目標是給 $a[L:R]$ 全部加上 $x$, 那麼可以發現 $d[L+1]$, $d[L+2]$, $\dots$, $d[R]$ 都沒有變化。 而變化的只有 $d[L]$ 增加了 $x$、$d[R+1]$ 減少了 $x$。 所以只需要 $add(L,x)$, $add(R+1,-x)$ 即可。 ### `查詢` 要單點查詢 $a[pos]$, 由上可知 $a[pos]=d[1]+d[2]+\dots+d[pos]$, 那麼原來的 $sum(pos)$ 函數不用修改, 就正好能返回 $a[pos]$ 的值。 ## 區間修改,區間查詢 BIT ### `原理` 由於目標記錄的是數組 $a[1:n]$, 而實際存儲的是 $d[1:n]$, 那麼已經實現了區間修改。 前綴和: $a[1]+a[2]+⋯+a[i]\\=d[1]\times i+d[2]\times (i−1)+\dots+d[i]\times 1\\=([d[1]\times (i+1)+d[2]\times (i+1)+\dots+d[i]\times (i+1))−(d[1]\times 1+d[2]\times 2+\dots+d[i]\times i)\\=(i+1)\times (d[1]+d[2]+\dots+d[i])−(d[1]\times 1+d[2]\times 2+\dots+d[i]\times i)$ 在原來的數組 $C[i]$ 記錄 $d[i]$ 的基礎上,再搞一個數組 $C2[i]$ 記錄 $d[i]×i$ 即可。 :::spoiler `code` ```cpp= // author: giver #pragma GCC optimize("O2") #include <bits/stdc++.h> #define ll long long #define lowbit(x) (x & -x) using namespace std; ll bit1[1000005], bit2[1000005], a[1000005]; int T; ll sum(ll *bit, int x) { ll ret = 0; while (x) { ret += bit[x]; x -= lowbit(x); } return ret; } void upd(ll *bit, int x, ll v) { while (x <= T) { bit[x] += v; x += lowbit(x); } } ll qry(int x) { return (x + 1) * sum(bit1, x) - sum(bit2, x); } ll qry(int l, int r) { return qry(r) - qry(l - 1); } void upd(int l, int r, ll v) { upd(bit1, l, v); upd(bit2, l, l * v); upd(bit1, r + 1, -v); upd(bit2, r + 1, (r + 1) * -v); } int main() { cin.tie(0), ios::sync_with_stdio(0); int n, q; cin >> n >> q; T = n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) bit1[i] = a[i] - a[i - lowbit(i)]; for (int i = n; i; i--) a[i] -= a[i - 1]; for (int i = 1; i <= n; i++) a[i] = a[i - 1] + a[i] * i; for (int i = 1; i <= n; i++) bit2[i] = a[i] - a[i - lowbit(i)]; while (q--) { int op; cin >> op; if (op == 1) { int l, r; cin >> l >> r; cout << qry(l, r) << "\n"; } else { int l, r; ll k; cin >> l >> r >> k; upd(l, r, k); } } return 0; } ``` ::: ## Problems :::spoiler `ZJ d788. 排名順序` ```cpp= #include <streambuf> #include <iostream> #include <cstring> #define lowbit(x) (x & -x) using namespace std; namespace IO { bool rit(auto& re) { re = 0; char c = cin.rdbuf()->sbumpc(); while (!isdigit(c)) { if (c == EOF) return false; c = cin.rdbuf()->sbumpc(); } while (isdigit(c)) re = re * 10 + c - '0', c = cin.rdbuf()->sbumpc(); return true; } void wit(auto x) { char tmp[20], cur = 0; do tmp[cur++] = x % 10 + '0', x /= 10; while (x); while (cur) cout.rdbuf()->sputc(tmp[--cur]); cout.rdbuf()->sputc('\n'); } } using namespace IO; int bit[100001], n, r; int sum(int* bit, int x) { int ret = 0; while (x) ret += bit[x], x -= lowbit(x); return ret; } void upd(int* bit, int x, int v) { while (x <= n) bit[x] += v, x += lowbit(x); } int main() { ios::sync_with_stdio(false), cin.tie(nullptr); while (rit(n)) { memset(bit, 0, (n + 1) * 4); for (int i = 1; i <= n; i++) { rit(r); upd(bit, r, 1); wit(i - sum(bit, r - 1)); } } } ``` ::: :::spoiler `ZJ d794. 世界排名` ```cpp= #include <algorithm> #include <streambuf> #include <iostream> #include <cstring> #define lowbit(x) (x & -x) using namespace std; namespace IO { bool rit(auto& re) { re = 0; char c = cin.rdbuf()->sbumpc(); while (!isdigit(c)) { if (c == EOF) return false; c = cin.rdbuf()->sbumpc(); } while (isdigit(c)) re = re * 10 + c - '0', c = cin.rdbuf()->sbumpc(); return true; } void wit(auto x) { char tmp[20], cur = 0; do tmp[cur++] = x % 10 + '0', x /= 10; while (x); while (cur) cout.rdbuf()->sputc(tmp[--cur]); cout.rdbuf()->sputc('\n'); } } using namespace IO; int n; int sum(int* bit, int x) { int ret = 0; while (x) ret += bit[x], x -= lowbit(x); return ret; } void upd(int* bit, int x, int v) { while (x <= n) bit[x] += v, x += lowbit(x); } int main() { ios::sync_with_stdio(false), cin.tie(nullptr); while (rit(n)) { int bit[n + 1], a[n], tmp[n]; memset(bit, 0, (n + 1) * 4); for (int i = 0, x; i < n; i++) rit(x), a[i] = tmp[i] = x; sort(tmp, tmp + n); for (auto& x : a) x = lower_bound(tmp, tmp + n, x) - tmp + 1; for (int i = 1, r; i <= n; i++) { r = a[i - 1]; upd(bit, r, 1); wit(i - sum(bit, r - 1)); } } } ``` ::: 參考資料 --- - [1](https://blog.csdn.net/Yaokai_AssultMaster/article/details/79492190) - [2](https://www.cnblogs.com/dilthey/p/9366491.html?fbclid=IwAR3CCgUAKu9SMhqrYqdH578--hGq1_k0Q9m6I6AMKg4moCOptRD_kP8rSn0)

    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