Luogu5055 模板可持久化文艺平衡树(fhq-treap)
Posted gloid
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Luogu5055 模板可持久化文艺平衡树(fhq-treap)相关的知识,希望对你有一定的参考价值。
注意下传标记时也需要新建节点。空间开的尽量大。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define N 200010 #define inf 2147483647 #define pii pair<int,int> #define lson tree[k].ch[0] #define rson tree[k].ch[1] char getc(){char c=getchar();while ((c<‘A‘||c>‘Z‘)&&(c<‘a‘||c>‘z‘)&&(c<‘0‘||c>‘9‘)) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<‘0‘||c>‘9‘) {if (c==‘-‘) f=-1;c=getchar();} while (c>=‘0‘&&c<=‘9‘) x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int m,root[N],cnt; ll lastans; struct data{int ch[2],size,x,p,rev;ll sum; }tree[N*80]; void up(int k) { tree[k].size=tree[lson].size+tree[rson].size+1; tree[k].sum=tree[lson].sum+tree[rson].sum+tree[k].x; } void rev(int &k){if (k) tree[++cnt]=tree[k],k=cnt,swap(lson,rson),tree[k].rev^=1;} void down(int k){if (tree[k].rev) rev(lson),rev(rson),tree[k].rev=0;} pii split(int k,int x) { if (!k) return make_pair(0,0); int u=++cnt;tree[u]=tree[k]; down(u);int t=tree[u].ch[0]; if (tree[tree[u].ch[0]].size==x) return tree[u].ch[0]=0,up(u),make_pair(t,u); if (tree[tree[u].ch[0]].size>x) { pii v=split(tree[u].ch[0],x); return tree[u].ch[0]=v.second,up(u),make_pair(v.first,u); } else { pii v=split(tree[u].ch[1],x-tree[tree[u].ch[0]].size-1); return tree[u].ch[1]=v.first,up(u),make_pair(u,v.second); } } int merge(int x,int y) { if (!x||!y) return x|y; int k=++cnt; if (tree[x].p>tree[y].p) { tree[k]=tree[x];down(k); rson=merge(rson,y); } else { tree[k]=tree[y];down(k); lson=merge(x,lson); } up(k); return k; } int newpoint(int x) { int k=++cnt;tree[k].x=tree[k].sum=x,tree[k].size=1,tree[k].rev=lson=rson=0,tree[k].p=rand(); return k; } int main() { #ifndef ONLINE_JUDGE freopen("splay.in","r",stdin); freopen("splay.out","w",stdout); const char LL[]="%I64d "; #else const char LL[]="%lld "; #endif srand(20020509); m=read(); for (int i=1;i<=m;i++) { int last=read(),op=read(); root[i]=root[last]; switch (op) { case 1: { int p=read()^lastans,x=read()^lastans; pii u=split(root[i],p);int v=newpoint(x); root[i]=merge(merge(u.first,v),u.second); break; } case 2: { int p=read()^lastans; pii u=split(root[i],p-1),v=split(u.second,1); root[i]=merge(u.first,v.second); break; } case 3: { int l=read()^lastans,r=read()^lastans; pii u=split(root[i],l-1),v=split(u.second,r-l+1); rev(v.first); root[i]=merge(merge(u.first,v.first),v.second); break; } case 4: { int l=read()^lastans,r=read()^lastans; pii u=split(root[i],l-1),v=split(u.second,r-l+1); printf(LL,lastans=tree[v.first].sum); root[i]=merge(merge(u.first,v.first),v.second); break; } } //for (int j=1;j<=m;j++) cout<<root[j]<<‘ ‘;cout<<endl; //for (int j=1;j<=cnt;j++) cout<<tree[j].x<<‘ ‘<<tree[j].size<<‘ ‘<<tree[j].sum<<‘ ‘<<tree[j].ch[0]<<‘ ‘<<tree[j].ch[1]<<‘ ‘<<tree[j].rev<<endl; //cout<<endl; } return 0; }
以上是关于Luogu5055 模板可持久化文艺平衡树(fhq-treap)的主要内容,如果未能解决你的问题,请参考以下文章
[luogu3391] 模板文艺平衡树(fhq-treap反转区间)