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 ...
随机推荐
- delphi TColorDialog
TColorDialog 预览 实现过程 动态创建和使用颜色对话框 function ShowColorDlg:TColor;begin with TColorDialog.Cre ...
- JAVA基于AE调用GP实现泰森多边形
调用GP实现数据处理是较快捷.较易入手的方法. 使用JAVA语言基于AE调用GP实现泰森多边形的代码例如以下: public void CreatVoronoi(){ try { GeoProcess ...
- QT+QT creator+OpenCV图像灰度化
1).pro文件 #------------------------------------------------- # # Project created by QtCreator 2014-05 ...
- 京东手机webapp商城
http://bases.wanggou.com/mcoss/mportal/show?tabid=2&ptype=1&actid=1562&tpl=3&pi=1&am ...
- [React] React Fundamentals: with-addons - ReactLink
It can be tedious to type out all the boilerplate needed to get the DOM and states in React to synch ...
- 晨曦之光 linux Crontab 使用(转)
cron用法说明 cron的用法老是记不住,索性写下来备忘.下文内容大部分是根据<Cron Help Guide>翻译而来,有些部分是自己加上的. 全文如下: cron来源于希腊单词chr ...
- android 22 启动带2个action值的预定义acticity
main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" andro ...
- 轻量级的原型设计工具-Axure RP
1. 软件下载地址: http://www.downxia.com/downinfo/25742.html 这个版本不需要注册码,不需要安装,存绿色版. 2. 基本介绍教程: http://wenku ...
- 从高德 SDK 学习 Android 动态加载资源
前不久跑去折腾高德 SDK 中的 HUD 功能,相信用过该功能的用户都知道 HUD 界面上的导航转向图标是动态变化的.从高德官方导航 API 文档中 AMapNaviGuide 类的描述可知,导航转向 ...
- Python_oldboy_自动化运维之路(一)
python简介: Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它具有 ...