比较基本的操作。

#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 99999999
#define ll __int64
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define key_value ch[ch[root][1]][0]
using namespace std;
const int MAXN = ;
int pre[MAXN],rev[MAXN],siz[MAXN],ch[MAXN][],s[MAXN],tot1,tot2,root;
int n;
char a[MAXN],key[MAXN];
void Treavel(int x)
{
if(x)
{
Treavel(ch[x][]);
printf("结点%2d:左儿子 %2d 右儿子 %2d 父结点 %2d size=%2d key=%2c rev=%2d pre=%2d\n",x,ch[x][],ch[x][],pre[x],siz[x],key[x],rev[x],pre[x]);
Treavel(ch[x][]);
}
}
void debug()
{
printf("root:%d\n",root);
Treavel(root);
}
void Newnode(int &rt,int pa,int k)
{
if(tot2){
rt = s[tot2--];
}
else {
rt = ++tot1;
}
pre[rt] = pa;
siz[rt] = ;
key[rt] = k;
rev[rt] = ;
ch[rt][] = ch[rt][] = ;
}
void pushup(int rt)
{
siz[rt] = siz[ch[rt][]] + siz[ch[rt][]] + ;
}
void updata_rev(int rt)
{
if(!rt)
return;
rev[rt] ^= ;
swap(ch[rt][],ch[rt][]);
}
void pushdown(int rt)
{
if(rev[rt]){
updata_rev(ch[rt][]);
updata_rev(ch[rt][]);
rev[rt] = ;
}
}
void build(int &rt,int l,int r,int pa)
{
if(l > r)
return ;
int m = (l+r)/;
Newnode(rt,pa,a[m]);
build(ch[rt][],l,m-,rt);
build(ch[rt][],m+,r,rt);
pushup(rt);
}
void Init()
{
root = tot1 = tot2 = ;
key[root] = pre[root] = rev[root] = siz[root] = ;
ch[root][] = ch[root][] = ;
Newnode(root,,);
Newnode(ch[root][],root,);
pushup(ch[root][]);
pushup(root);
}
void Rotate(int rt,int kind)
{
int y = pre[rt];
pushdown(y);
pushdown(rt);
ch[y][!kind] = ch[rt][kind];
pre[ch[rt][kind]] = y;
if(pre[y]){
ch[pre[y]][ch[pre[y]][]==y] = rt;
}
pre[rt] = pre[y];
ch[rt][kind] = y;
pre[y] = rt;
pushup(y);
}
void splay(int rt,int goal)
{
pushdown(rt);
while(pre[rt] != goal)
{
if(pre[pre[rt]] == goal){
pushdown(pre[rt]);
pushdown(rt);
Rotate(rt,ch[pre[rt]][]==rt);
}
else {
pushdown(pre[pre[rt]]);
pushdown(pre[rt]);
pushdown(rt);
int y = pre[rt];
int kind = ch[pre[y]][]==y;
if(ch[y][kind] == rt){
Rotate(rt,!kind);
Rotate(rt,kind);
}
else {
Rotate(y,kind);
Rotate(rt,kind);
}
}
}
if(goal == )
root = rt;
pushup(rt);
}
int Get_kth(int rt,int k)
{
pushdown(rt);
int t = siz[ch[rt][]] + ;
if(t == k){
return rt;
}
else if(t > k){
return Get_kth(ch[rt][],k);
}
else {
return Get_kth(ch[rt][],k-t);
}
}
int Get_next(int rt)
{
pushdown(rt);
int tmp = ch[rt][];
while(ch[tmp][]){
tmp = ch[tmp][];
pushdown(tmp);
}
return tmp;
}
int Get_pre(int rt)
{
pushdown(rt);
int tmp = ch[rt][];
while(ch[tmp][]){
tmp = ch[tmp][];
pushdown(tmp);
}
return tmp;
}
void erase(int rt)
{
if(!rt)
return;
s[++tot2] = rt;
erase(ch[rt][]);
erase(ch[rt][]);
}
int main()
{
int i,j;
while(~scanf("%d",&n))
{
Init();
char c[];
//debug();
while(n--)
{
scanf("%s",c);
if(c[] == 'I'){
int len;
scanf("%d",&len);
getchar();
gets(a);
splay(Get_next(root),root);
build(key_value,,len-,ch[root][]);
pushup(ch[root][]);
pushup(root);
//debug();
}
else if(c[] == 'D'){
int x;
scanf("%d",&x);
int ret = siz[ch[root][]] + ;
splay(Get_kth(root,ret+x+),root);
erase(key_value);
pre[key_value] = ;
key_value = ;
pushup(ch[root][]);
pushup(root);
//debug();
}
else if(c[] == 'R'){
int x;
scanf("%d",&x);
int ret = siz[ch[root][]] + ;
splay(Get_kth(root,ret+x+),root);
updata_rev(key_value);
pushup(ch[root][]);
pushup(root);
//debug();
}
else if(c[] == 'G'){
printf("%c\n",key[Get_next(root)]);
}
else if(c[] == 'M'){
int x;
scanf("%d",&x);
splay(Get_kth(root,x+),);
//debug();
}
else if(c[] == 'P'){
splay(Get_pre(root),);
//debug();
}
else {
splay(Get_next(root),);
//debug();
}
}
}
}

HYSBZ 1269文本编辑器 splay的更多相关文章

  1. BZOJ 1269 文本编辑器 Splay

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

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

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

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

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

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

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

  5. BZOJ 1269 文本编辑器editor(伸展树)

    题意 https://www.lydsy.com/JudgeOnline/problem.php?id=1269 思路 伸展树(\(\text{splay}\))功能比较齐全的模板,能较好的体现 \( ...

  6. [AHOI 2006][BZOJ 1269]文本编辑器editor

    好吧,我承认这是我用来刷随笔数的喵~ 这是一道 splay 裸题,但还是有想本傻 X 一样根本不会写 splay 的,于是乎又用 treap 水过了 splay 的常数我还是知道的,所以真是不知道那些 ...

  7. 【BZOJ】1269: [AHOI2006]文本编辑器editor(Splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1269 这题RE2次啊,好不爽啊,我一直以为是splay的问题,其实是数组开小了......(我老犯这 ...

  8. BZOJ 1269: [AHOI2006]文本编辑器editor( splay )

    splay..( BZOJ 1507 题目基本相同..双倍经验 ) ------------------------------------------------------------------ ...

  9. BZOJ 1269: [AHOI2006]文本编辑器editor (splay tree)

    1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1213  Solved: 454[Submit ...

随机推荐

  1. [cocos2dx]让CCScrollView支持分页

    [cocos2dx]让CCScrollView支持分页 做过IOS开发的朋友, 肯定知道UIScrollView有一个isPaged属性. 当设置其为true的时候, 滑动会自动分页. 即, 每次滑动 ...

  2. [转载]ExtJs4 笔记(9) Ext.Panel 面板控件、 Ext.window.Window 窗口控件、 Ext.container.Viewport 布局控件

    作者:李盼(Lipan)出处:[Lipan] (http://www.cnblogs.com/lipan/)版权声明:本文的版权归作者与博客园共有.转载时须注明本文的详细链接,否则作者将保留追究其法律 ...

  3. Remote Displayer for Android

    应用截图: 作者:sunrain_hjb      QQ:2157825357                 Email:sunrain_hjb@aliyun.com 版本:1.0.188     ...

  4. URAL 1416 Confidential --最小生成树与次小生成树

    题意:求一幅无向图的最小生成树与最小生成树,不存在输出-1 解法:用Kruskal求最小生成树,标记用过的边.求次小生成树时,依次枚举用过的边,将其去除后再求最小生成树,得出所有情况下的最小的生成树就 ...

  5. JS中的Navigator 对象

    Navigator 对象包含有关浏览器的信息. 很多时候我们需要在判断网页所处的浏览器和平台,Navigator为我们提供了便利 Navigator常见的对象属性如下: 属性 描述 appCodeNa ...

  6. Css 常用属性

    1. overflow:hidden和zoom:1 verflow:hidden;的作用 1. 隐藏溢出 :2.消除浮动 <style type="text/css"> ...

  7. Linux 网络编程一(TCP/IP协议)

    以前我们讲过进程间通信,通过进程间通信可以实现同一台计算机上不同的进程之间通信. 通过网络编程可以实现在网络中的各个计算机之间的通信. 进程能够使用套接字实现和其他进程或者其他计算机通信. 同样的套接 ...

  8. Enabling CORS in WCF

    Introduction This is an intermediate example of WCF as REST based solution and enabling CORS access, ...

  9. 那些年我们写过的T-SQL(下篇)(转)

    原文:http://www.cnblogs.com/wanliwang01/p/TSQL_Base04.html   下篇的内容很多都会在工作中用到,尤其是可编程对象,那些年我们写过的存储过程,有木有 ...

  10. linux实践——ELF分析

    一.ELF的部分结构定义 elf header(定义在/usr/include/elf.h)//64位的系统ELF文件头包括以下两个部分 #define EI_NIDENT (16) typedef ...