1269: [AHOI2006] text editor editor Time Limit:10 Sec Memory limit:162 MB
submit:2678 solved:996
[Submit] [Status] [Discuss] Description
These days, can not play with Kaka, originally cocoa is sleeping and sleeping to do a simple and efficient text editor. Can you help him? To clarify the task target, Cocoa makes an abstract definition of the text editor: text: A sequence of 0 or more characters. The ASCII codes for these characters are within the closed interval [32, 126], meaning that these characters are visible characters or spaces. Cursor: A marker in a text that indicates a position, which can precede the first character of the text, after the last character of the text, or between some two adjacent characters of the text. Text Editor: A program that can perform the following seven actions on a piece of text and a cursor in the text. If this text is empty, we will say that the text editor is empty. Write a program: Create an empty text editor. Read some operation instructions from the input file and execute it. writes the specified content to the output file for all performed get operations. Input
The first line in the input file is the number of instruction bars N, and here are the N operations that need to be performed. In addition to the carriage return character, the ASCII code for all characters of the input file is within the closed interval [32, 126]. And there are no spaces at the end of the line. Output
The output of each get instruction in the input file, in turn, must not have any extra characters. 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
We have the following assumptions about the input data: The move operation does not exceed 50 000, the total number of INSERT, delete, and rotate operations does not exceed 6 000,get operation is not more than 20 000, the total number of Prev and next operations does not exceed 20 000. The sum of the characters of all insert inserts is not more than 2M (1m=1 024*1 024). delete operations, rotate operations, and get operations execute when the cursor must have enough characters. Move, PREV, next do not move the cursor to an illegal position. There is no error in the input file.
Source
Acknowledgement Seter re-authoring data
This problem is obviously a splay maintenance interval problem, but here the value is a character rather than an integer, in fact, the truth is the same.
When I first submitted the input write scanf ("%d\n", &n) The result was tle, and later changed to scanf ("%d%*c", &n) the result was right.
Note: In scanf, if the% plus * indicates that the input is not assigned to the corresponding variable after reading it, the value is skipped.
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring > #include <algorithm> #define F (I,j,n) for (int. i=j;i<=n;i++) #define D (i,j,n) for (int i=j;i>=n;i--) #
Define LL Long long #define PA pair<int,int> #define MAXN 2100000 #define INF 1000000000 #define KEY C[c[rt][1]][0]
using namespace Std;
int POS=1,RT=0,TOT=0,N,X,C[MAXN][2],S[MAXN],FA[MAXN];
Char V[MAXN],OP[10],CH[MAXN];
BOOL REV[MAXN];
inline void pushup (int k) {if (!k) return;
s[k]=s[c[k][0]]+s[c[k][1]]+1;
} inline void update (int k) {if (!k) return;
Rev[k]^=1;
Swap (c[k][0],c[k][1]);
} inline void pushdown (int k) {if (!k) return;
if (Rev[k]) {update (c[k][0]); update (c[k][1]);
rev[k]=0;
}} inline void rotate (int x,int &k) {int y=fa[x],z=fa[y],l= (c[y][1]==x), r=l^1;
if (y==k) k=x;
else c[z][c[z][1]==y]=x;
Fa[x]=z;fa[y]=x;fa[c[x][r]]=y;
C[y][l]=c[x][r];c[x][r]=y;
Pushup (y);p ushup (x); } inline void splay (int x,int &k) {while (x!=k)
{int y=fa[x],z=fa[y];
if (y!=k) {if ((c[y][0]==x) ^ (c[z][0]==y)) rotate (x,k);
else rotate (y,k);
} rotate (x,k);
}} inline int find (int k,int rank) {pushdown (k);
int l=c[k][0],r=c[k][1];
if (S[l]+1==rank) return k;
else if (S[l]>=rank) return find (L,rank);
else return find (r,rank-s[l]-1);
} inline void Split (int l,int r) {int X=find (RT,L-1), Y=find (rt,r+1);
Splay (X,RT); splay (y,c[rt][1]);
} inline void NewNode (int &k,char ch,int last) {K=++tot;
Fa[k]=last;
V[k]=ch;
c[k][0]=c[k][1]=rev[k]=0;
S[k]=1;
} inline void ins (int &k,int l,int r,int last) {int mid= (L+R) >>1;
NewNode (K,ch[mid],last);
if (l<mid) ins (c[k][0],l,mid-1,k);
if (mid<r) ins (c[k][1],mid+1,r,k);
Pushup (k);
} inline void Solveins () {scanf ("%d%*c", &x);
Gets (CH);
Split (Pos+1,pos);
INS (key,0,x-1,c[rt][1]);
Pushup (c[rt][1]);p ushup (RT);
} inline void Solvedel () {scanf ("%d%*c", &x);
Split (POS+1,POS+X);
fa[key]=0;key=0; Pushup (c[rt][1]);p ushup (RT);
} inline void Solverev () {scanf ("%d%*c", &x);
Split (POS+1,POS+X);
Update (key);
} int main () {newnode (RT, ' * ', 0); NewNode (c[rt][1], ' * ', RT);p Ushup (RT);
scanf ("%d%*c", &n);
F (i,1,n) {scanf ("%s%*c", op);
if (op[0]== ' M ') {scanf ("%d%*c", &x);p os=x+1;}
else if (op[0]== ' I ') solveins ();
else if (op[0]== ' D ') Solvedel ();
else if (op[0]== ' R ') Solverev ();
else if (op[0]== ' G ') printf ("%c\n", V[find (rt,pos+1)]);
else if (op[0]== ' P ') pos--;
else pos++;
}
}