uva12538

本文介绍了一种使用可持久化Treap数据结构实现的文本编辑器,该编辑器支持插入和删除操作,并能自动保存版本,便于查看历史记录。文中详细解释了如何通过构建Treap来高效地处理这些操作,并附上了完整的代码实现。

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 }

 

转载于:https://www.cnblogs.com/chenyushuo/p/5183341.html

Matlab基于粒子群优化算法及鲁棒MPPT控制器提高光伏并网的效率内容概要:本文围绕Matlab在电力系统优化与控制领域的应用展开,重点介绍了基于粒子群优化算法(PSO)和鲁棒MPPT控制器提升光伏并网效率的技术方案。通过Matlab代码实现,结合智能优化算法与先进控制策略,对光伏发电系统的最大功率点跟踪进行优化,有效提高了系统在不同光照条件下的能量转换效率和并网稳定性。同时,文档还涵盖了多种电力系统应用场景,如微电网调度、储能配置、鲁棒控制等,展示了Matlab在科研复现与工程仿真中的强大能力。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的高校研究生、科研人员及从事新能源系统开发的工程师;尤其适合关注光伏并网技术、智能优化算法应用与MPPT控制策略研究的专业人士。; 使用场景及目标:①利用粒子群算法优化光伏系统MPPT控制器参数,提升动态响应速度与稳态精度;②研究鲁棒控制策略在光伏并网系统中的抗干扰能力;③复现已发表的高水平论文(如EI、SCI)中的仿真案例,支撑科研项目与学术写作。; 阅读建议:建议结合文中提供的Matlab代码与Simulink模型进行实践操作,重点关注算法实现细节与系统参数设置,同时参考链接中的完整资源下载以获取更多复现实例,加深对优化算法与控制系统设计的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值