好吧,我承认这是我用来刷随笔数的喵~

这是一道 splay 裸题,但还是有想本傻 X 一样根本不会写 splay 的,于是乎又用 treap 水过了

splay 的常数我还是知道的,所以真是不知道那些 500ms 的人都是怎么跑出来的,跪求大爷指导

近期要学一下可持久化的 AVL 和尝试把 treap 的代码非递归化模板化(话说我真的不知道原始的 treap 怎么写了)

写完后也会发出来的喵~

 #include <cstdio>
#include <cstdlib>
#include <cstring>
const int size=;
const int inf=0x7FFFFFFF; template <class type>
inline void swap(type & a, type & b)
{type t=a; a=b; b=t;} namespace treap
{
struct node
{
char ch; int size;
int weight;
bool rev;
node * left, * right;
inline node():size() {left=right=this;}
inline void pushdown() {if (rev) swap(left, right), left->rev^=, right->rev^=, rev=;}
inline void maintain() {size=left->size+right->size+;}
};
node * null=new node();
node MEM[size], * PORT=MEM; inline node * newnode(char ch, int size)
{
node * ret=PORT++;
ret->ch=ch; ret->size=size;
ret->rev=;
ret->left=ret->right=null;
return ret;
}
node * newtreap(char * s, int l, int w)
{
if (!l) return null;
node * ret=newnode(s[l>>], l);
ret->weight=rand()%w+;
if (l==) return ret;
ret->left=newtreap(s, l>>, ret->weight);
ret->right=newtreap(s+(l>>)+, l-(l>>)-, ret->weight);
return ret;
}
void split(node * t, int k, node *& l, node *& r)
{
if (t==null) l=r=null;
else
{
t->pushdown();
if (t->left->size<k)
{
l=t;
split(t->right, k-t->left->size-, t->right, r);
l->maintain();
}
else
{
r=t;
split(t->left, k, l, t->left);
r->maintain();
}
}
}
node * merge(node * l, node * r)
{
if (l==null) return r;
if (r==null) return l;
if (l->weight>r->weight)
{
l->pushdown();
l->right=merge(l->right, r);
l->maintain();
return l;
}
else
{
r->pushdown();
r->left=merge(l, r->left);
r->maintain();
return r;
}
}
} int N, mouse;
char str[size];
treap::node * root=treap::newnode(' ', );
void print(treap::node * );
inline void insert(char * , int);
inline void remove(int);
inline void rotate(int);
inline char get(); int main()
{
int x; scanf("%d", &N); mouse=;
root->weight=rand();
for ( ;N;N--)
{
scanf("%s", str);
if (str[]=='G') putchar(get()), putchar('\n');
else if (str[]=='M') scanf("%d", &x), mouse=x;
else if (str[]=='P' && mouse) mouse--;
else if (str[]=='N' && mouse<root->size) mouse++;
else if (str[]=='R') scanf("%d", &x), rotate(x);
else if (str[]=='D') scanf("%d", &x), remove(x);
else
{
scanf("%d", &x);
do gets(str); while (!strlen(str));
insert(str, x);
}
} return ;
}
inline void insert(char * s, int l)
{
treap::node * L, * M, * R;
treap::split(root, mouse, L, R);
M=treap::newtreap(s, l, inf);
L=treap::merge(L, M);
root=treap::merge(L, R);
}
inline void remove(int l)
{
treap::node * L, * M, * R;
treap::split(root, mouse, L, R);
treap::split(R, l, M, R);
root=treap::merge(L, R);
}
inline void rotate(int l)
{
treap::node * L, * M, * R;
treap::split(root, mouse, L, R);
treap::split(R, l, M, R);
M->rev^=;
L=treap::merge(L, M);
root=treap::merge(L, R);
}
inline char get()
{
treap::node * i=root;
int k=mouse+;
for ( ; ; )
{
i->pushdown();
if (i->left->size+==k) return i->ch;
if (i->left->size+<k)
{
k-=i->left->size+;
i=i->right;
}
else
i=i->left;
}
}

本傻装B不成反被艹系列

[AHOI 2006][BZOJ 1269]文本编辑器editor的更多相关文章

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

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

  2. BZOJ 1269 文本编辑器 Splay

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

  3. 【BZOJ】【1269】【AHOI2006】文本编辑器editor

    Splay Splay序列维护的模板题了……为了便于处理边界情况,我们可以先插入两个空格当作最左端和最右端,然后……其实本题主要考察的就是Build.splay和Findkth这三个操作,我们可以实现 ...

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

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

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

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

  6. AHOI2006文本编辑器editor

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

  7. BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor

    BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor 题意: 分析: splay模拟即可 注意1507的读入格式 ...

  8. BZOJ1269 [AHOI2006]文本编辑器editor 【82行splay】

    1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 4633  Solved: 1782 [Sub ...

  9. [bzoj1269]文本编辑器editor [bzoj1500]维修数列

    1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2540 Solved: 923 [Submit ...

随机推荐

  1. LINQ基础 之 LINQ TO SQL (二)

    配置LINQ TO SQL 首先添加一个Linq to sql文件,以.dbml结尾的文件.无法把表拖拽到.dbml文件中,提示“所选对象使用不支持的数据提供程序” 解决方案 在服务器资源管理器中右键 ...

  2. 外部表与partition

    在建立普通表的时候,如果数据是有分区的,在ADD DATA的时候需要指明分区,比方下面的例子: user表,包含 id bigint,name string,然后按照时间(date)来进行分区,路径存 ...

  3. type safe printf

    在书里看到的,摘录如下: #include <iostream> #include <stdexcept> template<class T> struct is_ ...

  4. ICTCLAS50中文分词软件“Init ICTCLAS failed!”问题

    if (!ICTCLAS_Init(Server.MapPath("ICTCLAS50")))            {               Response.Write( ...

  5. Ubuntu 升级VisualBox后无法启动 Kernel driver not installed (rc=-1908)

    VisualBox之所以在Linux上比传统的VMware快得多,关键一点就是它和Linux内核的结合比较紧密,这也是开源的优点. 不过Linux内核更新很频繁,每次更新内核后启动VirtualBox ...

  6. Python的魔法方法 .

    基本行为和属性 __init__(self[,....])构造函数 . 在实例化对象的时候会自动运行 __del__(self)析构函数 . 在对象被回收机制回收的时候会被调用 __str__(sel ...

  7. android中KSOAP2中的anytype{}问题

    如果web返回为空,即空字符串的时候,KSOAP2会返回一个anytype{}这样的一个串回来,要比对下是不是这个串,如果是就返回null,从而避免查不到数据时没有相应处理. if (jsonStr. ...

  8. python3爬虫初探(四)之文件保存

    接着上面的写,抓取到网址之后,我们要把图片保存到本地,这里有几种方法都是可以的. #-----urllib.request.urlretrieve----- import urllib.request ...

  9. C#基础知识学习

    C#基础知识整理 学习地址:http://blog.csdn.net/column/details/csarp.html

  10. ASP.NET 中DataGrid item 绑定方法

    <Columns> <asp:TemplateColumn HeaderImageUrl="../../Images/delete.GIF"> <He ...