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 MBSubmit: 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
相关文章推荐
- Viewpager+Fragment 实现滑动切换显示内容
- android开发第一次心的
- 5.swift关键字和符号
- 数据类型包装类,String,对象的复制
- 欢迎使用CSDN-markdown编辑器
- ./configure详解
- JAVA学习笔记整理一(基础知识)
- Problem-D
- 18-Python网络编程
- 学生信息管理系统——指针和链表的应用
- 机器学习(一)------- 线性回归(Linear regression )
- SpringMVC注解@RequestParam全面解析---打酱油的日子
- 二分答案方法、
- 中国UTM分区
- 20160516
- 初识AngularJS
- 面试笔试题总结
- Pyqt 浏览器自动填充input框小demo
- MongoDB MapReduce 性能提升20倍的优化宝典
- 可查询最值的栈