题目链接

\(Splay\)先练到这吧(好像还有道毒瘤的维护数列诶,算了吧)

记录下光标的编号,维护就是\(Splay\)基操了。

另外数据有坑,数据是\(Windows\)下生成了,回车是'\n\r',我就被坑了。

#include <cstdio>
#include <algorithm>
using namespace std;
inline int read(){
int s = 0, w = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-')w = -1;ch = getchar();}
while(ch >= '0' && ch <= '9') s = s * 10 + ch - '0',ch = getchar();
return s * w;
}
const int MAXINSERTLEN = 10000010;
struct SplayTree{
char val;
int ch[2], fa, size;
}t[MAXINSERTLEN];
char str[MAXINSERTLEN];
int num, root, n, len;
void pushup(int u){
t[u].size = t[t[u].ch[0]].size + t[t[u].ch[1]].size + 1;
}
void rotate(int x){
int y = t[x].fa; int z = t[y].fa; int k = t[y].ch[1] == x;
t[z].ch[t[z].ch[1] == y] = x; t[x].fa = z;
t[y].ch[k] = t[x].ch[k ^ 1]; t[t[x].ch[k ^ 1]].fa = y;
t[x].ch[k ^ 1] = y; t[y].fa = x;
pushup(y); pushup(x);
}
void Splay(int x, int goal){
while(t[x].fa != goal){
int y = t[x].fa; int z = t[y].fa;
if(z != goal)
(t[z].ch[0] == y) ^ (t[y].ch[0] == x) ? rotate(x) : rotate(y);
rotate(x);
}
if(goal == 0) root = x;
}
int build(int l, int r){
int id = ++num;
int mid = (l + r) >> 1;
t[id].val = str[mid];
if(mid > l){
t[id].ch[0] = build(l, mid - 1);
t[t[id].ch[0]].fa = id;
}
if(mid < r){
t[id].ch[1] = build(mid + 1, r);
t[t[id].ch[1]].fa = id;
}
pushup(id);
return id;
}
inline int findKth(int k){
int u = root;
while(1){
if(t[t[u].ch[0]].size >= k) u = t[u].ch[0];
else if(t[t[u].ch[0]].size == k - 1) return u;
else k -= t[t[u].ch[0]].size + 1, u = t[u].ch[1];
}
}
inline int next(int x, int mode){
Splay(x, 0);
int u = t[x].ch[mode]; mode ^= 1;
while(t[u].ch[mode]) u = t[u].ch[mode];
return u;
}
char opt;
int cur;
void print(int x){
if(t[x].ch[0]) print(t[x].ch[0]);
if(t[x].val != 1) printf("%c", t[x].val);
if(t[x].ch[1]) print(t[x].ch[1]);
}
int main(){
n = read();
root = cur = ++num; t[num].val = 1;
t[++num].fa = cur; t[num].val = 1;
t[cur].ch[1] = num; t[num].size = 1; t[cur].size = 2;
for(int i = 1; i <= n; ++i){
opt = getchar();
while(opt < 'A' || opt > 'Z') opt = getchar();
if(opt == 'I'){
len = read();
for(int i = 1; i <= len; ++i){
str[i] = getchar();
while(str[i] < 32 || str[i] > 126) str[i] = getchar();
}
Splay(cur, 0);
Splay(next(cur, 1), cur);
t[t[root].ch[1]].ch[0] = (len = build(1, len));
t[len].fa = t[root].ch[1];
Splay(t[t[root].ch[1]].ch[0], 0);
}
else if(opt == 'G'){
len = read();
Splay(cur, 0);
Splay(findKth(t[t[root].ch[0]].size + 2 + len), root);
print(t[t[root].ch[1]].ch[0]);
putchar('\n');
}
else if(opt == 'M')
cur = findKth(read() + 1);
else if(opt == 'P')
cur = next(cur, 0);
else if(opt == 'N')
cur = next(cur, 1);
else if(opt == 'D'){
len = read();
Splay(cur, 0);
Splay(findKth(t[t[root].ch[0]].size + 2 + len), root);
t[t[root].ch[1]].ch[0] = 0;
}
}
return 0;
}

【洛谷 P4008】 [NOI2003]文本编辑器 (Splay)的更多相关文章

  1. 洛谷 P4008 [NOI2003]文本编辑器 解题报告

    P4008 [NOI2003]文本编辑器 题目描述 很久很久以前,\(DOS3.x\)的程序员们开始对 \(EDLIN\) 感到厌倦.于是,人们开始纷纷改用自己写的文本编辑器⋯⋯ 多年之后,出于偶然的 ...

  2. 洛谷 P4008 [NOI2003]文本编辑器

    先推广一下 求赞 我们考虑这样的一个问题 给你一个序列,要求你支持插入,删除,查询单点值 如果用数组,查询O(1),插入删除最坏O(n) 如果用链表,插入删除O(1),查询最坏O(n) 如果用平衡树- ...

  3. luogu P4008 [NOI2003]文本编辑器 splay 块状链表

    LINK:文本编辑器 这个东西感觉块状链表写细节挺多 (块状链表本来就难写 解释一下块状链表的做法:其实是一个个数组块 然后利用链表给链接起来 每个块的大小为sqrt(n). 这样插入删除的时候直接暴 ...

  4. [NOI2003] 文本编辑器 (splay)

    复制炸格式了,就不贴题面了 [NOI2003] 文本编辑器 Solution 对于光标的移动,我们只要记录一下现在在哪里就可以了 Insert操作:手动维护中序遍历结果,即每次取中点像线段树一样一样递 ...

  5. P4008 [NOI2003]文本编辑器

    思路 FHQ Treap的板子 用FHQ Treap维护中序遍历序列即可 然后数组开够! 代码 #include <cstdio> #include <cstring> #in ...

  6. [洛谷日报第62期]Splay简易教程 (转载)

    本文发布于洛谷日报,特约作者:tiger0132 原地址 分割线下为copy的内容 [洛谷日报第62期]Splay简易教程 洛谷科技 18-10-0223:31 简介 二叉排序树(Binary Sor ...

  7. [NOI2003]文本编辑器 [Fhq Treap]

    [NOI2003]文本编辑器 没啥好说的 就是个板子 #include <bits/stdc++.h> // #define int long long #define rep(a , b ...

  8. [AHOI2006]文本编辑器 Splay tree区间操作

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1269 Description 这些日子,可可不和卡卡一起玩了,原来可可正废寝忘食的想做一个 ...

  9. BZOJ 1269 文本编辑器 Splay

    题目大意:维护一个文本编辑器,支持下列操作: 1.将光标移动到某一位置 2.在光标后插入一段字符串 3.删除光标后的一段字符 4.翻转光标后的一段字符 5.输出光标后的一个字符 6.光标-- 7.光标 ...

  10. 洛谷.2042.[NOI2005]维护数列(Splay)

    题目链接 2017.12.24 第一次写: 时间: 2316ms (1268ms) 空间: 19.42MB (19.5MB)(O2) 注:洛谷测的时间浮动比较大 /* 插入一段数:将这些数先单独建一棵 ...

随机推荐

  1. 【OSG】运行OSG示例出现的奶牛不完整问题

    发现一个很奇怪的问题:我用笔记本运行OSG里面的示例,出现的图案总是不完整显示的,以经典的奶牛图案为例,如图. 图一是我电脑上的情况,正常情况应该是图二.不知道这是什么原因,难道是我电脑显卡的原因吗? ...

  2. Python的time,datetime,string相互转换

    #把datetime转成字符串 def datetime_toString(dt): return dt.strftime("%Y-%m-%d-%H") #把字符串转成dateti ...

  3. PHP查询网站

    1.w3school http://www.w3school.com.cn/php/ 2.PHP官网 http://php.net/manual/en/funcref.php 3.国内的类似w3csh ...

  4. 第26天:js-$id函数、焦点事件

    一.函数return语句定义函数的返回值,在函数内部用return来设置返回值,一个函数只能有一个返回值.同时,终止代码的执行.所有自定义函数默认没有返回值return后面不要换行 var a=10, ...

  5. BZOJ 1406 密码箱(数论)

    很简洁的题目.求出x^2%n=1的所有x<=n的值. n<=2e9. 直接枚举x一定是超时的. 看看能不能化成有性质的式子. 有 (x+1)(x-1)%n==0,设n=a*b,那么一定有x ...

  6. BZOJ 1179 Atm(强连通分量缩点+DP)

    题目说可以通过一条边多次,且点权是非负的,所以如果走到图中的一个强连通分量,那么一定可以拿完这个强连通分量上的money. 所以缩点已经很明显了.缩完点之后图就是一个DAG,对于DAG可以用DP来求出 ...

  7. hdu 1142 A Walk Through the Forest (最短路径)

    A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Jav ...

  8. VS2017常用快快捷键

    VS2017常用快快捷键 VS中代码对齐等快捷键        在VS中,选定代码后,按Ctrl+K+F组合键,可以自动进行代码对齐.        注意:要先按下Ctrl和K,再按下F,因为Ctrl ...

  9. 虚拟机如何设置U盘启动项

    开始配置虚拟机时选"自定义"不要选"典型",在“SCSI设配器”选LSI logic ,(不是默认的那个):然后其他正常默认创建,虚拟机建好后,再添加U盘为虚拟 ...

  10. [洛谷P5169]xtq的异或和

    题目大意:给你一张$n(n\leqslant10^5)$个点$m(m\leqslant3\times10^5)$条边的无向图,每条边有一个权值,$q(q\leqslant2^{18})$次询问,每次询 ...