比较基本的操作。

#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. jemter的使用(二)

    在上篇文章中介绍了如何在jmeter中添加请求,并执行查看结果,下面介绍一下,在运行时需要用到的一些管理器 一.HTTP信息头管理器 1.添加信息头管理器 2.添加变量和值,如:Content-Typ ...

  2. 【Android 我的博客APP】1.抓取博客首页文章列表内容——网页数据抓取

    打算做个自己在博客园的博客APP,首先要能访问首页获取数据获取首页的文章列表,第一步抓取博客首页文章列表内容的功能已实现,在小米2S上的效果图如下: 思路是:通过编写的工具类访问网页,获取页面源代码, ...

  3. 【温故而知新-CSS】使用CSS设计网站导航栏

    body #nav li a { width: auto; } #nav li a:hover { background-color: #ffcc00; color: #fff; border-rig ...

  4. 最小生成树 kruskal hdu 5723 Abandoned country

    题目链接:hdu 5723 Abandoned country 题目大意:N个点,M条边:先构成一棵最小生成树,然后这个最小生成树上求任意两点之间的路径长度和,并求期望 /************** ...

  5. 计算机网络: IP地址,子网掩码,默认网关,DNS服务器详解

    楔子: 以Windows系统中IP地址设置界面为参考(如图1), IP地址, 子网掩码, 默认网关 和 DNS服务器, 这些都是什么意思呢? 学习IP地址的相关知识时还会遇到网络地址,广播地址,子网等 ...

  6. UVALive 6264 Conservation --拓扑排序

    题意:一个展览有n个步骤,告诉你每一步在那个场馆举行,总共2个场馆,跨越场馆需要1单位时间,先给你一些约束关系,比如步骤a要在b前执行,问最少的转移时间是多少. 解法:根据这些约束关系可以建立有向边, ...

  7. [转] 国外程序员整理的 C++ 资源大全

    关于 C++ 框架.库和资源的一些汇总列表,由 fffaraz 发起和维护. 内容包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++标准库,包括了S ...

  8. where,having与 group by连用的区别

    select 列a,聚合函数 from 表名 where 过滤条件 group by 列a having 过滤条件 group by 字句也和where条件语句结合在一起使用.当结合在一起时,wher ...

  9. Jsp页显示时间标签JSTL标签 <fmt:formatDate/> 实例大全

    <fmt:formatDate value="${isoDate}" type="both"/>2004-5-31 23:59:59 <fmt ...

  10. “PMS-基础权限管理系统”实施某谱OA系统经验总结

    “PMS-基础权限管理系统”介绍 "PMS-基础权限管理系统"是我一直想做的一个产品,融合多年开发及维护管理系统的经验,参考了很多系统,精心研制而成. 可以做为毕业设计参考,新手学 ...