bzoj4144 splay模板题

来源:互联网 发布:六神磊磊读金庸 知乎 编辑:程序博客网 时间:2024/06/03 22:55
#include<iostream>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>using namespace std;typedef long long LL;inline int read(){int x=0;bool f=0;char c=getchar();for (;c<'0'||c>'9';c=getchar()) f=c=='-'?1:0;for (;c>='0'&&c<='9';c=getchar()) x=x*10+c-'0';return f?-x:x;}#define key e[e[rt].ch[1]].ch[0]const int N=200005,inf=2147483647;int n,m,ll,rt;struct na {int ch[2],f,rev,add,val,mn,sz;} e[N];char op[10];inline void add(int x,int a){if (!x) return;e[x].val+=a;e[x].mn+=a;e[x].add+=a;}inline void rever(int x){if (!x) return;swap(e[x].ch[0],e[x].ch[1]);e[x].rev^=1;}inline void pu(int x){if (!x) return;e[x].mn=min(e[x].val,min(e[e[x].ch[0]].mn,e[e[x].ch[1]].mn));e[x].sz=e[e[x].ch[0]].sz+e[e[x].ch[1]].sz+1;}inline void pd(int x){if (!x) return;if (e[x].rev) rever(e[x].ch[0]),rever(e[x].ch[1]),e[x].rev=0;if (e[x].add)add(e[x].ch[0],e[x].add),add(e[x].ch[1],e[x].add),e[x].add=0;}int build(int l,int r,int fa){if (l>r) return 0;int p=l+r>>1;e[p].f=fa; e[p].sz=1;if (l!=r) e[p].ch[0]=build(l,p-1,p);e[p].val=e[p].mn=(p==1||p==n+2)?inf:read();if (l!=r) e[p].ch[1]=build(p+1,r,p);pu(p);return p;}inline void rot(int x){int y=e[x].f,t=(e[y].ch[1]==x),p=(e[e[y].f].ch[1]==y);pd(y);pd(x);if (rt==y) rt=x;e[y].ch[t]=e[x].ch[t^1];e[e[x].ch[t^1]].f=y;e[x].f=e[y].f;if (e[y].f) e[e[y].f].ch[p]=x;e[x].ch[t^1]=y;e[y].f=x;pu(y);pu(x);}void update(int x) {if (e[x].f) update(e[x].f);pd(x);}inline void splay(int x,int g=0){update(x);while (e[x].f!=g){int f=e[x].f,ff=e[f].f;if (ff==g) rot(x);else if ((e[f].ch[1]==x)^(e[ff].ch[1]==f)) rot(x),rot(x);else rot(f),rot(x);}}void rotk(int k,int g=0){int x=rt;while (1){pd(x);//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!if (e[e[x].ch[0]].sz+1==k) break;else if (e[e[x].ch[0]].sz+1<k) k-=(e[e[x].ch[0]].sz+1),x=e[x].ch[1];else x=e[x].ch[0];}splay(x,g);}int main(){n=read();ll=n+2;e[0].mn=inf;rt=build(1,n+2,0);m=read();for (int i=1,x,y,a;i<=m;i++){if (i%31==0){int aa=1;aa++;}scanf("%s",op);if (op[0]=='A') {x=read(),y=read()+2,a=read();rotk(x);rotk(y,rt);add(key,a);}else if (op[0]=='R'&&op[3]=='E'){x=read(),y=read()+2;rotk(x);rotk(y,rt);rever(key);}else if (op[0]=='R'&&op[3]=='O'){x=read(),y=read()+2,a=read()%(y-x-1);if (!a) continue;rotk(y-a-1);rotk(y,rt);int rt1=key;key=0;pu(e[rt].ch[1]);pu(rt);rotk(x);rotk(x+1,rt);key=rt1;e[rt1].f=e[rt].ch[1];pu(e[rt].ch[1]);pu(rt);}else if (op[0]=='I'){x=read()+1,a=read();e[++ll].sz=1;e[ll].val=e[ll].mn=a;rotk(x);rotk(x+1,rt);e[ll].f=e[rt].ch[1];key=ll;pu(e[rt].ch[1]);pu(rt);}else if (op[0]=='D'){x=read()+1;rotk(x-1);rotk(x+1,rt);key=0;pu(e[rt].ch[1]);pu(rt);}else{x=read(),y=read()+2;rotk(x);rotk(y,rt);pd(rt);pd(e[rt].ch[1]);printf("%d\n",e[key].mn);}}return 0;}

0 0
原创粉丝点击