[NOI2003][bzoj1507] 文本编辑器 editor [splay]
其实看明白了就是一道水题
毕竟模板
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]的更多相关文章
- 【bzoj1507】[NOI2003]Editor /【bzoj1269】[AHOI2006]文本编辑器editor Splay
[bzoj1507][NOI2003]Editor 题目描述 输入 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中 ...
- 【BZOJ1269/1507】[AHOI2006]文本编辑器editor Splay
[BZOJ1269][AHOI2006]文本编辑器editor Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目 ...
- BZOJ 1269: [AHOI2006]文本编辑器editor( splay )
splay..( BZOJ 1507 题目基本相同..双倍经验 ) ------------------------------------------------------------------ ...
- [bzoj1269][AHOI2006文本编辑器editor] (splay模版题 or pb_ds [rope]大法)
Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义: 文本:由0个或 ...
- [BZOJ1269] [AHOI2006] 文本编辑器editor (splay)
Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目标,可可对“文本编辑器”做了一个抽象的定义: 文本:由0个或多 ...
- BZOJ-1507 文本编辑器(Editor)
一道极其相似的题...http://hi.baidu.com/8361101/item/5b149103cbf4007cbee97e5f 就多了个区间查找,少了个翻转... 少了翻转的话貌似可以不用S ...
- BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor
BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor 题意: 分析: splay模拟即可 注意1507的读入格式 ...
- BZOJ 1269: [AHOI2006]文本编辑器editor (splay tree)
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1213 Solved: 454[Submit ...
- BZOJ1269 [AHOI2006]文本编辑器editor 【82行splay】
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MB Submit: 4633 Solved: 1782 [Sub ...
随机推荐
- Python实现进度条小程序
一.print()参数介绍 1.end:指定打印结束后添加的字符,默认值为换行符 for j in range(3): print('hello world') for i in range(3): ...
- 10个HTML5 实战教程 提升你的综合开发能力
HTML5 作为下一代网站开发技术,无论你是一个 Web 开发人员或者想探索新的平台的游戏开发者,都值得去研究.借助尖端功能,技术和 API,HTML5 允许你创建响应性.创新性.互动性以及令人惊叹的 ...
- java基础编程—统计二进制数中1的个数
题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 题目代码 /** * 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. * Created by YuKai ...
- java定义一个Circle类,包含一个double型的radius属性代表圆的半径,一个findArea()方法返回圆的面积
需求如下:(1)定义一个Circle类,包含一个double型的radius属性代表圆的半径,一个findArea()方法返回圆的面积. (2)定义一个类PassObject,在类中定义一个方法pri ...
- Uva 填充正方形
暴力出奇迹 #include<iostream> #include<cstdio> using namespace std; +; int T,n; char S[maxn][ ...
- 2018年湘潭大学程序设计竞赛 E 吃货
题目描述 作为一个标准的吃货,mostshy又打算去联建商业街觅食了.混迹于商业街已久,mostshy已经知道了商业街的所有美食与其价格,而且他给每种美食都赋予了一个美味度,美味度越高表示他越喜爱这种 ...
- Storm: 性能优化 (转载)
Storm 性能优化 原文地址:http://www.jianshu.com/p/f645eb7944b0 目录 场景假设 调优步骤和方法 Storm 的部分特性 Storm 并行度 Storm 消 ...
- day 16 JS DOM 继续
为什么有jquey了还学DOM ? 因为JQuey 是大而全,可能有10k 但是我们用到的只有1k 网站小没事,网站大流量就是问题 所以大网站都是自己用DOM 实现一个类似于JQuey 的适合自己 ...
- Spring MVC 使用 HttpServletResponseWrapper 修改返回结果
HttpServletResponseWrapper 是什么? ServletResponse 的包装类,相关设计模式 装饰者模式. 运行环境 jdk 1.7 spring boot 整合的web环境 ...
- 【精彩回顾】第二届微医前端技术沙龙(附PPT下载)
5 月 25 日,以「无界」为主题的第二届微医前端技术沙龙成功举办.本届沙龙的演讲题目涵盖了前端技术几个主要的应用场景,包括服务端.桌面端以及跨平台的开发.最近几年前端技术发展非常快,各种可以提高开发 ...