日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Luogu】P3369 【模板】普通平衡树(树状数组)

發(fā)布時間:2025/3/19 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Luogu】P3369 【模板】普通平衡树(树状数组) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

P3369 【模板】普通平衡樹(樹狀數(shù)組)

一、樹狀數(shù)組

樹狀數(shù)組(Binary Indexed Tree(B.I.T), Fenwick Tree)是一個查詢和修改復雜度都為log(n)的數(shù)據(jù)結(jié)構(gòu)。

這張圖總是讓很多初學者望而生畏(好吧只是我)
所以在學習它之前,我們來看看線段樹。

(現(xiàn)在我默認大家都會線段樹)
我們知道如果\(a + b = c\),則\(b = c - a\)
所以,所有節(jié)點的右兒子都是不需要的。

我們把線段樹上不必要的節(jié)點去掉。
它長得會像這樣。

這種數(shù)據(jù)結(jié)構(gòu)我們稱它為樹狀數(shù)組。可以發(fā)現(xiàn)所有線段的右斷點都互不相同,所以我們把它按右端點重新編號。

可以發(fā)現(xiàn)一些性質(zhì):

  • 節(jié)點\(p\)的父親即為\(p + lowbit(p)\)。(\(lowbit(x) = x and -x\)
  • 節(jié)點\(p\)的線段長度為\(lowbit(p)\)
  • 故我們可以寫出給一個數(shù)加\(x\)的代碼。即順著邊依次更行它的祖先。

    void update(int x, int y) {for (int i = x; i <= n; i += lowbit(i)) c[i] += y; }

    我們還需要查詢\([l, r]\)的和,即為\([1, r]\)的和 \(-\) \([1, l - 1]\)的和。
    下面有一個求\([1, x]\)的和的代碼。

    void query(int x) {int ret = 0;for (int i = x; i; i -= lowbit(i)) ret += c[i];return ret; }

    我們再根據(jù)樹狀數(shù)組的圖可以發(fā)現(xiàn)其實就是對x進行二進制拆分。
    求出每一段的和。

    二、這道題的解釋

    我們可以考慮類似計數(shù)的方法。即如果\(x\)比較小,我們可以用\(num[x]\)表示\(x\)出現(xiàn)的次數(shù)。所以查找排名即為查詢比\(x\)小的數(shù)的\(num\)和。

    三、Kth()

    我們考慮在樹狀數(shù)組上進行類似倍增的操作。

    int _kth(int k) {int ret = 0, sum = 0;for (int i = 20; i >= 0; --i)if (ret + (1 << i) <= lcnt && sum + c[ret + (1 << i)] < k) {sum += c[ret + (1 << i)];ret += 1 << i;}for (int i = 0; i <= 20; ++i)if (sum + c[ret + (1 << i)] >= k) {ret += 1 << i;break;}return ret; }

    簡單地說,就是先跳大的,再跳最小一步使剛好大于等于k。

    四、代碼

    #include <stdio.h> #include <cstring> #include <cstdlib> #include <cmath> #include <iostream> #include <algorithm> using namespace std; const int MAXN = 100005; int n, opt[MAXN], num[MAXN], lcnt, lsh[MAXN]; class BinaryIndexTree {private:int c[MAXN];int _kth(int k) {int ret = 0, sum = 0;for (int i = 20; i >= 0; --i)if (ret + (1 << i) <= lcnt && sum + c[ret + (1 << i)] < k) {sum += c[ret + (1 << i)];ret += 1 << i;}for (int i = 0; i <= 20; ++i)if (sum + c[ret + (1 << i)] >= k) {ret += 1 << i;break;}return ret;}void _insert(int x) {for (int i = x; i <= n; i += i & -i) ++c[i];}void _erase(int x) {for (int i = x; i <= n; i += i & -i) --c[i];}int num(int x) {int ret = 0;for (int i = x; i; i -= i & -i) ret += c[i];return ret;}public:int kth(int k) { return _kth(k); }void insert(int x) { _insert(x); }void erase(int x) { _erase(x); }int rank(int x) { return num(x - 1) + 1; }int pre(int x) { return _kth(num(x - 1)); }int suc(int x) { return _kth(num(x) + 1); } } bitree; int main() {scanf("%d", &n);for (int i = 1; i <= n; ++i) {scanf("%d%d", &opt[i], &num[i]);if (opt[i] != 4) lsh[++lcnt] = num[i];}sort(lsh + 1, lsh + lcnt + 1);lcnt = unique(lsh + 1, lsh + lcnt + 1) - lsh - 1;for (int i = 1; i <= n; ++i) {if (opt[i] == 4) {printf("%d\n", lsh[bitree.kth(num[i])]);} else {int x = lower_bound(lsh + 1, lsh + lcnt + 1, num[i]) - lsh;if (opt[i] == 1) bitree.insert(x);else if (opt[i] == 2) bitree.erase(x);else if (opt[i] == 3) printf("%d\n", bitree.rank(x));else if (opt[i] == 5) printf("%d\n", lsh[bitree.pre(x)]);else if (opt[i] == 6) printf("%d\n", lsh[bitree.suc(x)]); }}return 0; }

    轉(zhuǎn)載于:https://www.cnblogs.com/herald/p/9879577.html

    總結(jié)

    以上是生活随笔為你收集整理的【Luogu】P3369 【模板】普通平衡树(树状数组)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。