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. VirtualBox的usb支持

    解决usb支持: 0. 下载Oracle_VM_VirtualBox_Extension_Pack-4.0.4-70112.vbox-extpack后双击即可采用VB安装,若还是用ark打开可人为设置 ...

  2. ListBoxControl 删除选择的项的方法

    public void RemoveSelectItems<T>(BaseListBoxControl listControl)        {            var list ...

  3. web项目学习之spring-security

    转自<http://liukai.iteye.com/blog/982088> spring security功能点总结: 1. 登录控制 2. 权限控制(用户菜单的显示,功能点访问控制) ...

  4. nodejs端口被占用。

    I had the same issue. I ran: $ ps aux | grep node to get the process id, then: $ sudo kill -9 follow ...

  5. poj 2240 Arbitrage (Floyd)

    链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 Britis ...

  6. XZ压缩

    XZ压缩最新压缩率之王 xz这个压缩可能很多都很陌生,不过您可知道xz是绝大数linux默认就带的一个压缩工具. 之前xz使用一直很少,所以几乎没有什么提起. 我是在下载phpmyadmin的时候看到 ...

  7. Python开发【第二十三篇】:持续更新中...

    Python开发[第二十三篇]:持续更新中...

  8. Linux命令行编辑快捷键

    Linux命令行编辑快捷键: history 显示命令历史列表 ↑(Ctrl+p) 显示上一条命令 ↓(Ctrl+n) 显示下一条命令 !num 执行命令历史列表的第num条命令 !! 执行上一条命令 ...

  9. Less 关于css hack的写法

    由于工作需要,最近一直在弄css转写less,遇到最多的问题就是 hack的写法,一些IE的hack,less不支持编译: 常见的不支持的hack如下: IE的滤镜写法 \9\0    IE8部分支持 ...

  10. Chrome浏览器允许跨域请求配置

    最近有个做数据标注的任务,但是标注平台是别人公司的,他们又不愿意对平台进行升级改造: 其实要改的地方也很简单,就是对页面做一些处理,做一些脚本控制. 没办法,做了个 iframe 给她嵌入到我们自己的 ...