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
- BZOJ 1861
- [BZOJ ]
- BZOJ****-****
- BZOJ
- BZOJ
- BZOJ
- BZOJ
- BZOJ
- BZOJ
- BZOJ
- BZOJ
- BZOJ
- BZOJ
- bzoj
- bzoj
- BZOJ
- BZOJ
- bzoj
- C++实现查找数组中的值
- ora-12541:TNS:无监听程序”问题的解决
- 计算机视觉数据集
- OSX: Mac不睡眠的排查
- Unity手游之路<九>自动寻路Navmesh之高级主题
- BZOJ 1861
- 网络层IP地址划分
- USB详解
- Oracle PROFILE的管理
- 华为笔试1
- 华为笔试2
- (C语言)自己写字符串操作函数(一)——strlen/strcpy/strcat/strcmp/strchr/strpbrk/strstr等相关函数
- HDU 1559 最大子矩阵
- 华为笔试3