描述


http://www.lydsy.com/JudgeOnline/problem.php?id=1507

简单区间操作的模板题

1507: [NOI2003]Editor

Time Limit: 5 Sec  Memory Limit: 162 MB
Submit: 3092  Solved: 1244
[Submit][Status][Discuss]

Description

Input


入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作。其中:
为了使输入文件便于阅读,Insert操作的字符串中可能会插入一些回车符,请忽略掉它们(如果难以理解这句话,可以参考样例)。
除了回车符之外,输入文件的所有字符的ASCII码都在闭区间[32, 126]内。且行尾没有空格。 这里我们有如下假定: 
MOVE操作不超过50000个,INSERT和DELETE操作的总个数不超过4000,PREV和NEXT操作的总个数不超过200000。 
所有INSERT插入的字符数之和不超过2M(1M=1024*1024),正确的输出文件长度不超过3M字节。 
DELETE操作和GET操作执行时光标后必然有足够的字符。MOVE、PREV、NEXT操作必然不会试图把光标移动到非法位置。 
输入文件没有错误。 对C++选手的提示:经测试,最大的测试数据使用fstream进行输入有可能会比使用stdio慢约1秒。

Output

输出文件editor.out的每行依次对应输入文件中每条GET指令的输出。

Sample Input

15
Insert 26
abcdefghijklmnop
qrstuv wxy
Move 15
Delete 11
Move 5
Insert 1
^
Next
Insert 1
_
Next
Next
Insert 4
.\/.
Get 4
Prev
Insert 1
^
Move 0
Get 22

Sample Output

.\/.
abcde^_^f.\/.ghijklmno

HINT

Source

分析


和BZOJ_1269很像,而且更简单.

首先加入一个起始字符和尾字符(因为我们解决问题的办法都是在两个区间之间进行的).

操作:

1.插入:把at转到根节点,把at+1转到根节点的右儿子,然后在at+1的左儿子处插入即可.

2.移动:直接输入到at即可.

3.删除:把at转到根节点,把at+n+1转到根节点的右儿子,然后把at+n+1的左儿子直接改成null即可.

4.&5.移动光标就at--,at++即可.

 #include <cstdio>
#include <algorithm>
using namespace std; const int maxn=(<<)+,oo=~0u>>; int n,x,at,cur;
char str[maxn],s[]; struct Splay{
struct node{
node* ch[],* pa;
char v; int s;
node(int v,node* t):v(v){ ch[]=ch[]=pa=t; s=; }
bool d(){ return pa->ch[]==this; }
void setc(node* t,bool d){ ch[d]=t; t->pa=this; }
void push_up(){ s=ch[]->s+ch[]->s+; }
}*root,*null;
Splay(){
null=new node('\0',NULL); null->s=;
root=new node('\0',null);
node* t=new node('\0',null);
root->setc(t,);
root->push_up();
}
void rot(node* o){
node* pa=o->pa; bool d=o->d();
pa->pa->setc(o,pa->d());
pa->setc(o->ch[!d],d);
o->setc(pa,!d);
pa->push_up();
if(pa==root) root=o;
}
void splay(node* o,node* pa){
while(o->pa!=pa){
if(o->pa->pa==pa) rot(o);
else o->d()==o->pa->d()?(rot(o->pa),rot(o)):(rot(o),rot(o));
}
o->push_up();
}
node* kth(int k){
k++;
for(node* t=root;;){
int s=t->ch[]->s+;
if(s==k) return t;
if(k>s) k-=s,t=t->ch[];
else t=t->ch[];
}
}
node* build(int l,int r){
if(l==r) return new node(str[l],null);
if(l>r) return null;
int m=l+(r-l)/;
node* t=new node(str[m],null);
t->setc(build(l,m-),);
t->setc(build(m+,r),);
t->push_up();
return t;
}
node* find(int l,int r){
node* L=kth(l); splay(L,null);
node* R=kth(r); splay(R,L);
return R;
}
void insert(int at,int cur){
node* t=find(at,at+);
t->setc(build(,cur),); t->push_up();
splay(t,null);
}
void remove(int at,int n){
node* t=find(at,at+n+);
t->setc(null,); t->push_up();
splay(t,null);
}
void print(node* o){
if(o==null) return;
print(o->ch[]);
printf("%c",o->v);
print(o->ch[]);
}
void print(int at,int n){
node* t=find(at,at+n+);
print(t->ch[]);
printf("\n");
}
}tree; int main(){
scanf("%d",&n);
while(n--){
scanf("%s",s);
if(s[]=='I'){
cur=;
scanf("%d",&x);
while(x--){
while(str[cur]=getchar(),str[cur]=='\n');
cur++;
}
cur--;
tree.insert(at,cur);
}
else if(s[]=='M') scanf("%d",&at);
else if(s[]=='D'){
scanf("%d",&x);
tree.remove(at,x);
}
else if(s[]=='G'){
scanf("%d",&x);
tree.print(at,x);
}
else if(s[]=='P') at--;
else at++;
}
return ;
}

BZOJ_1507_Editor_[NOI2003]_(Splay)的更多相关文章

  1. [BZOJ1507] [NOI2003] Editor (splay)

    Description Input 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中可能会插入一些回车符,请忽略掉它 ...

  2. BZOJ 1507 NOI2003 Editor Splay

    题目大意: 1.将光标移动到某一位置 2.在光标后插入一段字符串 3.删除光标后的一段字符 4.输出光标后的一段字符 5.光标-- 6.光标++ 和1269非常像的一道题,只是弱多了 几个问题须要注意 ...

  3. BZOJ4545: DQS的trie

    BZOJ4545: DQS的trie https://lydsy.com/JudgeOnline/problem.php?id=4545 分析: 对trie用dfs建sam复杂度是\(O(n^2)\) ...

  4. bzoj 2555 SubString —— 后缀自动机+LCT

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2555 建立后缀自动机,就可以直接加入新串了: 出现次数就是 Right 集合的大小,需要查询 ...

  5. 【bzoj1507】[NOI2003]Editor /【bzoj1269】[AHOI2006]文本编辑器editor Splay

    [bzoj1507][NOI2003]Editor 题目描述 输入 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中 ...

  6. BZOJ1507 [NOI2003]Editor 【splay】

    1507: [NOI2003]Editor Time Limit: 5 Sec  Memory Limit: 162 MB Submit: 4129  Solved: 1660 [Submit][St ...

  7. 【BZOJ】1507: [NOI2003]Editor(Splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1507 当练splay模板了,发现wjmzbmr的splay写得异常简介,学习了.orzzzzzzzz ...

  8. BZOJ_4516_[Sdoi2016]生成魔咒_后缀数组+ST表+splay

    BZOJ_4516_[Sdoi2016]生成魔咒_后缀数组+ST表+splay Description 魔咒串由许多魔咒字符组成,魔咒字符可以用数字表示.例如可以将魔咒字符 1.2 拼凑起来形成一个魔 ...

  9. [NOI2003] 文本编辑器 (splay)

    复制炸格式了,就不贴题面了 [NOI2003] 文本编辑器 Solution 对于光标的移动,我们只要记录一下现在在哪里就可以了 Insert操作:手动维护中序遍历结果,即每次取中点像线段树一样一样递 ...

随机推荐

  1. [转] c# 数据类型占用的字节数

    http://www.cnblogs.com/laozuan/archive/2012/04/24/2467888.html

  2. spring线程池配置

    源自:http://zjriso.iteye.com/blog/771706 1.了解 TaskExecutor接口 Spring的TaskExecutor接口等同于java.util.concurr ...

  3. MSSQL的sysprocesses

          包含正在 SQL Server 实例上运行的进程的相关信息. 这些进程可以是客户端进程或系统进程. 若要访问 sysprocesses,您必须位于 master 数据库上下文中, 或者必须 ...

  4. 利用ORACLE ADV 功能完成SQL TUNING 调优(顾问培训) “让DBA失业还是解脱?”

    oracle自动判断SQL性能功能. 11G的ADV,建议.SNAPSHOT,数据集合, 存储在oracle sys $_开头的表(10几条).  创建SNAPSHOT时选择天数, 默认14天. sq ...

  5. javaScripte 创建对象。。

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  6. C++里容易忽视却不能忽视的

    1 define 只是简单地文本替换. 2 每个机器的字长不同. 3 每个类型在不同的机器上,所占用的内存空间不同. 4 每个机器内部的字节大小端不同. 5 并不是所有的编译器或机器都支持最新的C++ ...

  7. 九度OJ 1205 N阶楼梯上楼问题 -- 动态规划(递推求解)

    题目地址:http://ac.jobdu.com/problem.php?pid=1205 题目描述: N阶楼梯上楼问题:一次可以走两阶或一阶,问有多少种上楼方式.(要求采用非递归) 输入: 输入包括 ...

  8. tr设置border无效的解决方法

    给table的css添加属性:border-collapse: collapse; 边框不折叠的表格 行,列,行组是不具有border的

  9. utube视频落地

    utube视频落地 简单粗暴的方法: 利用视频下载网站的网页版进行处理. 比如需要下载的视频的url是vid_url, 需要用到的web服务的url是web_service vid_url='http ...

  10. 图片Base64编码 简单使用

    图片在线转换Base64,图片编码base64 http://tool.css-js.com/base64.html HTML5 + js <input type="file" ...