您的位置:首页 > 其它

BZOJ_1507_Editor_[NOI2003]_(Splay)

2016-05-16 21:59 239 查看

描述

http://www.lydsy.com/JudgeOnline/problem.php?id=1507

简单区间操作的模板题

 

 

1507: [NOI2003]Editor

Time Limit: 5 Sec  Memory Limit: 162 MB
Submit: 3092  Solved: 1244
[Submit][Status][Discuss]

 

Description

 

#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn=(2<<21)+5,oo=~0u>>1;

int n,x,at,cur;
char str[maxn],s[10];

struct Splay{
struct node{
node* ch[2],* pa;
char v; int s;
node(int v,node* t):v(v){ ch[0]=ch[1]=pa=t; s=1; }
bool d(){ return pa->ch[1]==this; }
void setc(node* t,bool d){ ch[d]=t; t->pa=this; }
void push_up(){ s=ch[0]->s+ch[1]->s+1; }
}*root,*null;
Splay(){
null=new node('\0',NULL); null->s=0;
root=new node('\0',null);
node* t=new node('\0',null);
root->setc(t,1);
root->push_up();
}
void rot(node* o){
node* pa=o->pa; bool d=o->d();
pa->pa->setc(o,pa->d());
pa->setc(o->ch[!d],d);
o->setc(pa,!d);
pa->push_up();
if(pa==root) root=o;
}
void splay(node* o,node* pa){
while(o->pa!=pa){
if(o->pa->pa==pa) rot(o);
else o->d()==o->pa->d()?(rot(o->pa),rot(o)):(rot(o),rot(o));
}
o->push_up();
}
node* kth(int k){
k++;
for(node* t=root;;){
int s=t->ch[0]->s+1;
if(s==k) return t;
if(k>s) k-=s,t=t->ch[1];
else t=t->ch[0];
}
}
node* build(int l,int r){
if(l==r) return new node(str[l],null);
if(l>r) return null;
int m=l+(r-l)/2;
node* t=new node(str[m],null);
t->setc(build(l,m-1),0);
t->setc(build(m+1,r),1);
t->push_up();
return t;
}
node* find(int l,int r){
node* L=kth(l); splay(L,null);
node* R=kth(r); splay(R,L);
return R;
}
void insert(int at,int cur){
node* t=find(at,at+1);
t->setc(build(0,cur),0); t->push_up();
splay(t,null);
}
void remove(int at,int n){
node* t=find(at,at+n+1);
t->setc(null,0); t->push_up();
splay(t,null);
}
void print(node* o){
if(o==null) return;
print(o->ch[0]);
printf("%c",o->v);
print(o->ch[1]);
}
void print(int at,int n){
node* t=find(at,at+n+1);
print(t->ch[0]);
printf("\n");
}
}tree;

int main(){
scanf("%d",&n);
while(n--){
scanf("%s",s);
if(s[0]=='I'){
cur=0;
scanf("%d",&x);
while(x--){
while(str[cur]=getchar(),str[cur]=='\n');
cur++;
}
cur--;
tree.insert(at,cur);
}
else if(s[0]=='M') scanf("%d",&at);
else if(s[0]=='D'){
scanf("%d",&x);
tree.remove(at,x);
}
else if(s[0]=='G'){
scanf("%d",&x);
tree.print(at,x);
}
else if(s[0]=='P') at--;
else at++;
}
return 0;
}
View Code  

 

 

 


 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: