其实看明白了就是一道水题

毕竟模板

splay敲一发,插入一个串的时候先把它构建成一棵平衡树,再挂到原来的splay上面去即可

没别的了,上代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
inline int read(){
int re=,flag=;char ch=getchar();
while(ch>''||ch<''){
if(ch=='-') flag=-;
ch=getchar();
}
while(ch>=''&&ch<='') re=(re<<)+(re<<)+ch-'',ch=getchar();
return re*flag;
}
int n,cnt=,root=,mouse=;
int fa[]={},ch[][]={},siz[]={};
char w[]={};
void update(int x){siz[x]=siz[ch[x][]]+siz[ch[x][]]+;}
int get(int x){return ch[fa[x]][]==x;}
void rotate(int x){
int f=fa[x],ff=fa[f],son=get(x);
ch[f][son]=ch[x][son^];
if(ch[f][son]) fa[ch[f][son]]=f;
fa[f]=x;ch[x][son^]=f;
fa[x]=ff;
if(ff) ch[ff][ch[ff][]==f]=x;
update(f);update(x);
}
void splay(int x,int to){
// cout<<"splay "<<x<<ends<<" "<<endl;
if(x==to||fa[x]==to) return;
if(to==) root=x;
for(int f;(f=fa[x])&&(f!=to);rotate(x)){
if(fa[f]!=to)
rotate(get(x)==get(f)?f:x);
}
update(x);
}
int rank(int x,int pos){
// cout<<"rank "<<x<<ends<<siz[pos]<<ends<<siz[ch[pos][0]]<<ends<<pos<<endl;
// if(pos==0) system("pause");
if(siz[ch[pos][]]+==x){
splay(pos,);return pos;
}
if(siz[ch[pos][]]>=x) return rank(x,ch[pos][]);
else return rank(x-siz[ch[pos][]]-,ch[pos][]);
}
char s[]={};
int build(int l,int r,int f){
if(l>r) return ;
// cout<<"build "<<l<<ends<<r<<ends<<f<<endl;
int mid=(l+r)>>,cur=++cnt;
fa[cur]=f;w[cur]=s[mid];
ch[cur][]=build(l,mid-,cur);
ch[cur][]=build(mid+,r,cur);
update(cur);return cur;
}
void insert(int l,int len){
int x=rank(l,root),y=rank(l+,root);
splay(x,);splay(y,root);
ch[y][]=build(,len,y);
update(y);update(x);
}
void del(int l,int r){
int x=rank(l,root),y=rank(r+,root);
splay(x,);splay(y,root);
ch[y][]=;update(y);update(x);
}
void dfs(int x){
if(!x) return;
dfs(ch[x][]);
printf("%c",w[x]);
dfs(ch[x][]);
}
void print(int l,int len){
int x=rank(l,root),y=rank(l+len+,root);
splay(x,);splay(y,root);
dfs(ch[y][]);puts("");
}
int main(){
// freopen("editor2003.in","r",stdin);
// freopen("editor2003.out","w",stdout);
int i,j,t1;char op[];char c;
n=read();
root=++cnt;w[cnt]=;siz[cnt]=;
ch[cnt][]=cnt+;cnt++;fa[cnt]=cnt-;w[cnt]=;siz[cnt]=;
mouse=;
for(i=;i<=n;i++){
scanf("%s",op);
if(op[]=='I'){
t1=read();
for(j=;j<=t1;j++){
c=getchar();
while(c=='\n') c=getchar();
s[j]=c;
// cout<<"inserted "<<s[j]<<endl;
}
insert(mouse,t1);
}
if(op[]=='D'){
t1=read();
del(mouse,mouse+t1-);
}
if(op[]=='G'){
t1=read();
print(mouse,t1);
}
if(op[]=='M'){
t1=read();
mouse=t1+;
}
if(op[]=='N') mouse++;
if(op[]=='P') mouse--;
}
}

奇怪的是,COGS和luogu上都说我没有输出

可是我Windows下都过了啊......

大概是时候装Linux了

UPDATE 18/1/30

装了一个Ubuntu 16.04,发现是换行符的问题

Linux评测机下是\n,但是因为数据是在Windows下生成的,所以实际上读入的文件里是\r\n,所以没有读出来

New Code:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
inline int read(){
int re=,flag=;char ch=getchar();
while(ch>''||ch<''){
if(ch=='-') flag=-;
ch=getchar();
}
while(ch>=''&&ch<='') re=(re<<)+(re<<)+ch-'',ch=getchar();
return re*flag;
}
int n,cnt=,root=,mouse=;
int fa[]={},ch[][]={},siz[]={};
char w[]={};
void update(int x){siz[x]=siz[ch[x][]]+siz[ch[x][]]+;}
int get(int x){return ch[fa[x]][]==x;}
void rotate(int x){
int f=fa[x],ff=fa[f],son=get(x);
ch[f][son]=ch[x][son^];
if(ch[f][son]) fa[ch[f][son]]=f;
fa[f]=x;ch[x][son^]=f;
fa[x]=ff;
if(ff) ch[ff][ch[ff][]==f]=x;
update(f);update(x);
}
void splay(int x,int to){
// cout<<"splay "<<x<<ends<<" "<<endl;
if(x==to||fa[x]==to) return;
if(to==) root=x;
for(int f;(f=fa[x])&&(f!=to);rotate(x)){
if(fa[f]!=to)
rotate(get(x)==get(f)?f:x);
}
update(x);
}
int rank(int x,int pos){
// cout<<"rank "<<x<<ends<<siz[pos]<<ends<<siz[ch[pos][0]]<<ends<<pos<<endl;
// if(pos==0) system("pause");
if(siz[ch[pos][]]+==x){
splay(pos,);return pos;
}
if(siz[ch[pos][]]>=x) return rank(x,ch[pos][]);
else return rank(x-siz[ch[pos][]]-,ch[pos][]);
}
char s[]={};
int build(int l,int r,int f){
if(l>r) return ;
// cout<<"build "<<l<<ends<<r<<ends<<f<<endl;
int mid=(l+r)>>,cur=++cnt;
fa[cur]=f;w[cur]=s[mid];
ch[cur][]=build(l,mid-,cur);
ch[cur][]=build(mid+,r,cur);
update(cur);return cur;
}
void insert(int l,int len){
int x=rank(l,root),y=rank(l+,root);
splay(x,);splay(y,root);
ch[y][]=build(,len,y);
update(y);update(x);
}
void del(int l,int r){
int x=rank(l,root),y=rank(r+,root);
splay(x,);splay(y,root);
ch[y][]=;update(y);update(x);
}
void dfs(int x){
if(!x) return;
dfs(ch[x][]);
printf("%c",w[x]);
dfs(ch[x][]);
}
void print(int l,int len){
int x=rank(l,root),y=rank(l+len+,root);
splay(x,);splay(y,root);
dfs(ch[y][]);puts("");
}
int main(){
// freopen("editor20031.in","r",stdin);
// freopen("editor2003.out","w",stdout);
int i,j,t1;char op[];char c;
n=read();
root=++cnt;w[cnt]=;siz[cnt]=;
ch[cnt][]=cnt+;cnt++;fa[cnt]=cnt-;w[cnt]=;siz[cnt]=;
mouse=;
for(i=;i<=n;i++){
scanf("%s",op);
if(op[]=='I'){
t1=read();
for(j=;j<=t1;j++){
c=getchar();
while(c=='\n'||c=='\r') c=getchar();
s[j]=c;
}
insert(mouse,t1);
}
if(op[]=='D'){
t1=read();
del(mouse,mouse+t1-);
}
if(op[]=='G'){
t1=read();
print(mouse,t1);
}
if(op[]=='M'){
t1=read();
mouse=t1+;
}
if(op[]=='N') mouse++;
if(op[]=='P') mouse--;
}
}

[NOI2003][bzoj1507] 文本编辑器 editor [splay]的更多相关文章

  1. 【bzoj1507】[NOI2003]Editor /【bzoj1269】[AHOI2006]文本编辑器editor Splay

    [bzoj1507][NOI2003]Editor 题目描述 输入 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中 ...

  2. 【BZOJ1269/1507】[AHOI2006]文本编辑器editor Splay

    [BZOJ1269][AHOI2006]文本编辑器editor Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目 ...

  3. BZOJ 1269: [AHOI2006]文本编辑器editor( splay )

    splay..( BZOJ 1507 题目基本相同..双倍经验 ) ------------------------------------------------------------------ ...

  4. [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)

    Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义:   文本:由0个或 ...

  5. [BZOJ1269] [AHOI2006] 文本编辑器editor (splay)

    Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义:  文本:由0个或多 ...

  6. BZOJ-1507 文本编辑器(Editor)

    一道极其相似的题...http://hi.baidu.com/8361101/item/5b149103cbf4007cbee97e5f 就多了个区间查找,少了个翻转... 少了翻转的话貌似可以不用S ...

  7. BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor

    BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor 题意: 分析: splay模拟即可 注意1507的读入格式 ...

  8. BZOJ 1269: [AHOI2006]文本编辑器editor (splay tree)

    1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1213  Solved: 454[Submit ...

  9. BZOJ1269 [AHOI2006]文本编辑器editor 【82行splay】

    1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 4633  Solved: 1782 [Sub ...

随机推荐

  1. 【luogu P1783 海滩防御】 题解

    题目链接:https://www.luogu.org/problemnew/show/P1783 先把题目改造一下:题目所求是要一条能从0列到n列的路径,使其路径上的最大边长一半最小. 为什么是一半呢 ...

  2. 分享12款最佳的Bootstrap设计工具

    设计师总会渴望有一些新奇有趣的设计工具来提高工作效率,而Bootstrap就是您的不二选择.2013年Bootstrap得到了广泛普及, 它是开发者较为常用的框架之一,本文我们将分享12款最佳的Boo ...

  3. RabbitMQ使用教程(五)如何保证队列里的消息99.99%被消费?

    1. 前情回顾 RabbitMQ使用教程(一)RabbitMQ环境安装配置及Hello World示例 RabbitMQ使用教程(二)RabbitMQ用户管理,角色管理及权限设置 RabbitMQ使用 ...

  4. 自建ssr(谷歌云免费试用一年)

    近期我一个朋友的VPN到期了,他也不想再去续费,同时发现谷歌云第一年申请时是免费的,所以他就自己搭建了一个自己专属的VPN 以下是他的搭建教程:  本教程难点在于申请免费试用资格 谷歌云+ssr搭建免 ...

  5. jenkins重置build序号

    来源:https://www.jianshu.com/p/e342b52d45e1 执行命令:item = Jenkins.instance.getItemByFullName("your- ...

  6. 问题:Could not install packages due to an EnvironmentError: [Errno 13] Permission denied:

    1.安装django 执行pip3 install --user django 2.解决方法:加--user   执行pip3 install --user django

  7. yum仓库及配置

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 最近由于服务器需求,需要在公司内网搭建内网yum源. 搭建内网yum源需要分以下几个步骤,如下: 1. yum是什么 2. repo文件是什么 3. r ...

  8. 动态拼接SQL语句

    1.参考官方文档 ? if:字符判断 ? choose (when, otherwise):分支选择 ? trim (where, set):字符串截取:其中where标签封装查询条件,set标签封装 ...

  9. oracle for update和for update nowait 的区别

    原文地址:http://www.cnblogs.com/quanweiru/archive/2012/11/09/2762223.html 1.for update 和 for update nowa ...

  10. 《鸟哥的Linux私房菜》学习笔记(5)——权限管理

    一.权限的基本概念                                                   权限:访问计算机资源或服务的访问能力. Linux中,每一个资源或者服务的权限, ...