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:
#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的更多相关文章
- UVA12538 Version Controlled IDE
题意翻译 维护一种数据结构,资磁三种操作. 1.在p位置插入一个字符串s 2.从p位置开始删除长度为c的字符串 3.输出第v个历史版本中从p位置开始的长度为c的字符串 1≤n≤50000,所有字符串总 ...
随机推荐
- 教你修改Linux下高并发socket最大连接数所受的各种限制
1.修改用户进程可打开文件数限制 在Linux平台上,无论编写客户端程序还是服务端程序,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开 文件数量的限制(这是因为系统为 ...
- UVaLive4043 UVa1411 Ants 巨人与鬼
题意:给出平面上n个白点n个黑点,要求两两配对,且配对所连线段没有交点. 法一:暴力 随机一个初始方案,枚举任意两条线段如果有交点就改一下. 效率其实挺好的. 法二:二分图最佳完美匹配 显然没有交点的 ...
- ICSharpCode.SharpZipLib压缩解压
一.使用ICSharpCode.SharpZipLib.dll: 下载地址 http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.asp ...
- EXCEL 如何将多个工作表或工作簿合并到一个工作表
在使用Excel 时,我们经常需要将多个工作表或工作簿合并到一个工作表中,这样我们就能快速地对数据进行分析和统计.对于一般用户而言,除了复制每个工作表后再粘贴,没有其他什么方法了.如果只是合并少数几个 ...
- Android read-only file system解决方法
adb shell su - mount -o rw,remount /system
- Fragment之我的解决方案:Fragmentation
Fragment系列文章:1.Fragment全解析系列(一):那些年踩过的坑2.Fragment全解析系列(二):正确的使用姿势3.Fragment之我的解决方案:Fragmentation 如果你 ...
- java 钱币的单位转换
将钱转成转换为带指定单位的钱 int money = 10; NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US); System ...
- Array,ArrayList 和 List<T>的选择和性能比较.
Array Class Provides methods for creating, manipulating, searching, and sorting arrays, thereby serv ...
- oracle 里面定时执行任务,比如存储过程内容等。
DECLARE job_no_ NUMBER; BEGIN DBMS_JOB.SUBMIT(job_no_, 'proc_qszx_dw_sc(' ...
- ASP.NET Excel数据导出数据库
/// <summary> /// 根據gridview導出excel /// </summary> /// <param name="ctl"> ...