主席樹 動態 Kth

2022-07-31 09:42:11 字數 1939 閱讀 8419

1

/*zoj2112

2動態 kth

3每一棵線段樹是維護每乙個序列字首的值在任意區間的個數,

4如果還是按照靜態的來做的話,那麼每一次修改都要遍歷o(n)棵樹,

5時間就是o(2*m*nlogn)->tle

6考慮到字首和,我們通過樹狀陣列來優化,即樹狀陣列套主席樹,

7每個節點都對應一棵主席樹,那麼修改操作就只要修改logn棵樹,

8o(nlognlogn+mlognlogn)時間是可以的,

9但是直接建樹要nlogn*logn(10^7)會mle

10我們發現對於靜態的建樹我們只要nlogn個節點就可以了,

11而且對於修改操作,只是修改m次,每次改變倆個值(減去原先的,加上現在的)

12也就是說如果把所有初值都插入到樹狀陣列裡是不值得的,

13所以我們分兩部分來做,所有初值按照靜態來建,記憶體o(nlogn),

14而修改部分儲存在樹狀陣列中,每次修改logn棵樹,每次插入增加logn個節點

15o(m*logn*logn+nlogn)

1617

18*/

19 #include20 #include21 #include22 #include23 #include24 #include25 #include26

#define ls(i) t[i].ls

27#define rs(i) t[i].rs

28#define w(i) t[i].w

29#define find(i) (lower_bound(lx.begin(),lx.begin()+n1,i)-lx.begin())+1

3031

using

namespace

std;

32const

int n=60000+10;33

struct

node

36 }t[2000000

];37

struct

opeop[11000

];40 vectorlx,q1,q2;

41int

n,n1,m,cnt;

42int a[61000],root[61000*2

];43 inline int lowbit(int

x)46

void build(int &i,int l,int r,int

x)54

void ins(int &i,int l,int r,int x,int

v)56 w(i)+=v;

57if (l==r) return;58

int m=(l+r)>>1;59

if (x<=m) ins(ls(i),l,m,x,v);

60else ins(rs(i),m+1

,r,x,v);61}

62void my_ins(int pos,int x,int

v)67}68

int qy(vector q1,vector q2,int l,int r,int

k)80

void query(int l,int r,int

k)90

void

work()

99for (int i=0;i)else

108}

109110

}111

intmain()

119char s[10

];120

for (int i=0;i)else

130}

131sort(lx.begin(),lx.end());

132 n1=unique(lx.begin(),lx.end())-lx.begin();

133work();

134}

135136

137return0;

138 }

主席樹動態

以zoj2114為模板題。主席樹的動態單點改值主要是主席樹加樹狀陣列,具體暫未理解,待熟練仔細揣摩。如下 include include include include include using namespace std const int maxn 60000 struct nood q ma...

動態主席樹

zoj 2112 include include include include define ll long long using namespace std const int maxn 10000 15 16 陣列註解 root 代表主席樹的每個數字對應點的節點編號 a 原陣列的數 hash ...

主席樹 動態 模板

自己整理模板,僅作模板儲存使用 include using namespace std const int maxn 6e4 5 主席樹最多需要在原空間上開大40倍,原空間本身首先要加大,這題原空間為50000 const int maxm 1e4 5 int t maxn s maxn l max...