bzoj 1269 [AHOI2006]文本编辑器editor
原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1269
伸展树的运用,如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using std::swap;
const int Max_N = ;
struct Node{
char chr;
int s;
bool rev;
Node *pre, *ch[];
inline void
set(char _chr = ' ', int _s = , Node *p = NULL){
chr = _chr, s = _s, rev = ;
pre = ch[] = ch[] = p;
}
inline void push_up(){
s = ch[]->s + ch[]->s + ;
}
inline void update(){
rev ^= ;
swap(ch[], ch[]);
}
inline void push_down(){
if (rev != ){
rev ^= ;
ch[]->update();
ch[]->update();
}
}
};
struct SplayTree{
char buf[Max_N];
Node *tail, *root, *null;
Node stack[Max_N], *store[Max_N];
int top, pos;
void init(){
top = , pos = ;
tail = &stack[];
null = tail++;
null->set();
root = newNode(' ');
root->ch[] = newNode(' ');
root->ch[]->pre = root;
root->ch[]->push_up();
root->push_up();
}
inline Node *newNode(char chr){
Node *p = null;
if (!top) p = tail++;
else p = store[--top];
p->set(chr, , null);
return p;
}
inline void rotate(Node *x, int c){
Node *y = x->pre;
y->push_down(), x->push_down();
y->ch[!c] = x->ch[c];
x->pre = y->pre;
if (x->ch[c] != null) x->ch[c]->pre = y;
if (y->pre != null) y->pre->ch[y->pre->ch[] != y] = x;
x->ch[c] = y;
y->pre = x;
y->push_up();
if (y == root) root = x;
}
inline void splay(Node *x, Node *f){
if (x == root) return;
for (; x->pre != f; x->push_down()){
if (x->pre->pre == f){
rotate(x, x->pre->ch[] == x);
} else {
Node *y = x->pre, *z = y->pre;
if (z->ch[] == y){
if (y->ch[] == x)
rotate(y, ), rotate(x, );
else rotate(x, ), rotate(x, );
} else {
if (y->ch[] == x)
rotate(y, ), rotate(x, );
else rotate(x, ), rotate(x, );
}
}
}
x->push_up();
}
inline Node *built(int l, int r){
if (l > r) return null;
int mid = (l + r) >> ;
Node *p = newNode(buf[mid]);
p->ch[] = built(l, mid - );
if (p->ch[] != null) p->ch[]->pre = p;
p->ch[] = built(mid + , r);
if (p->ch[] != null) p->ch[]->pre = p;
p->push_up();
return p;
}
inline void recycle(Node *x){
if (x != null){
recycle(x->ch[]);
store[top++] = x;
recycle(x->ch[]);
}
}
inline Node *select(Node *x, int k){
for (int t = ; x != null;){
x->push_down();
t = x->ch[]->s;
if (k == t + ) break;
else if (k <= t) x = x->ch[];
else k -= t + , x = x->ch[];
}
return x;
}
inline void get_range(int x, int y){
splay(select(root, x + ), null);
splay(select(root, y + ), root);
}
inline void Gets(char *s){
char c;
while (c = getchar(), c != '\n') *s++ = c;
*s = '\0';
}
inline void insert(int n){
char c;
scanf("%c", &c);
Gets(buf + );
get_range(pos, pos);
Node *ret = built(, n);
root->ch[]->ch[] = ret;
ret->pre = root->ch[];
root->ch[]->push_up();
root->push_up();
}
inline void del(int n){
get_range(pos, pos + n);
Node* &ret = root->ch[];
ret->ch[]->pre = null;
#ifdef LOCAL
recycle(ret->ch[]);
#endif
ret->ch[] = null;
ret->push_up();
root->push_up();
}
inline void rotate(int n){
get_range(pos, pos + n);
root->ch[]->ch[]->update();
}
inline void get(){
splay(select(root, pos + ), null);
printf("%c\n", root->chr);
}
inline void move(){
scanf("%d", &pos);
}
inline void prev(){
pos--;
}
inline void next(){
pos++;
}
}spt;
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
spt.init();
int m, n;
char str[];
scanf("%d", &m);
while (m--){
scanf("%s", str);
switch (str[]){
case 'M':
spt.move();
break;
case 'I':
scanf("%d", &n);
spt.insert(n);
break;
case 'D':
scanf("%d", &n);
spt.del(n);
break;
case 'N':
spt.next();
break;
case 'P':
spt.prev();
break;
case 'R':
scanf("%d", &n);
spt.rotate(n);
break;
case 'G':
spt.get();
break;
}
}
return ;
}
bzoj 1269 [AHOI2006]文本编辑器editor的更多相关文章
- BZOJ 1269: [AHOI2006]文本编辑器editor( splay )
splay..( BZOJ 1507 题目基本相同..双倍经验 ) ------------------------------------------------------------------ ...
- BZOJ 1269: [AHOI2006]文本编辑器editor (splay tree)
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1213 Solved: 454[Submit ...
- 【BZOJ】1269: [AHOI2006]文本编辑器editor(Splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1269 这题RE2次啊,好不爽啊,我一直以为是splay的问题,其实是数组开小了......(我老犯这 ...
- 1269: [AHOI2006]文本编辑器editor
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5269 Solved: 2037[Submit][Status][Discuss] Descript ...
- AHOI2006文本编辑器editor
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1885 Solved: 683[Submit ...
- BZOJ1269 [AHOI2006]文本编辑器editor 【82行splay】
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MB Submit: 4633 Solved: 1782 [Sub ...
- BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor
BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor 题意: 分析: splay模拟即可 注意1507的读入格式 ...
- 【BZOJ1269/1507】[AHOI2006]文本编辑器editor Splay
[BZOJ1269][AHOI2006]文本编辑器editor Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目 ...
- 【bzoj1507】[NOI2003]Editor /【bzoj1269】[AHOI2006]文本编辑器editor Splay
[bzoj1507][NOI2003]Editor 题目描述 输入 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中 ...
随机推荐
- 无法定位程序输入点ucrtbase.
转:http://tieba.baidu.com/p/3848709732 后的解决方法是需要安装VC 2015 Redistributable,请自己选择相应的版本.百度云盘共享地址:http:// ...
- Card objects
There are fifty-two cards in a deck, each of which belongs to one of four suits and one of thirteen ...
- 循环链表Josephus问题(c,cpp)
问题描述: 设有n个人围坐在一个圆桌周围,现从第s个人开始报数,数到第m个的人出列,然后从出列的下一个人重新开始报数,数到第m个的人又出列,.......,如此反复直到所有的人出列为止. Joseph ...
- break 和 continue
break 和 continue 相同点: 都 用在循环体内,如 switch.for.while.do while的程序块中,用于控制程序循环语句的执行 不同点: break可以离开当前switch ...
- cordova android ios
一 . cordova android 中js 调用JAVA 方法: 二 . cordova ios --->js 调用object (一); 三 .cordova ios --->OC ...
- C# 特性Attributes 和反射
一,Attributes 类新建一个子类,DetailAttributes 二, 在类的属性声明上面加Attributes public class testAttributes { [Detail( ...
- C#多线程案例基础
C#多线程案例基础(转) 在学习多线程之前,我们先来看几个概念: 1,什么是进程? 当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源,当然一个程序也可能开 ...
- angular service讲解
controller是相对独立的,也就是说,两个controller之间,内存是不共享的,这个controller是无法访问其他其他controller的属性或者方法的; 以前,我都是通过localS ...
- jquery控制元素的淡入淡出切换
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Java实现0~100之和
经典问题了,三个变量分别表示起始.结尾以及和,for循环从起始到结尾,和不断累积.代码如下: public class ForLoop { public static void main(String ...