[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 ...
随机推荐
- RPI学习--环境搭建_刷卡+wiringPi库安装
1,镜像地址 http://www.raspberrypi.org/downloads/ 2,Windows下刷写工具 Win32 Disk Imager 3,安装wiringPi库 (这里在连网状态 ...
- (五)CoreData 使用 (转)
第一次真正的使用CoreData,因此也会写下体会和心得...等有时间 Core Data数据持久化是对SQLite的一个升级,它是ios集成的,在说Core Data之前,我们先说说在CoreDat ...
- IOS 导航栏属性设置
IOS 7 以上系统导航栏: [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; // 返回按钮颜色 [UINaviga ...
- hdu 2049
Ps:WA了无限次...简直做到崩溃..高中学的知识都忘了....这道题就是跟2048差不多.. 从N个人里选M个人,有Cmn种选法,然后就是M的错排*Cnm 代码: #include "s ...
- php源码编译常见错误解决方案
在CentOS编译PHP5的时候有时会遇到以下的一些错误信息,基本上都可以通过yum安装相应的库来解决.以下是具体的一些解决办法: checking for BZip2 support… yes ch ...
- javascript笔记4-函数表达式
一般形式的创建函数,在执行代码之前会先读取函数声明,所以可以把函数声明写在函数调用的下面: sayHi(); function sayHi(){ alert("Hi!"); } 使 ...
- (spring-第4回【IoC基础篇】)spring基于注解的配置
基于XML的bean属性配置:bean的定义信息与bean的实现类是分离的. 基于注解的配置:bean的定义信息是通过在bean实现类上标注注解实现. 也就是说,加了注解,相当于在XML中配置了,一样 ...
- H5+app前端后台ajax交互总结
流应用开发 1.前端是HBuilder 编写的html页面,UI控件用MUI: 2.后台用Eclipse开发的Servlet做控制器: 3.前后台交互用MUI的Ajax. 在Hbuilder中选择在安 ...
- IOS上传图片
//原文地址http://www.cnblogs.com/skyblue/archive/2013/05/08/3067108.html,因为以后要用到,搬来存下// // RequestPostUp ...
- 最熟悉的陌生人:ListView 中的观察者模式
RecyclerView 得宠之前,ListView 可以说是我们用的最多的组件.之前一直没有好好看看它的源码,知其然不知其所以然. 今天我们来窥一窥 ListView 中的观察者模式. 不熟悉观察者 ...