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

毕竟模板

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. js 数组方法大集合,各方法是否改变原有的数组详解

    不会改变原来数组的有: concat()---连接两个或更多的数组,并返回结果. every()---检测数组元素的每个元素是否都符合条件. some()---检测数组元素中是否有元素符合指定条件. ...

  2. 查看电脑是否安装node.js

    打开命令行

  3. C#关系运算符

    一.C#关系运算符 C#语言的关系运算符是对操作数的比较运算. 二.示例 using System;using System.Collections.Generic;using System.Linq ...

  4. DOTA自走棋卡牌及搭配阵容

    这个游戏其实就根炉石jjc和A牌轮抽一样,前期要找着质量牌抓,保证你至少不漏.根据你的需求补一些你不会上场的阵容组件,最后根据你的组件和核心紫卡来哪张来决定打什么.另外也要考虑场上另外几家,如果有一家 ...

  5. http 调用错误处理

    1. http code 在使用Nginx时,经常会碰到502 Bad Gateway和504 Gateway Time-out错误,下面以Nginx+PHP-FPM来分析下这两种常见错误的原因和解决 ...

  6. 在mac下使用python抓取数据

    2015已经过去,这是2016的第一篇博文! 祝大家新年快乐! 但是我还有好多期末考试! 还没开始复习,唉,一把辛酸泪! 最近看了一遍彦祖的文章叫做 iOS程序员如何使用Python写网路爬虫 所以自 ...

  7. RabbitMQ-消费者"未处理完的消息"丢失

    一个关于客户端(消费者)开启自动应答,重启后"未处理消息丢失"的小坑.(主要是对RabbitMQ理解不够) 首先,申明一下: 本文所谓的 "丢失消息" 不是指服 ...

  8. css中让元素隐藏的多种方法

    { display: none; /* 不占据空间,无法点击 / } { visibility: hidden; / 占据空间,无法点击 / } { position: absolute; top: ...

  9. 爬取代理IP,并判断是否可用。

    # -*- coding:utf-8 -*- from gevent import monkey monkey.patch_all() import urllib2 from gevent.pool ...

  10. linux 编辑文档

    本篇主要分享下vi 命令行的操作: vi  /etc/sysconfig/iptabels 首先我们需要理解putty客户端的复制 粘贴 插入文档  退出等命令 复制:指在putty客户端中的选择复制 ...