一个简易的splay。。。。
Time Limit: 3000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
[Submit] [Go Back] [Status]
Description
Input
Output
Sample Input
5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
Sample Output
5 6 5 9
Hint
Huge input,the C function scanf() will work better than cin
Source
[Submit] [Go Back] [Status]
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; const int INF=0x3f3f3f3f; const int maxn=222222; int sz[maxn],ch[maxn][2],pre[maxn],m[maxn],v[maxn]; int id,root; int num[maxn],n,Q; void push_up(int x) { sz[x]=1+sz[ch[x][0]]+sz[ch[x][1]]; m[x]=max(v[x],max(m[ch[x][0]],m[ch[x][1]])); } void Rotate(int x,int d) { int y=pre[x]; ///push_down(y); ///push_down(x); pre[ch[x][d]]=y; ch[y][!d]=ch[x][d]; pre[x]=pre[y]; if(pre[x]) ch[pre[y]][ch[pre[y]][1]==y]=x; pre[y]=x; ch[x][d]=y; push_up(y); } void Splay(int x,int goal) { ///push_down(x); while(pre[x]!=goal) { if(pre[pre[x]]==goal) { Rotate(x,ch[pre[x]][0]==x); } else { int y=pre[x],d=ch[pre[y]][0]==y; if(ch[y][d]==x) Rotate(x,!d); else Rotate(y,d); Rotate(x,d); } ///push_down(x); } push_up(x); if(!goal) root=x; } void RotateTo(int k,int goal) { int x=root; ///push_down(x); while(sz[ch[x][0]]!=k) { if(k<sz[ch[x][0]]) { x=ch[x][0]; } else { k-=(sz[ch[x][0]]+1); x=ch[x][1]; } ///push_down(x); } Splay(x,goal); } void newNode(int& x,int c) { x=++id; sz[x]=1; m[x]=v[x]=c; ch[x][0]=ch[x][1]=pre[x]=0; } void makeTree(int& x,int l,int r,int f) { if(l>r) return ; int m=(l+r)>>1; newNode(x,num[m]); makeTree(ch[x][0],l,m-1,x); makeTree(ch[x][1],m+1,r,x); pre[x]=f; push_up(x); } void init() { ch[0][1]=ch[0][0]=pre[0]=0; sz[0]=v[0]=m[0]=0; id=root=0; newNode(root,-1); newNode(ch[root][1],-1); pre[id]=root; sz[root]=2; makeTree(ch[ch[root][1]][0],0,n-1,ch[root][1]); push_up(ch[root][1]); push_up(root); } int main() { while(scanf("%d%d",&n,&Q)!=EOF) { for(int i=0;i<n;i++) scanf("%d",num+i); init(); while(Q--) { char str[3]; int a,b; scanf("%s%d%d",str,&a,&b); if(str[0]=='Q') { RotateTo(a-1,0); RotateTo(b+1,root); printf("%d\n",m[ch[ch[root][1]][0]]); } else if(str[0]=='U') { RotateTo(a,0); v[root]=b; if(b>m[root]) m[root]=b; } } } return 0; }