[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 ...
随机推荐
- 2018.5.24 Oracle下的sqlplus编程 块结构
1.语句结构模板 declare --声明 begin dbms_output.put_line('Legend Hello world'); end; 2.变量使用 & 是输入符号 decl ...
- ambari过程中要求各个节点时间同步
设置时间同步 控制节点机器 cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime #设置时区为北京时间,这里为上海,因为centos里面只有上海... ...
- Java 性能优化的五大技巧
要对你的 Java 代码进行优化,需要理解 Java 不同要素之间的相互作用,以及它是如何与其运行时的操作系统进行交互的.使用下面这五个技巧和资源,开始学习如何分析和优化你的代码吧. 在我们开始之前, ...
- 去除myeclipse中doget和dopost方法中的注释
当我们使用myeclipse新建servlet时发现doget和dopost方法中有一些无用的注释,每次新建一个servlet时都要手动删除特别麻烦. 下面就教大家如何去除这些注释! 以myeclip ...
- 1207: [HNOI2004]打鼹鼠
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4154 Solved: 1997[Submit][Status][Discuss] Descript ...
- 【计数】cf938E. Max History
发现有一种奇怪的方法不能快速预处理? 复习一下常见的凑组合数的套路 You are given an array a of length n. We define fa the following w ...
- C语言:自己编写的简易ftp客户端,包含(列表,进入目录,上传文件,下载文件,删除文件)功能
//简易ftp客户端#include <stdio.h> #include <string.h> #include <sys/types.h> #include & ...
- 无屏幕和键盘配置树莓派WiFi和SSH
原文转载:http://shumeipai.nxez.com/2017/09/13/raspberry-pi-network-configuration-before-boot.html 不算是什么新 ...
- Django2.1中的分页功能详解
django的分页功能类将我们常用的多种方法均封装在Paginator类,根据这些方法我们均可深度定制我们的分页功能. 首先来看看[Paginator] 类的构造方法: class Paginator ...
- [译]The Python Tutorial#7. Input and Output
[译]The Python Tutorial#Input and Output Python中有多种展示程序输出的方式:数据可以以人类可读的方式打印出来,也可以输出到文件中以后使用.本章节将会详细讨论 ...