【BZOJ】1507: [NOI2003]Editor(Splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1507

当练splay模板了,发现wjmzbmr的splay写得异常简介,学习了。orzzzzzzzzzzz!!!!!!
这个版本很好写的,比数组的好写多了。但是异常的慢啊T_T
这个版本的splay,会修改null的fa,但不影响结果,这点要记住。
#include <string>
#include <cstdio> using namespace std;
char strI[1024*1024+100]; struct Splay {
struct node{
node *ch[2], *fa;
char key;
int size;
node() { ch[0]=ch[1]=fa=NULL; size=key=0; }
void pushup() { size=ch[0]->size+ch[1]->size+1; }
bool d() { return this==fa->ch[1]; }
void setc(node *c, int _d) {
ch[_d]=c; c->fa=this;
}
}*null, *root;
Splay() {
null=new node;
root=null;
root=newnode(0);
node *t=newnode(0);
root->setc(t, 1);
root->pushup();
}
node* newnode(char key) {
node *ret=new node;
ret->ch[0]=ret->ch[1]=ret->fa=null;
ret->key=key; ret->size=1;
return ret;
}
void rot(node *r) {
node *fa=r->fa; bool d=r->d();
fa->fa->setc(r, fa->d());
fa->setc(r->ch[!d], d);
r->setc(fa, !d);
fa->pushup();
if(fa==root) root=r;
}
void splay(node *r, node *fa) {
while(r->fa!=fa) {
if(r->fa->fa==fa) rot(r);
else r->d()==r->fa->d()?(rot(r->fa), rot(r)):(rot(r), rot(r));
}
r->pushup();
}
node* sel(int k) {
int s;
for(node *t=root; ;) {
s=t->ch[0]->size;
if(s==k) return t;
t=t->ch[k>s];
if(k>s) k-=s+1;
}
}
node* getrange(int l, int r) {
node *left=sel(l); splay(left, null);
node *right=sel(r); splay(right, left);
return right;
}
void insert(int at, int cur) {
node *t=getrange(at, at+1);
t->setc(build(0, cur), 0); t->pushup();
splay(t, null);
}
void remove(int at, int n) {
node *t=getrange(at, at+n+1);
t->setc(null, 0); t->pushup();
splay(t, null);
}
node* build(int l, int r) {
if(l>=r) return null;
int m=(l+r)>>1;
node *t=newnode(strI[m]);
t->setc(build(l, m), 0);
t->setc(build(m+1, r), 1);
t->pushup();
return t;
}
void print(node *r) {
if(r==null) return;
print(r->ch[0]);
printf("%c", r->key);
print(r->ch[1]);
}
void print(int at, int n) {
node *t=getrange(at, at+n+1);
print(t->ch[0]);
printf("\n");
}
}splay; char s[10]; int main() {
int n, t, cur, at=0;
scanf("%d", &n);
while(n--) {
scanf("%s", s);
if(s[0]=='I') {
scanf("%d", &t);
cur=0;
while(t--) {
while(strI[cur]=getchar(), strI[cur]=='\n');
cur++;
}
splay.insert(at, cur);
}
else if(s[0]=='M') {
scanf("%d", &at);
}
else if(s[0]=='D') {
scanf("%d", &t);
splay.remove(at, t);
}
else if(s[0]=='G') {
scanf("%d", &t);
splay.print(at, t);
}
else if(s[0]=='P') --at;
else if(s[0]=='N') ++at;
}
return 0;
}
Description

Input
Output
Sample Input
Insert 26
abcdefghijklmnop
qrstuv wxy
Move 16
Delete 11
Move 5
Insert 1
^
Next
Insert 1
_
Next
Next
Insert 4
.\/.
Get 4
Prev
Insert 1
^
Move 0
Get 22
Sample Output
abcde^_^f.\/.ghijklmno
HINT
Source
【BZOJ】1507: [NOI2003]Editor(Splay)的更多相关文章
- 【BZOJ】1251: 序列终结者(splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1251 不行..为什么写个splay老是犯逗,这次又是null的mx没有赋值-maxlongint.. ...
- 【BZOJ1056】[HAOI2008]排名系统(Splay)
[BZOJ1056][HAOI2008]排名系统(Splay) 题面 BZOJ 洛谷 题解 \(Splay\)随便维护一下就好了,至于名字什么的,我懒得手写哈希表了,直接哈希之后拿\(map\)压. ...
- 【BZOJ3506】排序机械臂(Splay)
[BZOJ3506]排序机械臂(Splay) 题面 神TMBZOJ没有题面,感谢SYC的题面 洛谷的题面也不错 题解 对于每次旋转的物体 显然可以预处理出来 现在只要模拟旋转操作就行了 至于在哪里放标 ...
- 【BZOJ】3670: [Noi2014]动物园(KMP)
题目 传送门:QWQ 分析 像求next一样求num. 第二次求next时加上不超过一半的条件. 时间复杂度: $ \huge O ( n ) $ 代码 // luogu-judger-enable- ...
- 【BZOJ】1068: [SCOI2007]压缩(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1068 发现如果只设一维的话无法转移 那么我们开第二维,发现对于前i个来说,如果确定了M在哪里,第i个 ...
- 【BZOJ】3709: [PA2014]Bohater(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=3709 很水的题...但是由于脑洞小..漏想了一种情况.. 首先显然能补血的先杀.. 然后杀完后从补血 ...
- 【HNOI2004】宠物收养所(splay)
题面 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的 ...
- 【BZOJ】1024: [SCOI2009]生日快乐(dfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1024 果然现在弱到连搜索都不会了么..... 一直想二分...但是无论如何也推不出怎么划分... Q ...
- 【BZOJ】1823: [JSOI2010]满汉全席(2-sat)
题目 传送门:QWQ 分析 2-sat模板(然而辣鸡如我还是调了好久) 代码 //bzoj 1823 2-sat #include <bits/stdc++.h> using namesp ...
随机推荐
- HDOJ 1874
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- Linux下PS1、PS2、PS3、PS4使用详解
参考印象笔记:
- 每天一个命令day1【diff 命令】(具体实例看下一节)
diff 命令是 linux上非常重要的工具,用于比较文件的内容,特别是比较两个版本不同的文件以找到改动的地方.diff在命令行中打印每一个行的改动.最新版本的diff还支持二进制文件.diff程序的 ...
- 【OpenStack】OpenStack系列5之Cinder详解
源码下载安装 git clone -b stable/icehouse https://github.com/openstack/cinder.git pip install -r requireme ...
- Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- tcp/ip程序
#include<stdio.h> #include<stdlib.h> #include<errno.h> #include<string.h> #i ...
- MST:Roadblocks(POJ 3255)
路上的石头 题目大意:某个街区有R条路,N个路口,道路双向,问你从开始(1)到N路口的次短路经长度,同一条边可以经过多次. 这一题相当有意思,现在不是要你找最短路径,而是要你找次短路经,而且次短 ...
- codeforces 467C.George and Job 解题报告
题目链接:http://codeforces.com/problemset/problem/467/C 题目意思:给出一条含有 n 个数的序列,需要从中找出 k 对,每对长度为 m 的子序列,使得 找 ...
- (五)转载:通用定时器PWM输出
1. TIMER输出PWM基本概念 脉冲宽度调制(PWM),是英文“Pulse Width Modulation”的缩写,简称脉宽调制,是利用微处理器的数字输出来对模拟电路进行控制的一种非常有 ...
- LinuxC语言读取文件,分割字符串,存入链表,放入另一个文件
//file_op.c #include <string.h> #include <stdio.h> #include <stdlib.h> struct info ...