[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 ...
随机推荐
- main函数参数的使用
int main(int argc, char * argv[]) argc: argument count argv:argument vector 其中, char * argv[] 指针数组 c ...
- 使用WebClient Post方式模拟上传文件和数据
假如某网站有个表单,例如(url: http://localhost/login.aspx):帐号 密码 我们需要在程序中提交数据到这个表单,对于这种表单,我们可以使用 WebClient.Uplo ...
- Linux内核-链表
linux内核链表的定义(定义了双向链表,不含数据域) 定义在 /linux-source-3.13.0/include/linux/types.h 头文件中. struct list_head { ...
- Mapper类/Reducer类中的setup方法和cleanup方法以及run方法的介绍
在hadoop的源码中,基类Mapper类和Reducer类中都是只包含四个方法:setup方法,cleanup方法,run方法,map方法.如下所示: 其方法的调用方式是在run方法中,如下所示: ...
- JS 防止表单重复提交
<script type="text/javascript"> var checkSubmitFlg = false; function checkSubmit() { ...
- 2016 - 1 -19 初学HTML5 第一天
1.HTML COMMANDS MHTL commands called elements.Usually, an element has a start tag and an end tag e.g ...
- 【LeetCode OJ】Word Ladder I
Problem Link: http://oj.leetcode.com/problems/word-ladder/ Two typical techniques are inspected in t ...
- WPF Step By Step 完整布局介绍
WPF Step By Step 完整布局介绍 回顾 上一篇,我们介绍了基本控件及控件的重要属性和用法,我们本篇详细介绍WPF中的几种布局容器及每种布局容器的使用场景,当 然这些都是本人在实际项目中的 ...
- magento添加分类属性
在magento中给产品添加自定义属性是很容易实现在后台就可以很轻易添加,但是给分类就不行了,magento本身没有提供给category添加自定义属性.在实际的运用过程中我们想给cagegory添加 ...
- magento后台登陆被锁定 索引报错的解决:General error: 1205 Lock wait timeout
1. magento在索引的时候用shell,有时候会报错: General error: 1205 Lock wait timeout exceeded 这个时候,是因为行锁的原因,在表中您直接用s ...