标签:
Description
Input
Output
Sample Input
5 5 1 2 3 4 0 6 2 1 3 5 4 6 2 0 0
Sample Output
1 2
Hint
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int maxn=100010; int n,fa[maxn],ch[maxn][2],sz[maxn]; int flip[maxn],pos[maxn],rt; struct Node{ int x,id; }a[maxn]; void Flip(int x){ swap(ch[x][0],ch[x][1]); flip[x]^=1; } void Push_down(int x){ if(flip[x]){ Flip(ch[x][0]); Flip(ch[x][1]); flip[x]=0; } } int pd[maxn]; void P(int x){ int cnt=0; while(x){ pd[++cnt]=x; x=fa[x]; } while(cnt){ Push_down(pd[cnt--]); } } void Push_up(int x){ sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1; } void Rotate(int x){ int y=fa[x],g=fa[y],c=ch[y][1]==x; ch[y][c]=ch[x][c^1];fa[ch[x][c^1]]=y; ch[x][c^1]=y;fa[y]=x;fa[x]=g; if(g)ch[g][ch[g][1]==y]=x; Push_up(y); } void Splay(int x,int g=0){ P(x); for(int y;(y=fa[x])!=g;Rotate(x)) if(fa[y]!=g) Rotate((ch[fa[y]][1]==y)==(ch[y][1]==x)?y:x); Push_up(x); if(!g)rt=x; } int Build(int f,int l,int r){ if(l>r)return 0; int mid=(l+r)>>1;fa[mid]=f; ch[mid][0]=Build(mid,l,mid-1); ch[mid][1]=Build(mid,mid+1,r); sz[mid]=1; Push_up(mid); return mid; } bool cmp(Node a,Node b){ if(a.x!=b.x) return a.x<b.x; return a.id<b.id; } int main(){ while(~scanf("%d",&n)&&n){ memset(flip,0,sizeof(flip)); rt=Build(0,1,n+2); for(int i=1;i<=n;i++) scanf("%d",&a[i].x); for(int i=1;i<=n;i++) a[i].id=i; sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i++) pos[i+1]=a[i].id+1; pos[1]=1;pos[n+2]=n+2; for(int i=2,p;i<n+1;i++){ Splay(pos[1]); Splay(pos[i],pos[1]); printf("%d ",sz[ch[ch[rt][1]][0]]+1); Splay(pos[i]); p=ch[pos[i]][1]; while(ch[p][0]){ Push_down(p); p=ch[p][0]; } Push_down(p); Splay(pos[i-1]); Splay(p,pos[i-1]); Flip(ch[ch[rt][1]][0]); } printf("%d\n",n); } return 0; }
标签:
原文地址:http://www.cnblogs.com/TenderRun/p/5353059.html