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

输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作。其中: 为了使输入文件便于阅读,Insert操作的字符串中可能会插入一些回车符,请忽略掉它们(如果难以理解这句话,可以参考样例)。 除了回车符之外,输入文件的所有字符的ASCII码都在闭区间[32, 126]内。且行尾没有空格。 这里我们有如下假定:  MOVE操作不超过50000个,INSERT和DELETE操作的总个数不超过4000,PREV和NEXT操作的总个数不超过200000。  所有INSERT插入的字符数之和不超过2M(1M=1024*1024),正确的输出文件长度不超过3M字节。  DELETE操作和GET操作执行时光标后必然有足够的字符。MOVE、PREV、NEXT操作必然不会试图把光标移动到非法位置。  输入文件没有错误。 对C++选手的提示:经测试,最大的测试数据使用fstream进行输入有可能会比使用stdio慢约1秒。

Output

输出文件editor.out的每行依次对应输入文件中每条GET指令的输出。

Sample Input

15
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)的更多相关文章

  1. 【BZOJ】1251: 序列终结者(splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1251 不行..为什么写个splay老是犯逗,这次又是null的mx没有赋值-maxlongint.. ...

  2. 【BZOJ1056】[HAOI2008]排名系统(Splay)

    [BZOJ1056][HAOI2008]排名系统(Splay) 题面 BZOJ 洛谷 题解 \(Splay\)随便维护一下就好了,至于名字什么的,我懒得手写哈希表了,直接哈希之后拿\(map\)压. ...

  3. 【BZOJ3506】排序机械臂(Splay)

    [BZOJ3506]排序机械臂(Splay) 题面 神TMBZOJ没有题面,感谢SYC的题面 洛谷的题面也不错 题解 对于每次旋转的物体 显然可以预处理出来 现在只要模拟旋转操作就行了 至于在哪里放标 ...

  4. 【BZOJ】3670: [Noi2014]动物园(KMP)

    题目 传送门:QWQ 分析 像求next一样求num. 第二次求next时加上不超过一半的条件. 时间复杂度: $ \huge O ( n ) $ 代码 // luogu-judger-enable- ...

  5. 【BZOJ】1068: [SCOI2007]压缩(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1068 发现如果只设一维的话无法转移 那么我们开第二维,发现对于前i个来说,如果确定了M在哪里,第i个 ...

  6. 【BZOJ】3709: [PA2014]Bohater(贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3709 很水的题...但是由于脑洞小..漏想了一种情况.. 首先显然能补血的先杀.. 然后杀完后从补血 ...

  7. 【HNOI2004】宠物收养所(splay)

    题面 Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的 ...

  8. 【BZOJ】1024: [SCOI2009]生日快乐(dfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1024 果然现在弱到连搜索都不会了么..... 一直想二分...但是无论如何也推不出怎么划分... Q ...

  9. 【BZOJ】1823: [JSOI2010]满汉全席(2-sat)

    题目 传送门:QWQ 分析 2-sat模板(然而辣鸡如我还是调了好久) 代码 //bzoj 1823 2-sat #include <bits/stdc++.h> using namesp ...

随机推荐

  1. python 异步线程简单实现

    import threading def foo(): with open(r'./result.log','wb') as f: f.write('=some logs here ==') t = ...

  2. Python字符串与数字互转,数字格式化

    # -*- coding: gbk -*- import re #将数字格式化为带三位数逗号的字符串 def formatNumber(number): numStr='%d'%number form ...

  3. Python中请使用isinstance()判断变量类型

    一.isinstance() 在Python中可以使用type()与isinstance()这两个函数判断对象类型,而isinstance()函数的使用上比type更加方便. # coding=utf ...

  4. liunx下安装MYSQL时需要安装的相关软件的作用

    2013年11月16日 14:18:39 This installs the package for MySQL server (mysql-community-server) and also pa ...

  5. 将文件放到Android模拟器的SD卡

    1.打开DDMS页面2.打开File Explorer页,如果没有,在Window –> Show View –>File Explorer3.一般就在mnt –> sdcard中4 ...

  6. codeforces 475A.Bayan Bus 解题报告

    题目链接:http://codeforces.com/problemset/problem/475/A 题目意思:输入一个整数 k(0 ≤ k ≤ 34),表示participants的人数,需要在一 ...

  7. 字串变换(codevs 1099)

    题目描述 Description 已知有两个字串 A$, B$ 及一组字串变换的规则(至多6个规则): A1$ -> B1$ A2$ -> B2$ 规则的含义为:在 A$中的子串 A1$ ...

  8. Nginx开启Gzip压缩大幅提高页面加载速度

    [root@12 conf]# vi nginx.conf gzip on;#开启GZIP gzip_min_length 1k; #不压缩临界值,大于1K的才压缩,一般不用改 gzip_buffer ...

  9. 使用jQuery简单实现产品展示的图片左右滚动功能

    今天要做一个产品展示功能,由于产品比较多,一屏展示不完,所以想要做一个通过点击进行翻页的效果,在网上找了几个都不大好用,最后只能自己动手写了. 效果如下所示: 原理比较简单:将要滚动显示的区域的CSS ...

  10. DbHelper-SQL数据库访问助手

    using System; using System.Data; using System.Data.SqlClient; namespace Whir.Software.Framework.Ulti ...