uva12538

12538 Version Controlled IDE
Programmers use version control systems to manage files in their projects, but in these systems, versions
are saved only when you manually submit. Can you implement an IDE that automatically saves a new
version whenever you insert or delete a string?
Positions in the buffer are numbered from 1 from left to right. Initially, the buffer is empty and in
version 0. Then you can execute 3 commands (vnow means the version before executing the command,
and L[v] means the length of buffer at version v):
1 p s: insert string s after position p (0 p L[vnow], p = 0 means insert before the start of the
buffer). s contains at most 1 and at most 100 letters.
2 p c: remove c characters starting at position p (p 1, p + c L[vnow] + 1). The remaining
charactesr (if any) will be shifted left, filling the blank
3 v p c: print c characters starting at position p (p 1, p + c L[v] + 1), in version v (1 v
vnow).
The first command is guaranteed to be command 1(insert). After executing each command 1 or 2,
version is incremented by 1.
Input
There is only one test case. It begins with a single integer n (1 n 50, 000), the number of commands.
Each of the following n lines contains a command. The total length of all inserted string will not
exceed 1,000,000.
Output
Print the results of command 3, in order. The total length of all printed strings will not exceed 200,000.
Obfuscation:
In order to prevent you from preprocessing the command, we adopt the following obfuscation scheme:
Each type-1 command becomes 1 p + d s
Each type-2 command becomes 2 p + d c + d
Each type-3 command becomes 3 v + d p + d c + d
Where d is the number of lowercase letter ‘c’ you printed, before processing this command.
Before the obfuscation, the sample input would be:
6
1 0 abcdefgh
2 4 3
3 1 2 5
3 2 2 3
1 2 xy
3 3 2 4
This is the real input that your program must process when it reads the Sample Input
below.

Sample Input
6
1 0 abcdefgh
2 4 3
3 1 2 5
3 3 3 4
1 4 xy
3 5 4 6
Sample Output
bcdef
bcg
bxyc

题解:题意就是要你实现一个超级文本编辑器(只有插入和删除),然后这就是可持久化treep了

code:

  1 #include<cstdio>
  2 #include<iostream>
  3 #include<cmath>
  4 #include<cstring>
  5 #include<algorithm>
  6 using namespace std;
  7 char ch;
  8 bool ok;
  9 void read(int &x){
 10     ok=0;
 11     for (ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=1;
 12     for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
 13     if (ok) x=-x;
 14 }
 15 typedef pair<int,int> pii;
 16 const int maxnode=4000000;
 17 const int maxn=50005;
 18 char s[maxnode];
 19 int q,op,tim,pos,id,len,cnt;
 20 int random(int lim){return rand()%lim+1;}
 21 struct treep{
 22     int root[maxn];
 23     char node[maxnode];
 24     int tot,son[maxnode][2],siz[maxnode];
 25     void init(){
 26         srand('f'+'u'+'c'+'k'),tot=0;
 27         memset(root,0,sizeof(root));
 28         memset(son,0,sizeof(son));
 29         memset(siz,0,sizeof(siz));
 30     }
 31     void update(int a){
 32         siz[a]=1;
 33         if (son[a][0]) siz[a]+=siz[son[a][0]];
 34         if (son[a][1]) siz[a]+=siz[son[a][1]];    
 35     }
 36     int newnode(char ch,int ls,int rs){
 37         node[++tot]=ch,son[tot][0]=ls,son[tot][1]=rs,update(tot);
 38         return tot;
 39     }
 40     int build(int l,int r){
 41         if (l>r) return 0;
 42         int m=(l+r)>>1;
 43         return newnode(s[m],build(l,m-1),build(m+1,r));
 44     }
 45     pii split(int a,int k){
 46         if (!k) return make_pair(0,a);
 47         if (k==siz[a]) return make_pair(a,0);
 48         pii tmp;
 49         if (k<=siz[son[a][0]]){
 50             tmp=split(son[a][0],k);
 51             return make_pair(tmp.first,newnode(node[a],tmp.second,son[a][1]));
 52         }
 53         else{
 54             tmp=split(son[a][1],k-siz[son[a][0]]-1);
 55             return make_pair(newnode(node[a],son[a][0],tmp.first),tmp.second);
 56         }
 57     }
 58     int merge(int a,int b){
 59         if (!a||!b) return a+b;
 60         if (random(siz[a]+siz[b])<=siz[a]) return newnode(node[a],son[a][0],merge(son[a][1],b));
 61         else return newnode(node[b],merge(a,son[b][0]),son[b][1]);
 62     }
 63     void watch(int a){
 64         if (son[a][0]) watch(son[a][0]);
 65         putchar(node[a]);
 66         if (node[a]=='c') cnt++;
 67         if (son[a][1]) watch(son[a][1]);    
 68     }
 69     void insert(int id,int pos){
 70         int a,b,c;
 71         pii tmp=split(root[id],pos);
 72         a=tmp.first,c=tmp.second,b=build(1,len);
 73         root[++tim]=merge(merge(a,b),c);
 74     }
 75     void remove(int id,int l,int r){
 76         int a,b,c;
 77         pii tmp=split(root[id],r);
 78         c=tmp.second,tmp=split(tmp.first,l-1),a=tmp.first,b=tmp.second;
 79         root[++tim]=merge(a,c);    
 80     }
 81     void look(int id,int l,int r){
 82         int a,b,c;
 83         pii tmp=split(root[id],r);
 84         c=tmp.second,tmp=split(tmp.first,l-1),a=tmp.first,b=tmp.second;
 85         watch(b),puts("");
 86     }
 87 }T;
 88 int main(){
 89     for (read(q);q;q--){
 90         read(op);
 91         if (op==1){
 92             read(pos),scanf("%s",s+1),len=strlen(s+1),pos-=cnt;
 93             T.insert(tim,pos);
 94         }
 95         else if (op==2){
 96             read(pos),read(len),pos-=cnt,len-=cnt;
 97             T.remove(tim,pos,pos+len-1);
 98         }
 99         else if (op==3){
100             read(id),read(pos),read(len),id-=cnt,pos-=cnt,len-=cnt;
101             T.look(id,pos,pos+len-1);
102         }
103     }
104     return 0;
105 }

 

posted @ 2016-02-05 16:52  chenyushuo  阅读(256)  评论(0编辑  收藏  举报