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:

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
char ch;
bool ok;
void read(int &x){
ok=;
for (ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
typedef pair<int,int> pii;
const int maxnode=;
const int maxn=;
char s[maxnode];
int q,op,tim,pos,id,len,cnt;
int random(int lim){return rand()%lim+;}
struct treep{
int root[maxn];
char node[maxnode];
int tot,son[maxnode][],siz[maxnode];
void init(){
srand('f'+'u'+'c'+'k'),tot=;
memset(root,,sizeof(root));
memset(son,,sizeof(son));
memset(siz,,sizeof(siz));
}
void update(int a){
siz[a]=;
if (son[a][]) siz[a]+=siz[son[a][]];
if (son[a][]) siz[a]+=siz[son[a][]];
}
int newnode(char ch,int ls,int rs){
node[++tot]=ch,son[tot][]=ls,son[tot][]=rs,update(tot);
return tot;
}
int build(int l,int r){
if (l>r) return ;
int m=(l+r)>>;
return newnode(s[m],build(l,m-),build(m+,r));
}
pii split(int a,int k){
if (!k) return make_pair(,a);
if (k==siz[a]) return make_pair(a,);
pii tmp;
if (k<=siz[son[a][]]){
tmp=split(son[a][],k);
return make_pair(tmp.first,newnode(node[a],tmp.second,son[a][]));
}
else{
tmp=split(son[a][],k-siz[son[a][]]-);
return make_pair(newnode(node[a],son[a][],tmp.first),tmp.second);
}
}
int merge(int a,int b){
if (!a||!b) return a+b;
if (random(siz[a]+siz[b])<=siz[a]) return newnode(node[a],son[a][],merge(son[a][],b));
else return newnode(node[b],merge(a,son[b][]),son[b][]);
}
void watch(int a){
if (son[a][]) watch(son[a][]);
putchar(node[a]);
if (node[a]=='c') cnt++;
if (son[a][]) watch(son[a][]);
}
void insert(int id,int pos){
int a,b,c;
pii tmp=split(root[id],pos);
a=tmp.first,c=tmp.second,b=build(,len);
root[++tim]=merge(merge(a,b),c);
}
void remove(int id,int l,int r){
int a,b,c;
pii tmp=split(root[id],r);
c=tmp.second,tmp=split(tmp.first,l-),a=tmp.first,b=tmp.second;
root[++tim]=merge(a,c);
}
void look(int id,int l,int r){
int a,b,c;
pii tmp=split(root[id],r);
c=tmp.second,tmp=split(tmp.first,l-),a=tmp.first,b=tmp.second;
watch(b),puts("");
}
}T;
int main(){
for (read(q);q;q--){
read(op);
if (op==){
read(pos),scanf("%s",s+),len=strlen(s+),pos-=cnt;
T.insert(tim,pos);
}
else if (op==){
read(pos),read(len),pos-=cnt,len-=cnt;
T.remove(tim,pos,pos+len-);
}
else if (op==){
read(id),read(pos),read(len),id-=cnt,pos-=cnt,len-=cnt;
T.look(id,pos,pos+len-);
}
}
return ;
}

uva12538的更多相关文章

  1. UVA12538 Version Controlled IDE

    题意翻译 维护一种数据结构,资磁三种操作. 1.在p位置插入一个字符串s 2.从p位置开始删除长度为c的字符串 3.输出第v个历史版本中从p位置开始的长度为c的字符串 1≤n≤50000,所有字符串总 ...

随机推荐

  1. automake---让Makefile变得更专业一点儿

    一般我们装软件时,都要运行 ./configure --prefix=/usr/local make make install 看着不断刷新的屏幕,总感觉真得好高深呀,其实我们的程序也可以这样子. 下 ...

  2. IDEA新建SpringMVC项目报错解决办法

    网页运行的错误: HTTP Status 500 - Handler processing failed; nested exception is java.lang.NoClassDefFoundE ...

  3. 纪录参加noip2015(pj+tg)复赛

    作为一个既参加了pj又参加了tg的初三蒟蒻,本次复赛不得不算一次很happy的事(可以不做周末作业,可以逃掉小班培训) 昨年参加pj的时候,一题眼瞎,二题作死,只有三题蒙了一点分,简直差到一种境界. ...

  4. IE8下提示&#39;console&#39;没有定义错误

    在开发的过程中因为调试的原因,在代码中增加console.info("xxxx"),而未进行删除 在IE8下測试该代码所在的页面报错,例如以下: 须要注意的是,使用console对 ...

  5. Oracle、MySql、Sql Server比对

    1.    价格 MySql:廉价(部分免费):当前,MySQL採用双重授权(DualLicensed),他们是GPL和MySQLAB制定的商业许可协议.假设你在一个遵循GPL的自由(开源)项目中使用 ...

  6. PHP问题Parse error: syntax error, unexpected end of file in

    检查一下你的php文件中是否存在这样的语法错误:<<php{>或者<?{?>以上两种写法都是有错误的,修改为下面的就可以了: <?php}?>

  7. Linux入门基础 #5:Linux文件系统挂载管理

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  8. Android 颜色渲染(九) PorterDuff及Xfermode详解

    版权声明:本文为博主原创文章,未经博主允许不得转载. Android 颜色渲染(九)  PorterDuff及Xfermode详解 之前已经讲过了除ComposeShader之外Shader的全部子类 ...

  9. Python 学习 第十篇 CMDB用户权限管理

    Python 学习 第十篇 CMDB用户权限管理 2016-10-10 16:29:17 标签: python 版权声明:原创作品,谢绝转载!否则将追究法律责任. 不管是什么系统,用户权限都是至关重要 ...

  10. codevs 1519 过路费 最小生成树+倍增

    /*codevs 1519 过路费 最小生成树+倍增*/ #include<iostream> #include<cstdio> #include<cstring> ...