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

1269: [AHOI2006]文本编辑器editor
Time Limit: 10 Sec  Memory Limit: 162 MB

Description
这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器。你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义:   文本:由0个或多个字符构成的序列。这些字符的ASCII码在闭区间[32, 126]内,也就是说,这些字符均为可见字符或空格。光标:在一段文本中用于指示位置的标记,可以位于文本的第一个字符之前,文本的最后一个字符之后或文本的某两个相邻字符之间。文本编辑器:为一个可以对一段文本和该文本中的一个光标进行如下七条操作的程序。如果这段文本为空,我们就说这个文本编辑器是空的。 编写一个程序: 建立一个空的文本编辑器。 从输入文件中读入一些操作指令并执行。 对所有执行过的GET操作,将指定的内容写入输出文件。

Input
输入文件中第一行是指令条数N,以下是需要执行的N个操作。除了回车符之外,输入文件的所有字符的ASCII码都在闭区间[32, 126]内。且行尾没有空格。

Output
依次对应输入文件中每条GET指令的输出,不得有任何多余的字符。

Sample Input
10
Insert 13
Balanced eert
Move 2
Delete 5
Next
Insert 7
editor
Move 0
Get
Move 11
Rotate 4
Get

Sample Output
B
t

HINT

对输入数据我们有如下假定: MOVE操作不超过50 000个,INSERT、DELETE和ROTATE操作作的总个数不超过6 000,GET操作不超过20 000个,PREV和NEXT操作的总个数不超过20 000。 所有INSERT插入的字符数之和不超过2M(1M=1 024*1 024)。 DELETE操作、ROTATE操作和GET操作执行时光标后必然有足够的字符。MOVE、PREV、NEXT操作不会把光标移动到非法位置。 输入文件没有错误。

Source
鸣谢seter重新制作数据

裸Splay。有几个问题导致调了很长时间……

1、update旋转标记时千万不要忘记清空自身标记,同时子树的标记应该取反,而非直接赋false

2、update最好过程内直接操作本身、左右子树,这样不容易写错。

3、pushup时一定要记得更新其父亲各节点!

program editor;//Splay Tree//By HT 2012/12//BZOJ P1269//AHOI2004 Editor{$DEFINE INDEBUG}Typenode=longint;rec=recordlch,rch,f,s:longint;w:char;ro:boolean;end;Varq:array[0..2000000] of rec;ss:array[0..10000] of char;h,null,now:node;top,i,j,k,n:longint;c:char;Procedure fopen;beginassign(input,'editor.in');assign(output,'editor.out');reset(input);rewrite(output);end;Procedure fclose;beginclose(input);close(output);end;Function mmin(a,b:longint):longint;beginif a<b then exit(a) else exit(b);end;Procedure new(var p:node);begininc(top);p:=top;end;Procedure pushup(P:node);beginwhile p<>null dobeginq[p].s:=q[q[p].lch].s+q[q[p].rch].s+1;p:=q[p].f;end;end;Procedure oldupdate(P:node);vary:node;beginif p=null then exit;if q[p].ro thenbeginy:=q[p].lch;q[p].lch:=q[p].rch;q[p].rch:=y;if q[p].lch<>null then q[q[p].lch].ro:=not q[q[p].lch].ro;if q[p].rch<>null then q[q[p].rch].ro:=not q[q[p].rch].ro;q[p].ro:=false;end;end;Procedure update(P:node);beginoldupdate(q[p].lch);oldupdate(q[p].rch);oldupdate(p);end;Procedure zig(P:node);// LeftRonatevarnewroot:node;beginif p=null then exit;update(p);if q[p].rch=null then exit;newroot:=q[p].rch;q[newroot].f:=q[p].f;if q[p].f<>null thenif q[q[p].f].lch=p thenq[q[p].f].lch:=newroot elseq[q[p].f].rch:=newrootelseh:=newroot;q[p].rch:=q[newroot].lch;if q[p].rch<>null thenq[q[p].rch].f:=p;q[newroot].lch:=p;q[p].f:=newroot;pushup(p);pushup(newroot);end;Procedure zag(P:node);//RightRonatevarnewroot:node;beginif p=null then exit;update(p);if q[p].lch=null then exit;newroot:=q[p].lch;q[newroot].f:=q[p].f;if q[p].f<>null thenif q[q[p].f].lch=p thenq[q[p].f].lch:=newroot elseq[q[p].f].rch:=newrootelseh:=newroot;q[p].lch:=q[newroot].rch;if q[p].lch<>null thenq[q[p].lch].f:=p;q[newroot].rch:=p;q[p].f:=newroot;pushup(p);pushup(newroot);end;Procedure Splay(P,tar:node);varx,y,z:node;beginif p=null then exit;x:=p;while q[x].f<>tar dobeginy:=q[x].f;z:=q[y].f;update(z);update(y);update(x);if z=tar thenbeginif q[y].lch=x then zag(y) else zig(y);exit;end;if (q[z].lch=y) and (q[y].lch=x) thenbeginzag(z);zag(y);end elseif (q[z].rch=y) and (q[y].rch=x) thenbeginzig(z);zig(y);end elseif (q[z].lch=y) and (q[y].rch=x) thenbeginzig(y);zag(z);end elsebeginzag(y);zig(z);end;end;end;Function kth(P:longint):node;beginkth:=h;update(kth);if p>q[h].s then p:=q[h].s;while q[kth].s>1 dobeginif q[q[kth].lch].s+1=p then exit;if p<q[q[kth].lch].s+1 then kth:=q[kth].lch elsebegindec(p,q[q[kth].lch].s+1);kth:=q[kth].rch;end;update(kth);end;end;Function buildtree(pl,pr:longint):node;varmid:longint;ans:node;beginif pr<pl then exit(null);if pl=pr thenbeginnew(ans);with q[ans] dobegins:=1;w:=ss[pl];lch:=null;rch:=null;f:=null;ro:=false;end;exit(ans);end;mid:=(pl+pr) div 2;new(ans);with q[ans] dobegins:=pr-pl+1;w:=ss[mid];ro:=false;lch:=buildtree(pl,mid-1);rch:=buildtree(mid+1,pr);if lch<>null thenq[lch].f:=ans;if rch<>null thenq[rch].f:=ans;exit(ans);end;end;Procedure Insert(k:longint);varp:node;beginp:=now;update(p);if q[p].rch=null then beginq[p].rch:=buildtree(1,k);q[q[p].rch].f:=p;pushup(p);exit;end;p:=q[p].rch;update(p);while q[p].lch<>null dobeginp:=q[p].lch;update(p);end;q[p].lch:=buildtree(1,k);q[q[p].lch].f:=p;pushup(p);end;Procedure move(P:longint);beginnow:=h;update(now);while q[now].s>1 dobeginif q[q[now].lch].s+1=p then exit;if p<q[q[now].lch].s+1 then now:=q[now].lchelsebegindec(p,q[q[now].lch].s+1);now:=q[now].rch;end;update(now);end;end;Procedure next;varo:node;beginupdate(now);if q[now].rch<>null thenbeginnow:=q[now].rch;update(now);while q[now].lch<>null dobeginnow:=q[now].lch;update(now);end;end elsebegino:=q[now].f;update(o);while o<>null dobeginif q[o].lch=now then break;o:=q[o].f;now:=q[now].f;update(o);end;now:=o;end;splay(now,null);end;Procedure prev;varo:node;beginupdate(now);if q[now].lch<>null thenbeginnow:=q[now].lch;update(now);while q[now].rch<>null dobeginnow:=q[now].rch;update(now);end;end elsebegino:=q[now].f;update(o);while o<>null dobeginif q[o].rch=now then break;o:=q[o].f;now:=q[now].f;update(o);end;now:=o;end;splay(now,null);end;Function Getpos:longint;varp,qq:node;beginp:=q[now].f;qq:=now;getpos:=q[q[now].lch].s+1;// if now=null then error(renone);update(p);update(qq);while p<>null dobeginif q[p].rch=qq then inc(getpos,q[q[p].lch].s+1);p:=q[p].f;qq:=q[qq].f;update(p);update(qq);end;end;Procedure get;varoldnow:node;beginoldnow:=now;if q[h].s<>getpos thenbeginnext;writeln(q[now].w);endelse writeln(' ');now:=oldnow;splay(now,null);end;Procedure delete(P:longint);vartmp:node;begintmp:=kth(mmin(q[h].s,getpos+p+1));splay(now,null);splay(tmp,h);update(h);update(q[h].rch);update(q[h].lch);q[q[h].rch].lch:=null;pushup(q[h].rch);end;Procedure ronate(P:longint);vartmp:node;begintmp:=kth(mmin(q[h].s,getpos+p+1));splay(now,null);splay(tmp,h);update(q[q[h].rch].lch);update(q[h].rch);update(h);q[q[q[h].rch].lch].ro:=true;//q[q[h].rch].lch:=null;end;Procedure init;varp:node;begintop:=0;new(null);with q[null] dobegins:=0;lch:=null;rch:=null;f:=null;w:=#0;end;new(h);now:=h;with q[h] dobeginlch:=null;rch:=null;f:=null;w:=#0;s:=1;end;for i:=1 to 10 doss[i]:=#0;insert(10);end;Procedure readchar(P:longint);vari:longint;o:char;beginfor i:=1 to p doread(o);end;begin{$IFDEF INDEBUG}fopen;{$ENDIF}readln(n);init;for i:=1 to n dobeginread(c);case c of'M':beginreadchar(3);readln(k);move(k+1);end;'I':beginreadchar(5);readln(k);for j:=1 to k doread(ss[j]);readln;insert(k);end;'D':beginreadchar(5);readln(k);delete(k);end;'R':beginreadchar(5);readln(k);ronate(k);end;'G':beginget;readchar(2);readln;end;'P':beginprev;readchar(3);readln;end;'N':beginnext;readchar(3);readln;end;end;// writeln('No.',i,' Succeed! ');//flush(output);//get;end;{$IFDEF INDEBUG}fclose;{$ENDIF}end.

转载于:https://www.cnblogs.com/htfy/archive/2012/12/25/2833107.html

[AHOI2006]Editor文本编辑器Splay Pascal相关推荐

  1. bzoj1269 文本编辑器 splay

    直接搞棵splay就行了,不要把光标弄到树中而是把光标当成询问或操作区间的端点标志这样会简单很多. 7点40分写到9点20分,包括调试总共花了一个小时40分钟,这次是自己独立调出来的,总算对splay ...

  2. [转]scite文本编辑器的说明

    scite,也就是SCIntilla based Text Editor, 基于SCIntilla编辑组件的文本编辑器.我们见到的许多文本编辑器都是基于SCIntilla编辑组件的. yidabu.c ...

  3. 关于scite文本编辑器的说明

    目录(?)[+] 关于scite文本编辑器的说明 text editor文本编辑器scite的配置文件类型 text editor文本编辑器scite用户配置例子 text editor文本编辑器sc ...

  4. Spring Boot使用Simditor富文本编辑器,并将图片上传到七牛云。

    开始之前,需要强调的是Simditor富文本编辑器是基于Jquery的.所以必须引入Jquery.Simditor下载地址 下载解压后会发现是整个simditor项目,很多东西项目根本不需要,只需要将 ...

  5. BZOJ 1269: [AHOI2006]文本编辑器editor Splay

    F.A.Qs Web Board Home ProblemSet Status Ranklist Contest ModifyUser   yejinru(0) Logout 捐赠本站 Notice: ...

  6. 1269: [AHOI2006]文本编辑器editor

    1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 4718  Solved: 1807 [Sub ...

  7. Bzoj1269 [AHOI2006]文本编辑器editor

    Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 3678  Solved: 1380 Description 这些日子,可可不和卡卡一起玩了,原来可可 ...

  8. 富文本编辑器中空格转化为a_如何对富文本编辑器(FCK Html Editor)的工具栏进行扩展?...

    我们在项目开发过程中,会经常使用到富文本编辑器.GeneXus内置的富文本编辑器FCK Html Editor使用起来非常方便,只要将页面变量的控件类型(Control Type)选择为FCK Htm ...

  9. layui获取select 文本_小程序富文本编辑器editor初体验

    终于,微信在5月9号的v2.7.0版本中新增了 editor富文本编辑器组件,今天有时间了准备体验一下 在5月6日的时候写了一篇小程序富文本解析的「伪需求」,从wxParse到towxml的坑,当时还 ...

  10. uni-app的editor的富文本编辑器

    在官方文档中教程真是也太简单了, 大家可以在官方代码中找到完整的内容 https://github.com/dcloudio/hello-uniapp/blob/master/pages/compon ...

最新文章

  1. 13装饰器和内置函数
  2. ABAP的Package interface, 安卓的manifest.xml和Kubernetes的Capabilities
  3. SQLite 函数大全
  4. 枚举--遍历搜索空间的例子:熄灯问题
  5. 检测数据类型的几种方式
  6. jvm性能调优实战 - 49OOM异常进行监控以及online处理
  7. zz eclipse.ini内存设置
  8. Eigen入门之密集矩阵 1 -- 类Matrix介绍
  9. The mook jong 计数DP
  10. pytorch 和 tensorflow2.0 方法替换
  11. PyCharm的structure自动定位到相应的函数位置
  12. c语言中栈堆,全程剖析C语言中堆和栈的区别
  13. Golang实践录:生成版本号和编译时间
  14. [论文阅读] IL2M: Class Incremental Learning With Dual Memory
  15. Lintcode 51.上一个排列[Medium]
  16. 湖南大学操作系统期末考试之英文PPT复习提纲
  17. C#照片合成PDF_ PDF合成或拆分PDF_PDF获取页数
  18. lingo数学软件完整教程
  19. ASAM XCP及驱动代码、ISO 11898+CANFD,ISO 14229,ISO 15031,ISO 15765相关标准文档
  20. Antd给表格一个斜线分隔(通过css改变)

热门文章

  1. Openfire服务端安装和配置
  2. 点对点协议(PPP)
  3. 学习逆向知识之用于游戏外挂的实现.第二讲,快速寻找植物大战僵尸阳光基址.以及动态基址跟静态基址的区别...
  4. VMware复制Centos6虚拟机要改的地方
  5. CSS文件未加载浏览器报警告:Resource interpreted as Stylesheet but transferred with MIME type text/html...
  6. vs2019 product key
  7. python获取小程序手机号并绑定
  8. CentOS下安装php gd库报错Error: php56w-common conflicts with php-common-5.3.3-48.el6_8.x86_64
  9. FieldGroup绑定ItemDataSource
  10. 微信公众平台开发(112) 微信卡券