题目大意:维护一个文本编辑器,支持下列操作:

1.将光标移动到某一位置

2.在光标后插入一段字符串

3.删除光标后的一段字符

4.翻转光标后的一段字符

5.输出光标后的一个字符

6.光标--

7.光标++

Splay中比較水的一道题,标记仅仅有区间翻转,也不用维护区间总值,只有须要注意的就是插入的时候fa要记得赋值,不然就会像本蒟蒻一样调半天,,,

这题要注意的是Insert操作的读入 首先读入第一个不是'\n'或者'\r'的字符,然后假设长度不为1就继续gets() 记住是get()不是scanf

然后就没啥了。。。 20%达成 啊啊爽翻天

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct abcd{
abcd *fa,*ls,*rs;
char c;
int siz;
bool rev_mark;
abcd (char C);
void Reverse();
void Push_Up();
void Push_Down();
}*null=new abcd(0),*root=null;
abcd :: abcd(char C)
{
fa=ls=rs=null;
c=C;
siz=C?1:0;
rev_mark=0;
}
void abcd :: Reverse()
{
rev_mark^=1;
swap(ls,rs);
}
void abcd :: Push_Up()
{
siz=ls->siz+rs->siz+1;
}
void abcd :: Push_Down()
{
if(rev_mark)
{
ls->Reverse();
rs->Reverse();
rev_mark=0;
}
}
void Zig(abcd *x)
{
abcd *y=x->fa;
y->ls=x->rs;
x->rs->fa=y;
x->rs=y;
x->fa=y->fa;
if(y==y->fa->ls)
y->fa->ls=x;
else if(y==y->fa->rs)
y->fa->rs=x;
y->fa=x;
y->Push_Up();
if(y==root)
root=x;
}
void Zag(abcd *x)
{
abcd *y=x->fa;
y->rs=x->ls;
x->ls->fa=y;
x->ls=y;
x->fa=y->fa;
if(y==y->fa->ls)
y->fa->ls=x;
else if(y==y->fa->rs)
y->fa->rs=x;
y->fa=x;
y->Push_Up();
if(y==root)
root=x;
}
void Splay(abcd *x,abcd *Tar)
{
while(1)
{
abcd *y=x->fa,*z=y->fa;
if(y==Tar)
break ;
if(z==Tar)
{
if(x==y->ls)
Zig(x);
else
Zag(x);
break;
}
if(x==y->ls)
{
if(y==z->ls)
Zig(y);
Zig(x);
}
else
{
if(y==z->rs)
Zag(y);
Zag(x);
}
}
x->Push_Up();
}
void Find(abcd *x,int y,abcd *z)
{
while(1)
{
x->Push_Down();
if(y<=x->ls->siz)
x=x->ls;
else
{
y-=x->ls->siz;
if(y==1)
break;
y--;
x=x->rs;
}
}
Splay(x,z);
}
char s[1<<21];
void Build_Tree(abcd *&x,int l,int r)
{
if(l>r)
return ;
int mid=l+r>>1;
x=new abcd(s[mid]);
Build_Tree(x->ls,l,mid-1);
Build_Tree(x->rs,mid+1,r);
if(x->ls!=null)
x->ls->fa=x;
if(x->rs!=null)
x->rs->fa=x;
x->Push_Up();
}
int cursor,m;
int main()
{
int i,x;
char p[100];
cin>>m;
{
root=new abcd('\n');
root->rs=new abcd('\n');
root->rs->fa=root;
root->Push_Up();
}
for(i=1;i<=m;i++)
{
scanf("%s",p);
if(p[0]=='M')
scanf("%d",&cursor);
else if(p[0]=='I')
{
scanf("%d",&x);
do s[0]=getchar(); while(s[0]=='\n'||s[0]=='\r');
if(x^1) gets(s+1);
Find(root,cursor+1,null);
Find(root,cursor+2,root);
Build_Tree(root->rs->ls,0,x-1);
root->rs->ls->fa=root->rs;
root->rs->Push_Up();
root->Push_Up();
}
else if(p[0]=='D')
{
scanf("%d",&x);
Find(root,cursor+1,null);
Find(root,cursor+x+2,root);
root->rs->ls=null;
root->rs->Push_Up();
root->Push_Up();
}
else if(p[0]=='R')
{
scanf("%d",&x);
Find(root,cursor+1,null);
Find(root,cursor+x+2,root);
root->rs->ls->Reverse();
}
else if(p[0]=='G')
{
Find(root,cursor+2,null);
printf("%c\n",root->c);
}
else if(p[0]=='P')
cursor--;
else
cursor++;
}
}

BZOJ 1269 文本编辑器 Splay的更多相关文章

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

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

  2. HYSBZ 1269文本编辑器 splay

    比较基本的操作. #include<map> #include<queue> #include<stack> #include<cmath> #incl ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. ExtJS拖拽效果

    ExtJS拖拽效果 <html> <head> <title>hello</title> <meta http-equiv="conte ...

  2. MSYS2 环境搭建(在Qt Creator可以设置环境变量来进行引用这些库)

    本机环境:Windows XP 32位MSYS2地址:http://sourceforge.net/projects/msys2/ 下载32位版本,地址:http://sourceforge.net/ ...

  3. 经典的C++库【转帖】

    源地址:http://www.deuxmille.org/archives/1472 基础类1. Dinkumware C++ Library 参考站点:http://www.dinkumware.c ...

  4. ISO/OSI网络体系结构和TCP/IP协议模型

    1. ISO/OSI的参考模型共有7层,由低层至高层分别为:物理层.数据链路层.网络层.传输层.会话层.表示层.     应用层.各层功能分别为: (1)物理层          提供建立.维护和拆除 ...

  5. VC生成的DLL给QT的EXE调用时lib路径问题小结

    VC生成的DLL给QT调用,有两种方式,一种是隐式调用调用(使用.lib文件方式): ① 在*.pro工程文件中添加VC生成的lib文件路径时,或者使用一个绝对路径,如: LIBS += " ...

  6. shodan

    https://www.shodan.io/ from:http://www.exploit-db.com/wp-content/themes/exploit/docs/33859.pdf 0x00 ...

  7. c#超时锁定

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. poj 1872 A Dicey Problem WA的代码,望各位指教!!!

    A Dicey Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 832   Accepted: 278 Des ...

  9. Hadoop Hive与Hbase关系 整合

    用hbase做数据库,但因为hbase没有类sql查询方式,所以操作和计算数据很不方便,于是整合hive,让hive支撑在hbase数据库层面 的 hql查询.hive也即 做数据仓库 1. 基于Ha ...

  10. 基于Cocos2dx开发卡牌游戏Demo_放开那三国 2.0

    PS:下载地址在最以下 1.登录 2.副本选择 3.地图 4. 选择敌人 5. 战斗 6. 战斗结算 7. 地图拓展 8. 武将拓展 9. 下载地址: 点击打开链接