BZOJ 1507 Editor
Description

Input
Output
Sample Input
Insert 26
abcdefghijklmnop
qrstuv wxy
Move 16
Delete 11
Move 5
Insert 1
^
Next
Insert 1
_
Next
Next
Insert 4
.\/.
Get 4
Prev
Insert 1
^
Move 0
Get 22
Sample Output
abcde^_^f.\/.ghijklmno
HINT
splay区间操作的裸题,就当是练习模板吧!
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
int t,a,p = ;
char cmd[],s[*+];
class Splay
{
private:
struct Node
{
char ch;int size,cnt;Node *c[],*fa;
Node (char _ch,Node *_fa)
{
ch = _ch; fa = _fa;
cnt = ; c[] = c[] = NULL;
}
int lsz(){return c[]?c[]->size:;}
int rsz(){return c[]?c[]->size:;}
};
Node *root;
inline void upd(Node *tag)
{
if (!tag) return;
tag->size = tag->cnt+tag->lsz()+tag->rsz();
}
inline void zig(Node *tag,int d)
{
Node *f = tag -> fa;
int anti = d^;
f -> c[d] = tag->c[anti];
if (f->c[d])
f -> c[d] -> fa = f;
tag -> fa = f -> fa;
if (tag -> fa -> c[] == f)
tag -> fa -> c[] = tag;
else tag -> fa -> c[] = tag;
f -> fa = tag;
tag -> c[anti] = f;
upd(f);
}
inline bool f(Node *tag){return tag->fa->c[] == tag;}
inline void splay(Node *tag,Node *goal)
{
while(tag->fa != goal){
if(tag->fa->fa == goal)
zig(tag,f(tag));
else{
if(f(tag) == f(tag->fa))
zig(tag->fa,f(tag->fa)),zig(tag,f(tag));
else
zig(tag,f(tag)),zig(tag,f(tag));
}
}
upd(tag);
}
inline Node *search(int key)
{
Node *tag = root->c[];
while (tag && (key <= tag->lsz()||key > tag -> lsz() + tag -> cnt))
{
if (key > tag ->lsz()+tag->cnt)
{
key -= tag->lsz()+tag->cnt; tag = tag->c[];
}
else tag = tag -> c[];
}
return tag;
}
public:
Splay()
{
root = new Node('#',NULL); Node *tmp = new Node('^',root); root ->c[] = tmp; upd(tmp);
}
void insert(char *s)
{
Node *chr = new Node(s[],NULL),*tmp,*now,*tag = root->c[];
tmp = now = chr; chr -> size = a;
for (int i = ;i < a;++i)
{
now = new Node(s[i],tmp);
tmp -> c[] = now; now -> size = a-i;
tmp = now;
}
Node *z = tag -> c[];
while (z && z->c[]) z = z->c[];
if (z)
{
splay(z,tag);
z -> c[] = chr; chr -> fa = z;
upd(z);
}
else
{
tag -> c[] = chr; chr -> fa = tag;
upd(tag);
}
}
void trans(int x) {Node *tag = search(x); splay(tag,root);}
void erase(int l)
{
Node *tag = root->c[]; if (!tag) return;
Node *end = search(tag->lsz()+tag->cnt+l+);
if (end){splay(end,tag); end -> c[] = NULL; upd(end);}
else tag -> c[] = NULL;
upd(tag);
}
void print(int l)
{
Node *tag = root->c[];
for (int i = ;i <= l;++i)
{
Node *tmp = search(p+i);
splay(tmp,tag);
putchar(tmp -> ch);
}
putchar('\n');
}
}tree;
int main()
{
freopen("1507.in","r",stdin);
freopen("1507.out","w",stdout);
scanf("%d",&t);
while (t--)
{
scanf("%s %d",cmd,&a);
if (cmd[] == 'I')
{
char c;
for (int i = ;i<a;)
{
while ((c = getchar())==||c == );
s[i++] = c;
}
tree.insert(s); continue;
}
if (cmd[] == 'M')
{
tree.trans(a+); p = a + ; continue;
}
if (cmd[] == 'D')
{
tree.erase(a); continue;
}
if (cmd[] == 'G')
{
tree.print(a); continue;
}
if (cmd[] == 'P')
{
tree.trans(--p); continue;
}
if(cmd[] == 'N')
{
tree.trans(++ p); continue;
}
}
fclose(stdin); fclose(stdout);
return ;
}
BZOJ 1507 Editor的更多相关文章
- BZOJ 1507 Editor(块状链表)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1507 题意:一个文本编辑器,模拟以下操作: 思路:块状链表的主要操作: (1)find( ...
- 【BZOJ 1507】【NOI 2003】&【Tyvj P2388】Editor 块状链表模板题
2016-06-18 当时关于块状链表的想法是错误的,之前维护的是一个动态的$\sqrt{n}$,所以常数巨大,今天才知道原因TwT,请不要参照这个程序为模板!!! 模板题水啊水~~~ 第一次写块状链 ...
- bzoj 1269 bzoj 1507 Splay处理文本信息
bzoj 1269 题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1269 大致思路: 用splay维护整个文本信息,splay树的中序遍历即为 ...
- BZOJ 1507 [NOI2003]Editor
Description Input 输 入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中可能会插入一些回车符,请忽略掉 ...
- BZOJ 1507 NOI2003 Editor Splay
题目大意: 1.将光标移动到某一位置 2.在光标后插入一段字符串 3.删除光标后的一段字符 4.输出光标后的一段字符 5.光标-- 6.光标++ 和1269非常像的一道题,只是弱多了 几个问题须要注意 ...
- BZOJ 1507 splay
写完维修数列 这不是水题嘛233333 //By SiriusRen #include <cstdio> #include <cstring> #include <alg ...
- BZOJ 1269: [AHOI2006]文本编辑器editor( splay )
splay..( BZOJ 1507 题目基本相同..双倍经验 ) ------------------------------------------------------------------ ...
- 平衡树之伸展树(Splay Tree)题目整理
目录 前言 练习1 BZOJ 3224 普通平衡树 练习2 BZOJ 3223 文艺平衡树 练习3 BZOJ 1588 [HNOI2002]营业额统计 练习4 BZOJ 1208 [HNOI2004] ...
- 【BZOJ】1507: [NOI2003]Editor(Splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=1507 当练splay模板了,发现wjmzbmr的splay写得异常简介,学习了.orzzzzzzzz ...
随机推荐
- 手把手教学:详解HTML5移动开发框架PhoneJS
摘要:HTML/JavaScript的优势自不必说,但却也并非完美,相比之下,原生App占内存更少.响应更快.本文详解了HTML5移动开发框架PhoneJS的使用全过程,通过它,能够让Web应用在移动 ...
- 开源 免费 java CMS - FreeCMS2.1 会员站内信
项目地址:http://www.freeteam.cn/ 站内信 1.1.1 写信 从左側管理菜单点击写信进入. 输入收信人.标题.内容后点击发送button. 1.1.2 收件箱 从左側管理菜单点击 ...
- js添加遮罩层
直接用代码来说明 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MaskT ...
- android开发之调试技巧 分类: android 学习笔记 2015-07-18 21:30 140人阅读 评论(0) 收藏
我们都知道,android的调试打了断点之后运行时要使用debug as->android application 但是这样的运行效率非常低,那么我们有没有快速的方法呢? 当然有. 我们打完断点 ...
- 理解JavaScript的定时器与回调机制
定时器方法 JavaScript是单线程的.虽然HTML5已经开始支持异步js了. JavaScript的setTimeout与setInterval看起来就像已经是多线程的了.但实际上setTime ...
- C#,MVC视图中把枚举转成DropdownList
1.拓展EnumHelper public static class EnumHelper { // Get the value of the description attribute if the ...
- 试着开发chrome插件
我的第一个chrome插件,是app形式的 代码如下 创建一个文件: 1.manifest.json { "version": "1.0", "man ...
- Http,Https (SSL)的Url绝对路径,相对路径解决方案Security Switch 4.2 英文帮助文档 分类: ASP.NET 2014-10-28 10:50 147人阅读 评论(1) 收藏
Security Switch 4.2 =================== Security Switch enables various ASP.NET applications to auto ...
- equals函数的作用
1.equals函数在什么地方 在Object类中,写法与==一样,但是我们用的时候要重写这个equals方法 String类型中的equals是复写好的 2.equals函数的作用 ==号在比较两个 ...
- Android开发常见错误类型一览表
这是我的第一个博客,我会一直添加我在Android开发中遇到的错误,用来记录我开发中踩过的那些坑 ------------------------分割线------------------------ ...