BZOJ 1861

来源:互联网 发布:淘宝内衣店名字大全 编辑:程序博客网 时间:2024/03/29 16:40

这题主要是来贴图的。。。。。。

那我就直接上代码了。。。。。。


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N=80100;int ch[N][2],size[N],pre[N];int n,m,i,rt,x,y,fa;char s[20];inline void update(int x) {size[x]=size[ch[x][0]]+size[ch[x][1]]+1;}inline void rotate(int x,int kind){   int y=pre[x];   ch[y][!kind]=ch[x][kind];   pre[ch[x][kind]]=y;   if (pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x;   pre[x]=pre[y];   ch[x][kind]=y;   pre[y]=x;   update(y);   update(x);}inline void splay(int x,int goal){     while (pre[x]!=goal)     {           if (pre[pre[x]]==goal) rotate(x,ch[pre[x]][0]==x);           else           {               int y=pre[x];               int kind=ch[pre[y]][0]==y;               if (ch[y][kind]==x)               {                  rotate(x,!kind);                  rotate(x,kind);               }               else               {                   rotate(y,kind);                   rotate(x,kind);               }           }     }     if (!goal) rt=x;}inline void mktb(int x,int k){     splay(x,0);     if (!ch[x][k]) return;     int a[2];     a[0]=a[1]=ch[x][k];     while (ch[a[0]][0]) a[0]=ch[a[0]][0];     while (ch[a[1]][1]) a[1]=ch[a[1]][1];     rt=ch[x][k];     pre[rt]=0;     ch[a[k]][k]=x;     pre[ch[x][!k]]=a[!k];     ch[a[!k]][!k]=ch[x][!k];     pre[x]=a[k];     ch[x][0]=ch[x][1]=0;     size[x]=1;     while (pre[x])     {           size[pre[x]]++;           x=pre[x];     }     while (a[!k])     {           update(a[!k]);           a[!k]=pre[a[!k]];     }     size[rt]=n;}inline void tswap(int x,int dlt){     if (!dlt) return;     splay(x,0);     int k=(dlt==-1)?0:1;     int r=ch[x][k];     while (ch[r][!k]) r=ch[r][!k];     splay(r,x);     pre[x]=r;     pre[r]=0;     pre[ch[r][k]]=x;     pre[ch[x][!k]]=r;     ch[x][k]=ch[r][k];     ch[r][!k]=ch[x][!k];     ch[r][k]=x;     ch[x][!k]=0;     swap(size[x],size[r]);     rt=r;}inline int get(int x){    int r=rt;    while (1)    {          while (x>=size[ch[r][0]]+1)          {                x-=size[ch[r][0]]+1;                if (!x) return r;                r=ch[r][1];          }          r=ch[r][0];    }}int main(){    scanf("%d%d",&n,&m);    fa=0;    for (i=1;i<=n;i++)    {        scanf("%d",&x);        if (i==1) rt=x;        if (fa) ch[fa][1]=x;        pre[x]=fa;        size[x]=n+1-i;        fa=x;    }    for (i=1;i<=m;i++)    {        scanf("%s",s);        if (s[0]=='A') scanf("%d",&x),splay(x,0),printf("%d\n",size[ch[x][0]]);        else if (s[0]=='T') scanf("%d",&x),mktb(x,0);        else if (s[0]=='B') scanf("%d",&x),mktb(x,1);        else if (s[0]=='I') scanf("%d%d",&x,&y),tswap(x,y);        else scanf("%d",&x),printf("%d\n",get(x));    }    return 0;}





亮点在代码长度。。。。。。


前面几次是删注释。。。后面几次是把 maketop  makebottom  合并了。。。。。。


0 0
原创粉丝点击