比较基本的操作。

#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. 如何实现ZBrush中的Alt和Shift键的快速运用

    ZBrush是一个数字雕刻和绘画软件,它以强大的功能和直观的工作流程彻底改变了整个三维雕刻行业.在一个简洁的界面中,ZBrush®为当代数字艺术家提供了世界上最先进的工具.利用快捷键能使操作更快捷高效 ...

  2. c++实现排序(简单插入,希尔,选择,快速,冒泡,堆排)

    简单插入排序 适用于记录较少且基本有序的记录.算法思想:给定一个存在分界线的序列,分界线左边有序,右边无序,依次将右边的没排序的数与左边序列进行比较,插入相应位置,再对分界线做出相应调整,下面用图来说 ...

  3. Django项目中model增加了新字段怎样更新?

    Django是不直接支持syncdb更新数据库的字段的,必须重新建立. 或者改一个表名新建一个表... 刚刚想出来一招: 自己在表上面先加一个字段,然后再在model上面改,貌似是可以的.

  4. 贴图在Unity中的设置

    例如:一张512X512的图片占用的内存大小,计算方法:512*512*4/1024=1.024MB. 如果在贴图设置里设置成真彩色那就等于计算的值,设置成16位色彩,内存占用会减少. 在进行性能优化 ...

  5. String类详解(1)

    首先String是一个类. 1,实例化String类方法. 1)直接赋值:String name="haha"; 2)通过关键字:String name=new String(&q ...

  6. 通过输入卡号前10位数字判断是哪个银行的卡和类型(储蓄卡or信用卡)

    19位银行卡(包括储蓄卡和信用卡)可以通过前10位数字判断是哪个银行的卡和类型(储蓄卡or信用卡) 16位银行卡(包括储蓄卡和信用卡)可以通过前10位数字判断是哪个银行的卡和类型(储蓄卡or信用卡) ...

  7. .NET 知识

    1.读懂IL代码就这么简单 IL是.NET框架中中间语言(Intermediate Language)的缩写.使用.NET框架提供的编译器可以直接将源程序编译为.exe或.dll文件,但此时编译出来的 ...

  8. Java连接Elasticsearch集群

    package cn.test; import java.net.InetAddress; import java.net.UnknownHostException; import org.elast ...

  9. poj3984迷宫问题 广搜+最短路径+模拟队列

    转自:http://blog.csdn.net/no_retreats/article/details/8146585   定义一个二维数组: int maze[5][5] = { 0, 1, 0, ...

  10. C++ 排序、查找的应用

    // order.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "string.h" #includ ...