【[AHOI2006]文本编辑器】
多了区间翻转,之后没了
区间翻转的标记记得在\(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;
}
【[AHOI2006]文本编辑器】的更多相关文章
- AHOI2006文本编辑器editor
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1885 Solved: 683[Submit ...
- BZOJ 1269: [AHOI2006]文本编辑器editor( splay )
splay..( BZOJ 1507 题目基本相同..双倍经验 ) ------------------------------------------------------------------ ...
- BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor
BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor 题意: 分析: splay模拟即可 注意1507的读入格式 ...
- BZOJ 1269: [AHOI2006]文本编辑器editor (splay tree)
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1213 Solved: 454[Submit ...
- 【BZOJ1269/1507】[AHOI2006]文本编辑器editor Splay
[BZOJ1269][AHOI2006]文本编辑器editor Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目 ...
- 【bzoj1507】[NOI2003]Editor /【bzoj1269】[AHOI2006]文本编辑器editor Splay
[bzoj1507][NOI2003]Editor 题目描述 输入 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中 ...
- BZOJ1269 [AHOI2006]文本编辑器editor 【82行splay】
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MB Submit: 4633 Solved: 1782 [Sub ...
- [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)
Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义: 文本:由0个或 ...
- Bzoj1269 [AHOI2006]文本编辑器editor
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3678 Solved: 1380 Description 这些日子,可可不和卡卡一起玩了,原来可可正 ...
- 【BZOJ】1269: [AHOI2006]文本编辑器editor(Splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1269 这题RE2次啊,好不爽啊,我一直以为是splay的问题,其实是数组开小了......(我老犯这 ...
随机推荐
- Yii1 的数据库操作整理
1.根据条件查询一个集合 $objectResult=Post::model()->findAll($condition,$params);$objectResult=Post::model() ...
- html自定义垂直导航菜单(加强版--自定义传入menu参数,支持JSONArray、JSArray、JSONObject、JSObject)
在上一篇中我简单写了个html自定义垂直导航菜单,缺点很明显,里面的数据是固定死的,不能动态更改数据. 这里我重写了一个修改版的垂直二级导航菜单,将原先的menuBox.init(config);修改 ...
- easyUI----grid
1.设置标题行高 .datagrid-header-row td{background-color:rgb(15,185,234);color:#fff;height:35px ;font-size: ...
- PHP冒泡排序的实现方法
<?php function BubbleSort($arr){ $count = count($arr); if($count<=1){ return $arr; } for($i=0; ...
- CSS像素、设备独立像素、设备像素之间关系
CSS像素.设备独立像素.设备像素,三者联系紧密又有很大的区别,而我们主要是在做移动端开发的时候需要更多地用到这些概念,那他们分别是指什么呢? 概念 CSS像素(CSS Pixel):适用于web编程 ...
- 给string添加新的函数
var str = "abcdefg";String.prototype.constr = function(){ return this.split('').join('-'); ...
- Java根据byte数组,生成文件
原文出自:https://blog.csdn.net/seesun2012 根据byte数组,生成文件 自己写的小案例,找个地方记录一下 package com.seesun2012.utils; i ...
- jquery的$.getScript在IE下的缓存问题
jquery的$.getScript在IE下的缓存问题
- NodeJs异步上传multer插件报Multipart: Boundary not found错误解决方法
NodeJs-express架构下实现文件上传两大利器: 1.前端异步插件h5uploader https://github.com/wewoor/h5uploader 2.后端处理multer ht ...
- Nginx+PM2+Node.js最简单的配置
一个最简单的反向代理配置方式 server { listen ; server_name www.luckybing.top; location / { proxy_pass http://127.0 ...