1507: [NOI2003]Editor
Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 1908 Solved: 738
[Submit][Status]
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 16
Delete 11
Move 5
Insert 1
^
Next
Insert 1
_
Next
Next
Insert 4
.\/.
Get 4
Prev
Insert 1
^
Move 0
Get 22
Insert 26
abcdefghijklmnop
qrstuv wxy
Move 16
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
abcde^_^f.\/.ghijklmno
HINT
Source
题解:
我靠。。。
刚开始WA,后来栈溢出,然后TLE,还让不让人活了。。。
代码:
1.递归,中序遍历输出结果
1 {$inline on} 2 {$M 10000000,0,maxlongint} 3 const maxn=2000000+1000; 4 var s,fa:array[0..maxn] of longint; 5 v:array[0..maxn] of char; 6 c:array[0..maxn,0..1] of longint; 7 i,j,n,m,x,y,z,tot,rt,now,l,r:longint; 8 ch:char; 9 st,st2:ansistring; 10 procedure pushup(x:longint);inline; 11 begin 12 s[x]:=s[c[x,0]]+s[c[x,1]]+1; 13 end; 14 procedure rotate(x:longint;var k:longint);inline; 15 var y,z,l,r:longint; 16 begin 17 y:=fa[x];z:=fa[y]; 18 l:=ord(c[y,1]=x);r:=l xor 1; 19 if y=k then k:=x else c[z,ord(c[z,1]=y)]:=x; 20 fa[x]:=z;fa[y]:=x;fa[c[x,r]]:=y; 21 c[y,l]:=c[x,r];c[x,r]:=y; 22 pushup(y);pushup(x); 23 end; 24 procedure splay(x:longint;var k:longint);inline; 25 var y,z:longint; 26 begin 27 while x<>k do 28 begin 29 y:=fa[x];z:=fa[y]; 30 if y<>k then 31 if (c[z,0]=y) xor (c[y,0]=x) then rotate(x,k) 32 else rotate(y,k); 33 rotate(x,k); 34 end; 35 end; 36 function find(x,rank:longint):longint;inline; 37 var l,r:longint; 38 begin 39 l:=c[x,0];r:=c[x,1]; 40 if s[l]+1=rank then exit(x) 41 else if s[l]>=rank then exit(find(l,rank)) 42 else exit(find(r,rank-s[l]-1)); 43 end; 44 procedure print(x:longint);inline; 45 begin 46 if x=0 then exit; 47 print(c[x,0]); 48 write(v[x]); 49 print(c[x,1]); 50 end; 51 52 procedure main; 53 begin 54 readln(n); 55 l:=maxn-100+1;r:=maxn-100+2; 56 rt:=l; 57 c[l,1]:=r;fa[r]:=l;s[l]:=2;s[r]:=1; 58 now:=1;tot:=0; 59 for i:=1 to n do 60 begin 61 read(ch); 62 case ch of 63 'P':begin 64 readln; 65 dec(now); 66 end; 67 'N':begin 68 readln; 69 inc(now); 70 end; 71 'M':begin 72 while ch<>' ' do read(ch);readln(x); 73 now:=x+1; 74 end; 75 'I':begin 76 while ch<>' ' do read(ch);readln(m); 77 readln(st);while length(st)<m do begin readln(st2);st:=st+st2;end; 78 for j:=1 to m do begin inc(tot);fa[tot]:=tot-1;v[tot]:=st[j];s[tot]:=m-j+1;c[tot,0]:=0;if j<>m then c[tot,1]:=tot+1 else c[tot,1]:=0;end; 79 x:=find(rt,now);y:=find(rt,now+1); 80 splay(x,rt);splay(y,c[x,1]); 81 c[y,0]:=tot-m+1;fa[tot-m+1]:=y; 82 pushup(y);pushup(x); 83 end; 84 'D':begin 85 while ch<>' ' do read(ch);readln(m); 86 x:=find(rt,now);y:=find(rt,now+m+1); 87 splay(x,rt);splay(y,c[x,1]); 88 z:=c[y,0];fa[z]:=0;c[y,0]:=0; 89 pushup(y);pushup(x); 90 end; 91 'G':begin 92 while ch<>' ' do read(ch);readln(m); 93 x:=find(rt,now);y:=find(rt,now+m+1); 94 splay(x,rt);splay(y,c[x,1]); 95 print(c[y,0]);writeln; 96 end; 97 end; 98 end; 99 end; 100 begin 101 assign(input,'input.txt');assign(output,'output.txt'); 102 reset(input);rewrite(output); 103 main; 104 close(input);close(output); 105 end.