BZOJ_1507_Editor_[NOI2003]_(Splay)
描述
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
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)的更多相关文章
- [BZOJ1507] [NOI2003] Editor (splay)
Description Input 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中可能会插入一些回车符,请忽略掉它 ...
- BZOJ 1507 NOI2003 Editor Splay
题目大意: 1.将光标移动到某一位置 2.在光标后插入一段字符串 3.删除光标后的一段字符 4.输出光标后的一段字符 5.光标-- 6.光标++ 和1269非常像的一道题,只是弱多了 几个问题须要注意 ...
- BZOJ4545: DQS的trie
BZOJ4545: DQS的trie https://lydsy.com/JudgeOnline/problem.php?id=4545 分析: 对trie用dfs建sam复杂度是\(O(n^2)\) ...
- bzoj 2555 SubString —— 后缀自动机+LCT
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2555 建立后缀自动机,就可以直接加入新串了: 出现次数就是 Right 集合的大小,需要查询 ...
- 【bzoj1507】[NOI2003]Editor /【bzoj1269】[AHOI2006]文本编辑器editor Splay
[bzoj1507][NOI2003]Editor 题目描述 输入 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中 ...
- BZOJ1507 [NOI2003]Editor 【splay】
1507: [NOI2003]Editor Time Limit: 5 Sec Memory Limit: 162 MB Submit: 4129 Solved: 1660 [Submit][St ...
- 【BZOJ】1507: [NOI2003]Editor(Splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1507 当练splay模板了,发现wjmzbmr的splay写得异常简介,学习了.orzzzzzzzz ...
- BZOJ_4516_[Sdoi2016]生成魔咒_后缀数组+ST表+splay
BZOJ_4516_[Sdoi2016]生成魔咒_后缀数组+ST表+splay Description 魔咒串由许多魔咒字符组成,魔咒字符可以用数字表示.例如可以将魔咒字符 1.2 拼凑起来形成一个魔 ...
- [NOI2003] 文本编辑器 (splay)
复制炸格式了,就不贴题面了 [NOI2003] 文本编辑器 Solution 对于光标的移动,我们只要记录一下现在在哪里就可以了 Insert操作:手动维护中序遍历结果,即每次取中点像线段树一样一样递 ...
随机推荐
- groupBy
public List groupBy(List list,String flag,String... sortName) throws Exception{ Map<String,List&l ...
- linux mysql 安装(rpm)
linux上安装mysql, 就需要两个文件, xx.client.xx.rpm和 xx.server.xx.rpm 如 MySQL-client-community-5.1.72-1.rhel5.i ...
- static的用途
1)限制变量的作用域:即在函数体,一个被声明为静态的变量在这一函数被调用过程中维持其值不变: 2)限制变量的存储域:<a>在模块内(但在函数体外),一个被声明为静态的变量,可以被模块内的所 ...
- (二)Android 基本控件
第一节:View 视图组件 Andorid 中的View 视图组件,实现类是android.view.View 类,是绝大多数图形显示类的父类,提供了大量的方法和属性.在View 类下,有很多子类,如 ...
- Sublime Text使用手记
1.Package Control 输入python 命令安装,打开控制台输入下方代码运行即可.控制台打开可使用快捷键Ctrl+~ 或菜单栏中View> Show Console,可访问Pack ...
- 图像储存容器Mat[OpenCV 笔记11]
IplImage 与 Mat IplImage是OpenCV1中的图像存储结构体,基于C接口创建.在退出之前必须release,否则就会造成内存泄露.在一些只能使用C语言的嵌入式系统中,不得不使用. ...
- Scala - 处理时间(nscala-time - Joda Time的scala封装)
GITHUB : https://github.com/nscala-time/nscala-time MAVEN : (注意选对scala版本) <dependency> <gro ...
- windows平台 culture name 详细列表
点击打开链接http://msdn.microsoft.com/zh-cn/goglobal/bb896001.aspx LCID Culture Identifier Culture Name Lo ...
- 常用的工具GCC GDB Make Makefile
系统调用系统调用是操作系统提供给外部应用程序的一组特殊的接口.应用程序通过这组特殊“接口”来获得操作系统内核提供的服务.在 C 语言中,操作系统的系统调用通常通过函数调用的形式完成, 这是因为这些函数 ...
- php中CURL技术模拟登陆抓取数据实战,抓取某校教务处学生成绩。
这两天有基友要php中curl抓取教务处成绩的源码,用于微信公众平台的开发.下面笔者只好忍痛割爱了.php中CURL技术模拟登陆抓取数据实战,抓取沈阳工学院教务处学生成绩. 首先,教务处登录需要验证码 ...