[BZOJ1269]文本编辑器editor
Problem
有n个操作

Solution
splay模板题,用splay维护下标。
Notice
需要把l的前一个位置旋转到根,r的后一个位置旋转到根的右节点。所以特别要注意0的大坑。
Code
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define sqz main
#define ll long long
#define reg register int
#define rep(i, a, b) for (reg i = a; i <= b; i++)
#define per(i, a, b) for (reg i = a; i >= b; i--)
#define travel(i, u) for (reg i = head[u]; i; i = edge[i].next)
const int INF = 1e9, N = 2200000;
const double eps = 1e-6, phi = acos(-1);
ll mod(ll a, ll b) {if (a >= b || a < 0) a %= b; if (a < 0) a += b; return a;}
ll read(){ ll x = 0; int zf = 1; char ch; while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
if (ch == '-') zf = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * zf;}
void write(ll y) { if (y < 0) putchar('-'), y = -y; if (y > 9) write(y / 10); putchar(y % 10 + '0');}
int point = 0, root, pre, suf, ans, pos = 2;
char st[N + 5];
struct node
{
char val[N + 5];
int count[N + 5], son[2][N + 5], parent[N + 5], tag[N + 5];
inline void up(int u)
{
count[u] = count[son[0][u]] + count[son[1][u]] + 1;
}
inline void New(int &u, char ch, int last)
{
u = ++point;
parent[u] = last;
val[u] = ch;
son[0][u] = son[1][u] = tag[u] = 0;
}
inline void change(int u)
{
if (u == 0) return;
tag[u] ^= 1;
swap(son[0][u], son[1][u]);
}
inline void down(int u)
{
if (u == 0 || tag[u] == 0) return;
change(son[0][u]), change(son[1][u]);
tag[u] = 0;
}
inline void build()
{
New(root, '~', 0);
New(son[1][root], '~', root);
up(root);
}
void Rotate(int x, int &rt)
{
int y = parent[x], z = parent[y];
int l = (son[1][y] == x), r = 1 - l;
if (y == rt) rt = x;
else if (son[0][z] == y) son[0][z] = x;
else son[1][z] = x;
parent[x] = z;
parent[son[r][x]] = y, son[l][y] = son[r][x];
parent[y] = x, son[r][x] = y;
up(y);
up(x);
}
void Splay(int x, int &rt)
{
while (x != rt)
{
int y = parent[x], z = parent[y];
if (y != rt)
{
if ((son[0][z] == y) ^ (son[0][y] == x))
Rotate(x, rt);
else Rotate(y, rt);
}
Rotate(x, rt);
}
}
int Find(int u, int x)
{
down(u);
if (x == count[son[0][u]] + 1) return u;
else if (x <= count[son[0][u]]) return Find(son[0][u], x);
else return Find(son[1][u], x - count[son[0][u]] - 1);
}
void Insert(int &u, int l, int r, int last)
{
int mid = (l + r) >> 1;
New(u, st[mid], last);
if (l < mid) Insert(son[0][u], l, mid - 1, u);
if (r > mid) Insert(son[1][u], mid + 1, r, u);
up(u);
}
void adjust(int l, int r)
{
int x = Find(root, l - 1), y = Find(root, r + 1);
Splay(x, root), Splay(y, son[1][root]);
}
void solve1()
{
int t = read();
adjust(pos, pos - 1);
gets(st);
Insert(son[0][son[1][root]], 0, t - 1, son[1][root]);
up(son[1][root]), up(root);
}
void solve2()
{
int t = read();
adjust(pos, pos + t - 1);
parent[son[0][son[1][root]]] = 0, son[0][son[1][root]] = 0;
up(son[1][root]), up(root);
}
void solve3()
{
int t = read();
adjust(pos, pos + t - 1);
change(son[0][son[1][root]]);
}
void solve4()
{
int t = Find(root, pos);
printf("%c\n", val[t]);
}
}Splay_tree;
int sqz()
{
Splay_tree.build();
int H_H = read();
char op[10];
while(H_H--)
{
scanf("%s", op);
if (op[0] == 'I') Splay_tree.solve1();
else if (op[0] == 'M') pos = read() + 2;
else if (op[0] == 'P') --pos;
else if (op[0] == 'N') ++pos;
else if (op[0] == 'D') Splay_tree.solve2();
else if (op[0] == 'R') Splay_tree.solve3();
else Splay_tree.solve4();
}
return 0;
}
[BZOJ1269]文本编辑器editor的更多相关文章
- [bzoj1269]文本编辑器editor [bzoj1500]维修数列
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2540 Solved: 923 [Submit ...
- 【BZOJ1269/1507】[AHOI2006]文本编辑器editor Splay
[BZOJ1269][AHOI2006]文本编辑器editor Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个简单而高效的文本编辑器.你能帮助他吗?为了明确任务目 ...
- 【bzoj1507】[NOI2003]Editor /【bzoj1269】[AHOI2006]文本编辑器editor Splay
[bzoj1507][NOI2003]Editor 题目描述 输入 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中 ...
- BZOJ1269 [AHOI2006]文本编辑器editor 【82行splay】
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MB Submit: 4633 Solved: 1782 [Sub ...
- 【BZOJ】【1269】【AHOI2006】文本编辑器editor
Splay Splay序列维护的模板题了……为了便于处理边界情况,我们可以先插入两个空格当作最左端和最右端,然后……其实本题主要考察的就是Build.splay和Findkth这三个操作,我们可以实现 ...
- AHOI2006文本编辑器editor
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1885 Solved: 683[Submit ...
- BZOJ 1269: [AHOI2006]文本编辑器editor( splay )
splay..( BZOJ 1507 题目基本相同..双倍经验 ) ------------------------------------------------------------------ ...
- BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor
BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor 题意: 分析: splay模拟即可 注意1507的读入格式 ...
- BZOJ 1269: [AHOI2006]文本编辑器editor (splay tree)
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1213 Solved: 454[Submit ...
随机推荐
- h5做直播的弹幕效果
最近在搞弹幕效果所以就写一个关于弹幕的吧,刚开始寻思去网上找插件的,但找的插件和我的需求都不太相符,其实做弹幕我知道的有两种方法: 1:一种是用canvas和对象来完成弹幕想过,用canvas来完成弹 ...
- 下一个更大的数 Next Greater Element
2018-09-24 21:52:38 一.Next Greater Element I 问题描述: 问题求解: 本题只需要将nums2中元素的下一个更大的数通过map保存下来,然后再遍历一遍nums ...
- 放弃 Tightvnc, 选择 Tigervnc
构建headless vnc server ,我终于放弃了Tightvnc 基于以下原因: 1) 已知的Qt5的键盘映射问题,导致virtualbox 的使用出现困难 https://unix.sta ...
- Jedis与Lua脚本结合
使用Lua脚本的好处 1.减少网络开销:可以将多个请求通过脚本的形式一次发送,减少网络时延和请求次数. 2.原子性的操作:Redis会将整个脚本作为一个整体执行,中间不会被其他命令插入.因此在编 ...
- 20180518VSTO多簿单表汇总外接程序按钮
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsof ...
- scrapy-redis(一)
安装scrapy-redis pip install scrapy-redis 从GitHub 上拷贝源码: clone github scrapy-redis源码文件 git clone https ...
- php中Redis的扩展
首先要下载 php_redis.dll 和 php_igbinary.dll 在官网(https://windows.php.net/downloads/pecl/snaps/redis/3.1.4/ ...
- Oracl 12c安装
Oracl安装部署 一.前置条件准备 修改hostname: hostname oracle 修改/etc/hosts:添加192.168.10.106 oracle 添加软件开发工具 搭建yum源 ...
- DRF之简介以及序列化操作
1. Web应用模式. 在开发Web应用中,有两种应用模式: 前后端不分离 2.前后端分离 2. api接口 为了在团队内部形成共识.防止个人习惯差异引起的混乱,我们需要找到一种大家都觉得很好的接口实 ...
- Hongcow Buys a Deck of Cards CodeForces - 744C (状压)
大意: n个红黑卡, 每天可以选择领取一块红币一块黑币, 或者买一张卡, 第$i$张卡的花费红币数$max(r_i-A,0)$, 花费黑币数$max(b_i-B,0)$, A为当前红卡数, B为当前黑 ...