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. JDK6 下载地址

    http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-javase6-41940 ...

  2. ColorComboBox

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; u ...

  3. AE二次开发中,过滤后的图层,实现缩放至图层效果

    //featureClass是自己获取的featureClass,也可是sde中获取的. public void FilterAndZoomToLayer(IFeatureClass featureC ...

  4. CSU1306:Manor(优先队列)

    Description Bob有n个正整数,他将这n个整数根据大小划分成两部分.对于小于等于k的整数放在集合A中,其余的放在集合B中.每次他从集合B中取出一个最大的值,将其变成0放入A集合中.然后将A ...

  5. leetcode-1 Two Sum 找到数组中两数字和为指定和

     问题描写叙述:在一个数组(无序)中高速找出两个数字,使得两个数字之和等于一个给定的值.如果数组中肯定存在至少一组满足要求. <剑指Offer>P214(有序数组) <编程之美& ...

  6. iOS--NSAttributedString使用介绍

    iOS–NSAttributedString使用介绍 原文见: http://www.itnose.net/detail/6177538.html http://***/html/topnews201 ...

  7. Android绘制流程

    一.前言 1.1.C++界面库 MFC.WTL.DuiLib.QT.Skia.OpenGL.Android里面的画图分为2D和3D两种: 2D是由Skia 来实现的,3D部分是由OpenGL实现的. ...

  8. Linux 关闭及重启方式

    一.shutdown 命令 作用:关闭或重启系统 使用权限:超级管理员使用 常用选项 1. -r 关机后立即重启 2. -h关机后不重启 3. -f快速关机,重启时跳过fsck(file system ...

  9. Python之路【第十七篇】:Django之【进阶篇】

    Python之路[第十七篇]:Django[进阶篇 ]   Model 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MySQLdb 来连接 ...

  10. WebForm开发常用代码

    1.获取服务器绝对路径: public static string GetMapPath(string strPath) { if (HttpContext.Current != null) { re ...