版本比较
打包数据结构
正文
增加 221 行删除 96 行
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051062 行未改动1091101111121131141151161171181191201211221231241251261271281291301311321331341351367 行未改动1441451461471481491501511525 行未改动1581591601611621631641651661671681691701711721731741751761771781791801811821831841851864 行未改动19119219319419519619719819920020120220320420520620720820921021121221321421521624 行未改动241242243244245246247248249250251252253254255256树状数组```cpptemplate<class florr>struct bit{florr tree[N];inline void add(florr x,florr d){for (florr i=x;i<=n;i+=lowbit(i))tree[i]+=d;}inline florr query(florr x){florr s=0;for (florr i=x;i>=1;i-=lowbit(i))s+=tree[i];return s;}};```注:附带```cpp#define lowbit(x) (x&(-x))```---并查集```cpptemplate<class digdig>struct dsu{digdig fa[N];inline void init(){for (digdig i=1;i<=n;i++)fa[i]=i;}inline digdig findset(digdig x){while (x!=fa[x])x=fa[x]=fa[fa[x]];return fa[x];}inline void merge(digdig x,digdig y){digdig xx=findset(x),yy=findset(y);if (xx==yy)return ;fa[xx]=yy;}};```---线段树```cpptemplate<class zorr>struct segmentTree{zorr tree[N<<4],tag[N<<4];inline zorr ls(zorr x){return x<<1;}inline zorr rs(zorr x){return x<<1|1;}inline void push_up(zorr x){tree[x]=tree[ls(x)]+tree[rs(x)];}inline void addtag(zorr p,zorr pl,zorr pr,zorr d){tag[p]+=d;tree[p]+=d*(pr-pl+1);}inline void push_down(zorr p,zorr pl,zorr pr){if (tag[p]){zorr mid=(pl+pr)>>1;addtag(ls(p),pl,mid,tag[p]);addtag(rs(p),mid+1,pr,tag[p]);tag[p]=0;}}inline void build(zorr p,zorr pl,zorr pr){tag[p]=0;if (pl==pr){tree[p]=a[pl];return ;}zorr mid=(pl+pr)>>1;build(ls(p),pl,mid);build(rs(p),mid+1,pr);push_up(p);}inline void update(zorr p,zorr pl,zorr pr,zorr L,zorr R,zorr d){if (L<=pl && pr<=R){addtag(p,pl,pr,d);return ;}push_down(p,pl,pr);zorr mid=(pl+pr)>>1;if (L<=mid)update(ls(p),pl,mid,L,R,d);if (R>mid)update(rs(p),mid+1,pr,L,R,d);push_up(p);}inline zorr query(zorr p,zorr pl,zorr pr,zorr L,zorr R){if (L<=pl && pr<=R)return tree[p];push_down(p,pl,pr);zorr ans=0,mid=(pl+pr)>>1;if (L<=mid)ans+=query(ls(p),pl,mid,L,R);if (R>mid)ans+=query(rs(p),mid+1,pr,L,R);2 行未改动};```---平衡树(无旋treap)```cpptemplate<class hornex>struct FHQ{struct leaf{hornex l,r,x,w,s;}tree[N];hornex root=0,cnt=0;inline void push_up(hornex p){tree[p].s=tree[tree[p].l].s+tree[tree[p].r].s+1;}inline hornex init(hornex d){cnt++;tree[cnt].l=tree[cnt].r=0;tree[cnt].x=d;tree[cnt].w=rand_int(1,114514);tree[cnt].s=1;return cnt;}inline void split(hornex p,hornex x,hornex &L,hornex &R){if (p==0){L=R=0;return ;}7 行未改动split(tree[p].l,x,L,tree[p].l);push_up(R);}}inline hornex merge(hornex L,hornex R){if (L==0 || R==0)return L+R;if (tree[L].w<tree[R].w){tree[L].r=merge(tree[L].r,R);5 行未改动push_up(R);return R;}}inline void create(hornex p){hornex L,R;split(root,p,L,R);hornex now=init(p);root=merge(merge(L,now),R);}inline void clean(hornex p){hornex L,R,mid;split(root,p,L,R);split(L,p-1,L,mid);mid=merge(tree[mid].l,tree[mid].r);root=merge(merge(L,mid),R);}inline hornex getrank(hornex p){hornex L,R;split(root,p-1,L,R);hornex now=tree[L].s+1;root=merge(L,R);return now;}inline hornex getth(hornex p,hornex d){while (true){hornex now=tree[tree[p].l].s;if (now>=d)p=tree[p].l;4 行未改动p=tree[p].r;}}}inline hornex getma(hornex p){hornex L,R;split(root,p-1,L,R);hornex now=getth(L,tree[L].s);root=merge(L,R);return now;}inline hornex getmi(hornex p){hornex L,R;split(root,p,L,R);hornex now=getth(R,1);root=merge(L,R);return now;}};```---笛卡尔树```cpptemplate<class thoita>struct ct{24 行未改动dfs(root);}};```---随机promax```cppmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());inline int rand_int(int l,int r){uniform_int_distribution<int> abt(l, r);return abt(rng);}```1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556572 行未改动606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651667 行未改动1741751761771781791801811825 行未改动1881891901911921931941951961971981992002012022032042052062072082092102112122132142152164 行未改动22122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235324 行未改动378379380381线段树```cpptemplate<class idle>struct segmentTree{idle tree[N<<4];int tag[N<<4];inline int ls(int p){return p<<1;}inline int rs(int p){return p<<1|1;}inline void push_up(int p){tree[p]=tree[ls(p)]+tree[rs(p)];}inline void addtag(int p,int pl,int pr,idle d){tag[p]+=d;tree[p]+=d*(pr-pl+1);}inline void push_down(int p,int pl,int pr){if (tag[p]){int mid=(pl+pr)>>1;addtag(ls(p),pl,mid,tag[p]);addtag(rs(p),mid+1,pr,tag[p]);tag[p]=0;}}inline void build(int p,int pl,int pr){tag[p]=0;if (pl==pr){tree[p]=a[pl];return ;}int mid=(pl+pr)>>1;build(ls(p),pl,mid);build(rs(p),mid+1,pr);push_up(p);}inline void update(int p,int pl,int pr,int L,int R,idle d){if (L<=pl && pr<=R){addtag(p,pl,pr,d);return ;}push_down(p,pl,pr);int mid=(pl+pr)>>1;if (L<=mid)update(ls(p),pl,mid,L,R,d);if (R>mid)update(rs(p),mid+1,pr,L,R,d);push_up(p);}inline idle query(int p,int pl,int pr,int L,int R){if (L<=pl && pr<=R)return tree[p];push_down(p,pl,pr);idle ans=0;int mid=(pl+pr)>>1;if (L<=mid)ans+=query(ls(p),pl,mid,L,R);if (R>mid)ans+=query(rs(p),mid+1,pr,L,R);2 行未改动};```---树状数组```cpptemplate<class florr>struct bit{florr tree[N];inline void add(int x,florr d){for (int i=x;i<=n;i+=lowbit(i))tree[i]+=d;}inline florr query(int x){florr s=0;for (int i=x;i>=1;i-=lowbit(i))s+=tree[i];return s;}};```附录```cpp#define lowbit(x) (x&(-x))```---并查集```cpptemplate<class zorr>struct dsu{zorr fa[N];inline void init(){for (int i=1;i<=n;i++)fa[i]=i;}inline int findset(int x){while (x!=fa[x])x=fa[x]=fa[fa[x]];return fa[x];}inline void merge(int x,int y){int xx=findset(x),yy=findset(y);if (xx==yy)return ;fa[xx]=yy;}};```---字典树```cpptemplate<class box>struct trie{box nxt[N][26],w[N],cnt=0;inline void insert(string s){int now=0;for (int i=0;i<s.size();i++){int x=s[i]-'a';if (!nxt[now][x])nxt[now][x]=++cnt;now=nxt[now][x];}w[now]++;}inline box search(string s){int now=0;for (int i=0;i<s.size();i++){int x=s[i]-'a';if (!nxt[now][x])return 0;now=nxt[now][x];}return w[now]>0;}};```---treap```cpptemplate<class hornex>struct FHQ{struct leaf{hornex x,w,s;int l,r;}tree[N];int root=0,cnt=0;inline void push_up(int p){tree[p].s=tree[tree[p].l].s+tree[tree[p].r].s+1;}inline int init(hornex d){cnt++;tree[cnt].l=tree[cnt].r=0;tree[cnt].x=d;tree[cnt].w=rand_int(1,114514);tree[cnt].s=1;return cnt;}inline void split(int p,hornex x,int &L,int &R){if (p==0){L=R=0;return ;}7 行未改动split(tree[p].l,x,L,tree[p].l);push_up(R);}}inline hornex merge(int L,int R){if (L==0 || R==0)return L+R;if (tree[L].w<tree[R].w){tree[L].r=merge(tree[L].r,R);5 行未改动push_up(R);return R;}}inline void create(int p){int L,R;split(root,p,L,R);hornex now=init(p);root=merge(merge(L,now),R);}inline void clean(int p){int L,R,mid;split(root,p,L,R);split(L,p-1,L,mid);mid=merge(tree[mid].l,tree[mid].r);root=merge(merge(L,mid),R);}inline hornex getrank(int p){int L,R;split(root,p-1,L,R);hornex now=tree[L].s+1;root=merge(L,R);return now;}inline hornex getth(int p,hornex d){while (true){hornex now=tree[tree[p].l].s;if (now>=d)p=tree[p].l;4 行未改动p=tree[p].r;}}}inline hornex getma(int p){int L,R;split(root,p-1,L,R);hornex now=getth(L,tree[L].s);root=merge(L,R);return now;}inline hornex getmi(int p){int L,R;split(root,p,L,R);hornex now=getth(R,1);root=merge(L,R);return now;}};```---随机化```cppmt19937 rng(chrono::steady_clock::now().time_since_epoch().count());inline int rand_int(int l,int r){uniform_int_distribution<int> abt(l, r);return abt(rng);}```---pbds平衡树(splay)```cpptemplate<class super>using bst=tree<super,null_type,less<super>,rb_tree_tag,tree_order_statistics_node_update>;```附录```cpp#include<ext/pb_ds/assoc_container.hpp>#include<ext/pb_ds/tree_policy.hpp>#include<ext/pb_ds/priority_queue.hpp>#include<ext/pb_ds/hash_policy.hpp>using namespace __gnu_pbds;```---文艺平衡树(treap)```cpptemplate<class world>struct LBT{struct leaf{world l,r,x,w,s;bool b;}tree[N];int root=0,cnt=0;inline void addtag(int p){if (!p)return ;tree[p].b^=1;swap(tree[p].l,tree[p].r);}inline void push_down(int p){if (tree[p].b){addtag(tree[p].l);addtag(tree[p].r);tree[p].b=false;}}inline void push_up(int p){tree[p].s=1;if (tree[p].l)tree[p].s+=tree[tree[p].l].s;if (tree[p].r)tree[p].s+=tree[tree[p].r].s;}inline int init(int d){cnt++;tree[cnt].l=tree[cnt].r=0;tree[cnt].x=d;tree[cnt].w=rand_int(1,100000);tree[cnt].s=1;tree[cnt].b=false;return cnt;}inline void split(int p,int x,int &L,int &R){if (p==0){L=R=0;return ;}push_down(p);if (tree[tree[p].l].s<x){L=p;split(tree[p].r,x-tree[tree[p].l].s-1,tree[p].r,R);}else{R=p;split(tree[p].l,x,L,tree[p].l);}push_up(p);}inline int merge(int L,int R){if (L==0 || R==0)return L+R;if (tree[L].w<tree[R].w){push_down(L);tree[L].r=merge(tree[L].r,R);push_up(L);return L;}else{push_down(R);tree[R].l=merge(L,tree[R].l);push_up(R);return R;}}inline void query(int p){if (!p)return ;push_down(p);query(tree[p].l);cout << tree[p].x << ' ';query(tree[p].r);}};```---笛卡尔树```cpptemplate<class thoita>struct ct{24 行未改动dfs(root);}};```