[AHOI2006]Editor文本编辑器Splay Pascal
http://www.lydsy.com/JudgeOnline/problem.php?id=1269
1269: [AHOI2006]文本编辑器editor Description Input Output Sample Input Sample Output 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 |
裸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相关推荐
- bzoj1269 文本编辑器 splay
直接搞棵splay就行了,不要把光标弄到树中而是把光标当成询问或操作区间的端点标志这样会简单很多. 7点40分写到9点20分,包括调试总共花了一个小时40分钟,这次是自己独立调出来的,总算对splay ...
- [转]scite文本编辑器的说明
scite,也就是SCIntilla based Text Editor, 基于SCIntilla编辑组件的文本编辑器.我们见到的许多文本编辑器都是基于SCIntilla编辑组件的. yidabu.c ...
- 关于scite文本编辑器的说明
目录(?)[+] 关于scite文本编辑器的说明 text editor文本编辑器scite的配置文件类型 text editor文本编辑器scite用户配置例子 text editor文本编辑器sc ...
- Spring Boot使用Simditor富文本编辑器,并将图片上传到七牛云。
开始之前,需要强调的是Simditor富文本编辑器是基于Jquery的.所以必须引入Jquery.Simditor下载地址 下载解压后会发现是整个simditor项目,很多东西项目根本不需要,只需要将 ...
- BZOJ 1269: [AHOI2006]文本编辑器editor Splay
F.A.Qs Web Board Home ProblemSet Status Ranklist Contest ModifyUser yejinru(0) Logout 捐赠本站 Notice: ...
- 1269: [AHOI2006]文本编辑器editor
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MB Submit: 4718 Solved: 1807 [Sub ...
- Bzoj1269 [AHOI2006]文本编辑器editor
Time Limit: 10 Sec Memory Limit: 162 MB Submit: 3678 Solved: 1380 Description 这些日子,可可不和卡卡一起玩了,原来可可 ...
- 富文本编辑器中空格转化为a_如何对富文本编辑器(FCK Html Editor)的工具栏进行扩展?...
我们在项目开发过程中,会经常使用到富文本编辑器.GeneXus内置的富文本编辑器FCK Html Editor使用起来非常方便,只要将页面变量的控件类型(Control Type)选择为FCK Htm ...
- layui获取select 文本_小程序富文本编辑器editor初体验
终于,微信在5月9号的v2.7.0版本中新增了 editor富文本编辑器组件,今天有时间了准备体验一下 在5月6日的时候写了一篇小程序富文本解析的「伪需求」,从wxParse到towxml的坑,当时还 ...
- uni-app的editor的富文本编辑器
在官方文档中教程真是也太简单了, 大家可以在官方代码中找到完整的内容 https://github.com/dcloudio/hello-uniapp/blob/master/pages/compon ...
最新文章
- 13装饰器和内置函数
- ABAP的Package interface, 安卓的manifest.xml和Kubernetes的Capabilities
- SQLite 函数大全
- 枚举--遍历搜索空间的例子:熄灯问题
- 检测数据类型的几种方式
- jvm性能调优实战 - 49OOM异常进行监控以及online处理
- zz eclipse.ini内存设置
- Eigen入门之密集矩阵 1 -- 类Matrix介绍
- The mook jong 计数DP
- pytorch 和 tensorflow2.0 方法替换
- PyCharm的structure自动定位到相应的函数位置
- c语言中栈堆,全程剖析C语言中堆和栈的区别
- Golang实践录:生成版本号和编译时间
- [论文阅读] IL2M: Class Incremental Learning With Dual Memory
- Lintcode 51.上一个排列[Medium]
- 湖南大学操作系统期末考试之英文PPT复习提纲
- C#照片合成PDF_ PDF合成或拆分PDF_PDF获取页数
- lingo数学软件完整教程
- ASAM XCP及驱动代码、ISO 11898+CANFD,ISO 14229,ISO 15031,ISO 15765相关标准文档
- Antd给表格一个斜线分隔(通过css改变)
热门文章
- Openfire服务端安装和配置
- 点对点协议(PPP)
- 学习逆向知识之用于游戏外挂的实现.第二讲,快速寻找植物大战僵尸阳光基址.以及动态基址跟静态基址的区别...
- VMware复制Centos6虚拟机要改的地方
- CSS文件未加载浏览器报警告:Resource interpreted as Stylesheet but transferred with MIME type text/html...
- vs2019 product key
- python获取小程序手机号并绑定
- CentOS下安装php gd库报错Error: php56w-common conflicts with php-common-5.3.3-48.el6_8.x86_64
- FieldGroup绑定ItemDataSource
- 微信公众平台开发(112) 微信卡券