BZOJ 1507 splay
写完维修数列 这不是水题嘛233333
//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=5000050;
int n,pos=1,ch[N][2],fa[N],size[N],root,xx,cnt;
char op[15],ins[N],v[N];
void push_up(int x){size[x]=size[ch[x][0]]+size[ch[x][1]]+1;}
void rotate(int p){
int q=fa[p],y=fa[q],f=(ch[q][1]==p);
ch[q][f]=ch[p][!f],fa[ch[q][f]]=q;
ch[p][!f]=q,fa[q]=p,fa[p]=y;
if(y)ch[y][ch[y][1]==q]=p;
push_up(q);
}
void splay(int x,int tp){
for(int y;y=fa[x];rotate(x)){
if(y==tp)break;
if(fa[y]!=tp){
if((ch[y][0]==x)^(ch[fa[y]][0]==y))rotate(x);
else rotate(y);
}
}push_up(x);
if(!tp)root=x;
}
int find(int x,int sz){
if(size[ch[x][0]]+1==sz)return x;
else if(size[ch[x][0]]>=sz)return find(ch[x][0],sz);
else return find(ch[x][1],sz-size[ch[x][0]]-1);
}
int build(int l,int r,int father){
if(l>r)return 0;
int pos=++cnt;
if(l==r){
size[pos]=1;
fa[pos]=father;
v[pos]=ins[l];
return pos;
}
int mid=(l+r)>>1;
ch[pos][0]=build(l,mid-1,pos);
fa[pos]=father;v[pos]=ins[mid];
ch[pos][1]=build(mid+1,r,pos);
push_up(pos);
return pos;
}
void check(int x){
if(ch[x][0])check(ch[x][0]);
printf("%c",v[x]);
if(ch[x][1])check(ch[x][1]);
}
int main(){
scanf("%d",&n);
root=1,size[1]=2,ch[1][0]=2,size[2]=1,fa[2]=1,cnt=2;
for(int i=1;i<=n;i++){
scanf("%s",op);
if(op[0]=='I'){
int fx=find(root,pos),fy=find(root,pos+1);
splay(fx,0),splay(fy,fx);
scanf("%d",&xx);
for(int j=1;j<=xx;j++)
while(scanf("%c",&ins[j]),ins[j]<32);
ch[fy][0]=build(1,xx,fy);
push_up(fy),push_up(fx);
// check(root);
}
else if(op[0]=='M')scanf("%d",&pos),pos++;
else if(op[0]=='P')pos--;
else if(op[0]=='N')pos++;
else if(op[0]=='D'){
scanf("%d",&xx);
xx=min(size[root],xx+pos+1);
int fx=find(root,pos),fy=find(root,xx);
splay(fx,0),splay(fy,fx);
ch[fy][0]=0,push_up(fy),push_up(fx);
}
else{
scanf("%d",&xx);
int fx=find(root,pos),fy=find(root,min(pos+xx+1,size[root]));
splay(fx,0),splay(fy,fx);
check(ch[fy][0]),puts("");
}
}
}
BZOJ 1507 splay的更多相关文章
- bzoj 1269 bzoj 1507 Splay处理文本信息
bzoj 1269 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1269 大致思路: 用splay维护整个文本信息,splay树的中序遍历即为 ...
- 【BZOJ 1507】【NOI 2003】&【Tyvj P2388】Editor 块状链表模板题
2016-06-18 当时关于块状链表的想法是错误的,之前维护的是一个动态的$\sqrt{n}$,所以常数巨大,今天才知道原因TwT,请不要参照这个程序为模板!!! 模板题水啊水~~~ 第一次写块状链 ...
- BZOJ 1507 NOI2003 Editor Splay
题目大意: 1.将光标移动到某一位置 2.在光标后插入一段字符串 3.删除光标后的一段字符 4.输出光标后的一段字符 5.光标-- 6.光标++ 和1269非常像的一道题,只是弱多了 几个问题须要注意 ...
- BZOJ 1507 Editor
Description Input 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中可能会插入一些回车符,请忽略掉它 ...
- bzoj 3506 && bzoj 1552 splay
查最小值,删除,翻转... 显然splay啊... #include<iostream> #include<cstdio> #include<algorithm> ...
- bzoj 1014 splay维护hash值
被后缀三人组虐了一下午,写道水题愉悦身心. 题很裸,求lcq时二分下答案就行了,写的不优美会被卡时. (写题时精神恍惚,不知不觉写了快两百行...竟然调都没调就A了...我还是继续看后缀自动机吧... ...
- bzoj 1503 splay
因为是整体加减,所以直接记录在外面. #include<iostream> #include<cstdio> #include<cstring> #include& ...
- bzoj 3224 splay模板题4
再刷水题我就废了... #include<iostream> #include<cstdio> #include<algorithm> #include<cs ...
- bzoj 3223 splay模板题3
水题...貌似理解splay怎么维护数列了... 每个点维护一个size,它的位置就是它的size,区间翻转的话可以打标记,find的时候push_down,交换左右子树. #include<i ...
随机推荐
- Java多线程中常见的几个问题
我们都知道,在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口. 1.进程和线程的区别是什么? 进程是执行着的应用程序,而线程是进程内部的一个执行序列. ...
- OpenCV : 基于切线方向的边缘增强算法
使用切线方法,对切线方向上的边缘进行强化: 参考连接:图像锐化和边缘检测 代码: //在种子点方向上寻找合适的梯度,用于寻找边缘 //对low_Gray, high_gray之间的点寻找边缘 void ...
- ROS:ubuntu-Ros使用OrbSLAM
一般无误的官方连接:https://github.com/raulmur/ORB_SLAM ubuntu16.04没有多少改变,还是使用kinetic老代替indigo Related Publica ...
- 工作中总结的经验之git篇
不要以为你会git,你要知道,git不是只有commit和push 由于系统分析与设计的期末Project需要团队合作开发,因此在这里想谈谈GitHub团队项目合作开发的流程: 项目创建 项目负责人在 ...
- redis得配置及使用
http://www.cnblogs.com/huskyking/p/6004772.html
- C#连接Oracle数据库的方法(System.Data.OracleClient、Oracle.DataAccess.Client也叫ODP.net、Oracle.ManagedDataAccess.dll)
官方下载地址(ODP.net)(中文):http://www.oracle.com/technetwork/cn/topics/dotnet/downloads/index.html 官方下载地址(O ...
- UIAutomator定位简介
UIAutomator元素定位是 Android 系统原生支持的定位方式,虽然与 xpath 类似,但比它更加好用,且支持元素全部属性定位.定位原理是通过android 自带的android uiau ...
- [Kernel参数]----/etc/sysctl.conf
修改sysctl方法 方法一:修改/proc下内核参数文件内容 直接修改内核参数ip_forward对应在/proc下的文件/proc/sys/net/ipv4/ip_forward.用下面命令查看i ...
- sass使用中出现的问题
问题一:ruby按照官方文档安装后更换gem源时,报错Error fetching https://gems.ruby-china.org/: bad response Not Found 404 ( ...
- Fleury算法 求欧拉回路
Fleury算法 #include <iostream> #include <cstdio> #include <cstring> #include <cma ...