题目

多了区间翻转,之后没了

区间翻转的标记记得在\(kth\)的时候下传

代码

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define maxn 2100005
#define re register
int n,m,root,len,pos=1;
char val[maxn],S[maxn],opt[15];
int fa[maxn],sz[maxn],ch[maxn][2],rev[maxn];
inline void pushup(int x) {sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;}
inline void pushdown(int x) {
if(!rev[x]) return;
std::swap(ch[ch[x][0]][0],ch[ch[x][0]][1]);
std::swap(ch[ch[x][1]][0],ch[ch[x][1]][1]);
rev[x]=0,rev[ch[x][0]]^=1,rev[ch[x][1]]^=1;
}
inline void rotate(int x) {
int y=fa[x],z=fa[y],k=ch[y][1]==x,w=ch[x][k^1];
ch[z][ch[z][1]==y]=x;ch[x][k^1]=y;ch[y][k]=w;
pushup(y),pushup(x),fa[w]=y,fa[y]=x,fa[x]=z;
}
inline void splay(int x,int goal) {
while(fa[x]!=goal) {
int y=fa[x];
if(fa[y]!=goal) rotate((ch[y][1]==x)^(ch[fa[y]][1]==y)?x:y);
rotate(x);
}
if(!goal) root=x;
}
inline int kth(int now) {
int x=root;
while(1) {
pushdown(x);
if(sz[ch[x][0]]+1==now) return x;
if(sz[ch[x][0]]>=now) x=ch[x][0];
else now-=sz[ch[x][0]]+1,x=ch[x][1];
}
}
int build(int x,int y,int f) {
if(x>y) return 0;
if(x==y) {
fa[++m]=f,sz[m]=1,val[m]=S[x];
return m;
}
int mid=x+y>>1,rt=++m;
fa[rt]=f,val[rt]=S[mid];
ch[rt][0]=build(x,mid-1,rt),ch[rt][1]=build(mid+1,y,rt);pushup(rt);
return rt;
}
int main()
{
scanf("%d",&n);
root=1,sz[1]=2,ch[1][1]=2,sz[2]=1,fa[2]=1;m=2;
while(n--) {
scanf("%s",opt);
if(opt[0]=='M') scanf("%d",&pos),pos++;
if(opt[0]=='P') pos--;
if(opt[0]=='N') pos++;
if(opt[0]=='I') {
scanf("%d",&len);S[0]=getchar();
for(re int i=1;i<=len;i++) S[i]=getchar();
int aa=kth(pos),bb=kth(pos+1);
splay(aa,0),splay(bb,aa);
int t=ch[root][1],rt=build(1,len,t);
ch[t][0]=rt;pushup(t);splay(t,0);
}
if(opt[0]=='D') {
scanf("%d",&len);
int aa=kth(pos),bb=kth(pos+len+1);
splay(aa,0),splay(bb,aa);
ch[ch[root][1]][0]=0;pushup(ch[root][1]);splay(ch[root][1],0);
}
if(opt[0]=='G') {
int aa=kth(pos),bb=kth(pos+2);
splay(aa,0),splay(bb,aa);
putchar(val[ch[ch[root][1]][0]]);
if(val[ch[ch[root][1]][0]]!=10) putchar(10);
}
if(opt[0]=='R') {
scanf("%d",&len);
int aa=kth(pos),bb=kth(pos+len+1);
splay(aa,0),splay(bb,aa);
int t=ch[ch[root][1]][0];
std::swap(ch[t][1],ch[t][0]),rev[t]^=1;
}
}
return 0;
}

最新文章

  1. VB.NET 创建文件以及文件的读写(创建随机数)
  2. Kooboo CMS 无聊随笔 (1)
  3. iis 部署 webapi2.0 访问报错解决
  4. MYSQL INSERT INTO SELECT 不插入重复数据
  5. Entity Framework 学习初级篇--基本操作:增加、更新、删除、事务(转)
  6. [方法] ubuntu12.04开启root账户
  7. 封装自己的Ajax框架
  8. HTML meta refresh 刷新与跳转(重定向)页面
  9. Java String使用总结
  10. git 解决每次更新代码都要输入用户名密码的解决方案
  11. nginx+vsftp图片下载java代码上传
  12. winform界面特效470多例
  13. python -----一个简单的小程序(监控电脑内存,cpu,硬盘)
  14. 关于Python打包运行的一些思路
  15. 基于 Python 和 Pandas 的数据分析(1)
  16. ATOM常用插件推荐
  17. Loj 10211 sumdiv
  18. 每日英语:How to Save Detroit
  19. FindBugs:Java 静态代码检查
  20. Mac下的裁剪快捷键

热门文章

  1. linux对于zombie的处理
  2. express遇到的问题
  3. Android学习06Android应用程序的基本组件
  4. C#的params参数遇到null
  5. sublime的reopen with encoding和reload with encoding区别
  6. 4.爬虫 requests库讲解 GET请求 POST请求 响应
  7. 工作采坑札记:4. Hadoop获取InputSplit文件信息
  8. MsysGit下GUI乱码问题解决
  9. 深入理解JavaScript系列(29):设计模式之装饰者模式
  10. continue break return