[AHOI 2006][BZOJ 1269]文本编辑器editor
好吧,我承认这是我用来刷随笔数的喵~
这是一道 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的更多相关文章
- BZOJ 1269 文本编辑器editor(伸展树)
题意 https://www.lydsy.com/JudgeOnline/problem.php?id=1269 思路 伸展树(\(\text{splay}\))功能比较齐全的模板,能较好的体现 \( ...
- BZOJ 1269 文本编辑器 Splay
题目大意:维护一个文本编辑器,支持下列操作: 1.将光标移动到某一位置 2.在光标后插入一段字符串 3.删除光标后的一段字符 4.翻转光标后的一段字符 5.输出光标后的一个字符 6.光标-- 7.光标 ...
- 【BZOJ】【1269】【AHOI2006】文本编辑器editor
Splay Splay序列维护的模板题了……为了便于处理边界情况,我们可以先插入两个空格当作最左端和最右端,然后……其实本题主要考察的就是Build.splay和Findkth这三个操作,我们可以实现 ...
- BZOJ 1269: [AHOI2006]文本编辑器editor( splay )
splay..( BZOJ 1507 题目基本相同..双倍经验 ) ------------------------------------------------------------------ ...
- BZOJ 1269: [AHOI2006]文本编辑器editor (splay tree)
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1213 Solved: 454[Submit ...
- AHOI2006文本编辑器editor
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1885 Solved: 683[Submit ...
- BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor
BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor 题意: 分析: splay模拟即可 注意1507的读入格式 ...
- BZOJ1269 [AHOI2006]文本编辑器editor 【82行splay】
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MB Submit: 4633 Solved: 1782 [Sub ...
- [bzoj1269]文本编辑器editor [bzoj1500]维修数列
1269: [AHOI2006]文本编辑器editor Time Limit: 10 Sec Memory Limit: 162 MB Submit: 2540 Solved: 923 [Submit ...
随机推荐
- python利用or在列表解析中调用多个函数.py
python利用or在列表解析中调用多个函数.py """ python利用or在列表解析中调用多个函数.py 2016年3月15日 05:08:42 codegay & ...
- Android类参考---Fragment
Android类参考---Fragment public final boolean isAdded() 如果该Fragment对象被添加到了它的Activity中,那么它返回true,否则返回fal ...
- 摘自:java夜未眠之java学习之道
目前Java可以说是产业界和学术界最热门的语言,许多读者都很急切想把Java学好.除非是武侠小说中的运功传送内力的方式,否则花上一段时间苦学是免不了的.花时间,不打紧,就是怕方法错误,事倍功半.我认为 ...
- catch的执行与try的匹配
这里有一段代码: public class EmbededFinally { public static void main(String args[]) { int result; try { Sy ...
- 2016-1-9 Quartz框架的学习,写字板demo
一:自定义view .h文件中代码如下 #import <UIKit/UIKit.h> @interface ZLpaintView : UIView @property(nonatomi ...
- 为什么socket编程要用到多线程
不得不佩服计算机先驱的设计:socket编程为什么需要多线程.如果只有一个ServerSocket线程,那么如下代码: public void start() throws Exception { S ...
- php大力力 [035节] 先记录一些链接
[IT名人堂]专访百分点研发总监:不止于平台,大数据操作系统重磅来袭! [2015-8-11 14:17:04] [IT名人堂]专访1号店技术总监:大型电商网站的IT架构 [2015-8-25 15: ...
- Magento Soap Api接口出错无法使用
在给客户测试Magento Soap接口的时候出现如下错误提示. This page contains the following errors:error on line 3 at column 6 ...
- magento搬家步骤和可能遇到的问题
将原来网站文件中的var文件中的cache和session文件删除,将media中的缓存文件删除.然后将所有文件制作成一个压缩包,以减少文件体积,方便转移. 将压缩包转移到新的服务器域名指向的文件夹, ...
- matio使用
http://na-wiki.csc.kth.se/mediawiki/index.php/MatIO (1)build根据教程 (2)sudo ldconfig (3)写main根据链接:修改几个类 ...