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操作的字符串中 ...
随机推荐
- DB2命令大全
1.1查看表空间 db2 list tablespaces show detail 1.2查看数据库的表死锁 方法一: 打开监控 db2 update monitor switches using ...
- linux Apache和php配置
今天安装Apache httpd web服务器,安装过程分为三部分: (1)./configure (2)make (3)make install (需要root权限) 我的apache 安装在/ho ...
- SWFUpload使用指南
SWFUpload是一个flash和js相结合而成的文件上传插件,其功能非常强大. SWFUpload的特点: 1.用flash进行上传,页面无刷新,且可自定义Flash按钮的样式; 2.可以在浏览器 ...
- OSI(Open System Interconnection)网络模型
OSI模型是国际互连网标准化组织(International Standards Organizations ISO)所定义的,为了使网络的各个层次有标准.这个模型一般被称为“ISO OSI(Open ...
- 【IHttpHandler】ASP.NET 生命周期
对由 Microsoft® Internet 信息服务 (IIS) 处理的 Microsoft® ASP.NET 页面的每个请求都会被移交到 ASP.NET HTTP 管道.HTTP 管道由一系列托管 ...
- 利用JSONP进行水坑攻击
0x00 简介 前几天安全研究者Jaime Blasco发现了在中国某些特定主题的网站被进行了水坑攻击,攻击方法有一定多样性,其中存在一些比较少见于此类型攻击中的技术,不过其实是比较早的技术了,国内猥 ...
- 014安装Linux系统到开发板
SD卡----->开发板 1.安装准备: 硬件连接 USB下载线,一端连到开发板,另一端连到PC机: 串口线连好: 电源线连好: 设置开发板从SD卡启动: 2.打开开发板进入选单界面: 进入选单 ...
- Android IOS WebRTC 音视频开发总结(五一)-- 降噪基本原理
文章主要介绍噪声消除,文章来自博客园RTC.Blacker,支持原创,转载必须说明出处,欢迎关注微信公众号blacker,更多详见www.rtc.help ---------------------- ...
- WAMP搭建
win2003Apache+PHP+Mysql+PHPmyadmin 软件版本: Apacheèhttpd-2.2.25-win32-x86-no_ssl.msi php-5.4.3-Win32-VC ...
- 批量关闭 WordPress 的 Pingback 和 Trackback 功能
方法很简单,WordPress 后台即可实现,在设置-讨论中把"接收来自外部博客的引用通告(Pingback 和 Trackback)"这一项勾选去掉,保存设置.这样,以后新增的文 ...