[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 ...
随机推荐
- Redis学习记录(一)
在学习Redis之前,要知道什么是NoSQL? 1.NoSQL 1.1. 什么是NoSQL NoSQL(NoSQL = Not Only SQL),表示“不仅仅是SQL”,泛指非关系型数据库. 1.2 ...
- 更新MySQL数据库( java.sql.SQLException: No value specified for parameter 1) 异常 解决方法
package com.swift; import java.io.File; import java.sql.Connection; import java.sql.PreparedStatemen ...
- Everything Be True-freecodecamp算法题目
Everything Be True 1.要求 完善every函数,如果集合(collection)中的所有对象都存在对应的属性(pre),并且属性(pre)对应的值为真.函数返回ture.反之,返回 ...
- 1025: [SCOI2009]游戏
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 2727 Solved: 1794[Submit][Status][Discuss] Descripti ...
- 第七篇:suds.TypeNotFound: Type not found: '(string, http://schemas.xmlsoap.org/soap/encoding/, )'
想要用Python的suds模块调用webservice地址做自动测试,但是找了很多方法都失败了,最终找到另外一个模块可以作为客户端访问服务器地址. 1.针对非安全的http from zeep im ...
- 六、Linux 文件基本属性
Linux 文件基本属性 Linux系统是一种典型的多用户系统,不同的用户处于不同的地位,拥有不同的权限.为了保护系统的安全性,Linux系统对不同的用户访问同一文件(包括目录文件)的权限做了不同的规 ...
- windows_Bat_Scripts查看系统IP-更改regedit-更新系统补丁
1.1 脚本名称 Update_patch.bat 1.2 脚本代码 @echo off :menu cls mode con cols=48 lines=27 & color 0 ...
- JS — 实现简单的数字时钟
js实现简单的数字时钟 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...
- du 与df 统计系统磁盘不一致原因与解决方法
事件起因: 同事发现云主机磁盘系统盘满了,准备清理系统盘,便利用du 命令统计了根目录下各文件夹的大小,发现统计的各文件夹的大小总和 加起来比 df 命令查看到的系统盘所使用空间 要小很多.这里记录下 ...
- Leetcode 96. 不同的二叉搜索树
题目链接 https://leetcode.com/problems/unique-binary-search-trees/description/ 题目描述 给定一个整数 n,求以 1 ... n ...