Description
Input inputs a total of two lines, the first behavior of an integer n,n indicates the number of items, 1<=n<=100000. The second behavior n a positive integer separated by a space, representing the number of the first rank of n items. Output outputs a total of one row, n spaces separated by a positive integer p1,p2,p3 ... The PN,PI represents the position of the article I small before the first I operation. Note: If a small item of I has been placed in the correct position Pi before the first operation, we will reverse the interval [PI,PI] (single item). Sample Input6
3 4 5 1 6 2Sample Output4 6 4 5 6 6record the corresponding position of each unit and then splay the flip interval in turn.
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #define N (100000+ ) using namespace Std;struct node{int id,num;} b[n];int key[n],rev[n],size[n];int Father[n],son[n][2];int N,Root,a[N ],rank[n];bool CMP (node A,node b) {return a.num==b.num?a.id<b.id:a.num<b.num;} inline int Get (int x) {return son[father[x]][1]==x;} inline void Update (int x) {size[x]=size[son[x][0]]+size[son[x][1]]+1;} inline void pushdown (int x) {if (rev[x]) {rev[x]=0; Swap (son[x][0],son[x][1]); Rev[son[x][0]]^=1; Rev[son[x][1]]^=1; }}inline void Rotate (int x) {pushdown (father[x]); Pushdown (x); int Wh=get (x); int FA=FATHER[X],FAFA=FATHER[FA]; SON[FA][WH]=SON[X][WH^1]; Father[fa]=x; if (SON[FA][WH]) Father[son[fa][wh]]=fa; SON[X][WH^1]=FA; Father[x]=fafa; if (FAFA) son[fafa][son[fafa][1]==fa]=x; Update (FA); Update (x);} inline void splay (int x,int tar) {for (int fa; (fa=father[x])!=tar; Rotate (x)) if (Father[fa]!=tar) Rotate (Get (FA) ==get (x)? fa:x); if (!tar) root=x;} void Build (int l,int r,int FA) {if (l>r) return; if (l==r) {size[l]=1; FATHER[L]=FA; son[fa][l>fa]=l; Return } int mid= (L+R) >>1; Build (L,mid-1,mid); Build (Mid+1,r,mid); FATHER[MID]=FA; son[fa][mid>fa]=mid; Update (mid);} int findx (int x) {int now=root; while (1) {pushdown (now); if (size[son[now][0]]>=x) now=son[now][0]; else {x-=size[son[now][0]]; if (x==1) {splay (now,0); return now; } x--; NOW=SON[NOW][1]; }}}inline int Split (int x,int y) {int xx=findx (x), Yy=findx (y); Splay (xx,0); Splay (YY,XX); return son[yy][0];} int main () {scanf ("%d", &n), for (int i=1; i<=n; ++i) {scanf ("%d", &a[i+1]); b[i].id=i+1;b[i].num=a[i+1];} Sort (b+1,b+n+1,cmp); for (int i=1; i<=n; ++i) Rank[i]=b[i].id; Build (1,n+2,0); root= (n+3)/2;for (int i=1; i<=n; ++i) {splay (rank[i],0); int ans=size[son[root][0]]+1;printf ("%d", ans-1); int hh= Split (i,ans+1); Rev[hh]^=1;}}
1552 & 3506. [CQOI2014] Sorting arm "balance tree-splay"