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操作的字符串中 ...
随机推荐
- iOS百度地图探索
新建工程后,几项准备: 1.工程中一个文件设为.mm后缀 2.在Xcode的Project -> Edit Active Target -> Build -> Linking -&g ...
- [AngularJS 1] Introduction to AngularJS
introduction:this article is going to introduce AngularJS in generally. I will write it through five ...
- sass mapsource --->gulp
详细,请戳这里 1.安装插件 npm install --save-dev gulp-sass gulp-sourcemaps gulp-autoprefixer 如果安装错误,请用sudo 权限: ...
- .Net 乱序方法
前两天开发一个奇葩的功能,突然间想到了用打乱顺序的方式解决.记录代码如下: /// <summary> /// 把收集回来的列表打乱顺序之后返回 /// </summary> ...
- C# 清空sessin
Session.Abandon();//清除全部Session//清除某个SessionSession["UserName"] = null;Session.Remove(&quo ...
- sorcketlog
正在运行的API有bug,不能var_dump进行调试,因为会影响client的调用. 将日志写到文件,查看也不方便,特别是带调用栈或大数据结构的文件日志,查看日志十分困难. 这时候用SocketLo ...
- js控制div颜色
<html><head></head><style>#div1{width:400px;height:400px;background-color:re ...
- Java druid
1.ConnectionFactory (添加引用:druid-1.0.1.jar) package nankang.test; import java.sql.Connection; import ...
- IIS7 配置URL_REWRITE
webconfig 文件 system.webServer节点下配置rewrite 报错 是因为需要安装URL重写 需要安装: https://www.microsoft.com/zh-cn/down ...
- java编程的78条黄金法则
创建和销毁对象 1.考虑用静态工厂方法(返回类的实例的静态方法)代替构造器2.遇到多个构造器参数时要考虑用构造器3.用私有构造器或者枚举类型强化Singleton属性4.通过私有构造器强化不可实例化的 ...