练手QAQ

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string> using namespace std; void setIO(const string& a) {
freopen((a+".in").c_str(), "r", stdin);
freopen((a+".out").c_str(), "w", stdout);
} struct Node* null; const int N = * * + ; struct Node {
char v;
int sz;
Node* ch[]; Node(char v = ) : v(v) {
ch[] = ch[] = null;
sz = ;
} void maintain() {
sz = ch[]->sz + ch[]->sz + ;
} int cmp(int k) const {
int s = ch[]->sz + ;
if(k == s) return -;
return k < s ? : ;
}
}pool[N], *pis = pool, *root; void init() {
null = new(pis++) Node();
null->sz = ;
null->ch[] = null->ch[] = null;
root = new(pis++) Node(-);
root->ch[] = new(pis++) Node(-);
} void rotate(Node*& o, int d) {
Node* t = o->ch[d];
o->ch[d] = t->ch[d^];
t->ch[d^] = o;
o->maintain();
(o = t)->maintain();
} void splay(Node*& o, int k) {
int d = o->cmp(k);
if(d == -) return;
if(d == ) k -= o->ch[]->sz + ;
Node*& c = o->ch[d];
int d2 = c->cmp(k);
if(d2 != -) {
if(d2 == ) k -= c->ch[]->sz + ;
splay(c->ch[d2], k);
if(d == d2) rotate(o, d);
else rotate(c, d2);
}
rotate(o, d);
} void split(Node*o, int k, Node*& l, Node*& r) {
splay(o, k);
l = o;
r = o->ch[];
o->ch[] = null;
o->maintain();
} Node* merge(Node* l, Node* r) {
splay(l, l->sz);
l->ch[] = r;
l->maintain();
return l;
} void print(Node* o) {
if(o == null) return;
print(o->ch[]);
if( < o->v) putchar(o->v);
print(o->ch[]);
} Node* newtree(const char s[], int l, int r) {
if(l > r) return null;
int mid = (l + r) >> ;
Node* o = new(pis++) Node();
o->ch[] = newtree(s, l, mid - );
o->v = s[mid];
o->ch[] = newtree(s, mid + , r);
o->maintain();
return o;
} char text[N]; void insert(int pos, Node* o) {
Node *lft, *rgt;
split(root, pos, lft, rgt);
root = merge(lft, merge(o, rgt));
} int main() {
#ifdef DEBUG
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif int m;
scanf("%d", &m);
init(); char opt[], c;
int pos = , n, sz = ;
Node *o, *lft, *rgt, *mid; while(m--) {
scanf("%s", opt);
if(strcmp(opt, "Move") == ) {
scanf("%d", &pos); ++pos;
}else if(strcmp(opt, "Insert") == ) {
int n;
scanf("%d", &n);
for(int i = ; i < n; i++) {
c = getchar();
while(c == '\n') c = getchar();
text[i] = c;
}
sz += n;
insert(pos, newtree(text, , n - ));
}else if(strcmp(opt, "Delete") == ) {
scanf("%d", &n);
n = min(sz - pos + , n);
split(root, pos, lft, o);
split(o, n, mid, rgt);
sz -= mid->sz;
root = merge(lft, rgt);
}else if(strcmp(opt, "Prev") == ) {if(pos > ) pos--;}
else if(strcmp(opt, "Next") == ) {if(pos <= sz) pos++;}
else {
scanf("%d", &n);
split(root, pos, lft, o);
n = min(sz - pos + , n);
split(o, n, mid, rgt);
print(mid); puts("");
root = merge(merge(lft, mid), rgt);
}
} return ;
}

有空了去写块链?

NOI2003 文本编辑器的更多相关文章

  1. [NOI2003] 文本编辑器 (splay)

    复制炸格式了,就不贴题面了 [NOI2003] 文本编辑器 Solution 对于光标的移动,我们只要记录一下现在在哪里就可以了 Insert操作:手动维护中序遍历结果,即每次取中点像线段树一样一样递 ...

  2. 洛谷 P4008 [NOI2003]文本编辑器 解题报告

    P4008 [NOI2003]文本编辑器 题目描述 很久很久以前,\(DOS3.x\)的程序员们开始对 \(EDLIN\) 感到厌倦.于是,人们开始纷纷改用自己写的文本编辑器⋯⋯ 多年之后,出于偶然的 ...

  3. [NOI2003]文本编辑器 [Fhq Treap]

    [NOI2003]文本编辑器 没啥好说的 就是个板子 #include <bits/stdc++.h> // #define int long long #define rep(a , b ...

  4. cogs 330. [NOI2003] 文本编辑器

    ★★★   输入文件:editor2003.in   输出文件:editor2003.out   简单对比 时间限制:2 s   内存限制:128 MB [问题描述] 很久很久以前,DOS3.x的程序 ...

  5. luogu P4008 [NOI2003]文本编辑器 splay 块状链表

    LINK:文本编辑器 这个东西感觉块状链表写细节挺多 (块状链表本来就难写 解释一下块状链表的做法:其实是一个个数组块 然后利用链表给链接起来 每个块的大小为sqrt(n). 这样插入删除的时候直接暴 ...

  6. NOI2003 文本编辑器editor

    1507: [NOI2003]Editor Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 1908  Solved: 738[Submit][Statu ...

  7. 题解 P4008 【[NOI2003]文本编辑器】

    块状链表及其应用 思路楼上已经说的很清楚了 看代码注释 代码很丑 #include<cstdio> #include<cctype> #include<cstring&g ...

  8. P4008 [NOI2003]文本编辑器

    思路 FHQ Treap的板子 用FHQ Treap维护中序遍历序列即可 然后数组开够! 代码 #include <cstdio> #include <cstring> #in ...

  9. 【洛谷 P4008】 [NOI2003]文本编辑器 (Splay)

    题目链接 \(Splay\)先练到这吧(好像还有道毒瘤的维护数列诶,算了吧) 记录下光标的编号,维护就是\(Splay\)基操了. 另外数据有坑,数据是\(Windows\)下生成了,回车是'\n\r ...

随机推荐

  1. iOS 中如何监测某段代码运行的时间

    在iOS里面有时间涉及到网络请求,有时间涉及到数据库的查询,我们需要计算该段代码的效率, 以及执行时间方面的问题,为此,可以使用下面方法: double a = CFAbsoluteTimeGetCu ...

  2. IIS部署.NET项目的有关事项_2015.07.02

    今天在做项目中的关于发送邮件的一些功能.在部署服务的时候遇到了一些奇葩的问题,基本上是和IIS有关的问题. 首先,项目是基于.NET Framework4.0 版本的,由于本人用的是一台新配置好的机器 ...

  3. POJ3285 River Hopscotch(最大化最小值之二分查找)

    POJ3285 River Hopscotch 此题是大白P142页(即POJ2456)的一个变形题,典型的最大化最小值问题. C(x)表示要求的最小距离为X时,此时需要删除的石子.二分枚举X,直到找 ...

  4. 微信公众平台开发(免费云BAE+高效优雅的Python+网站开放的API)

    虽然校园App是个我认为的绝对的好主意,但最近有个也不错的营销+开发的模式出现:微信平台+固定域名服务器. 微信公众平台的运行模式不外两个: 一.机器人模式或称转发模式,将说话内容转发到服务器上完成, ...

  5. 谷歌地图实现车辆轨迹移动播放(google map api)

    开发技术:jquery,js baidu map api,json,ajax QQ1310651206 谷歌地图(google map api)实现车辆轨迹移动播放(google map api)

  6. 代码bug

    1.webstorm ide未配置basePath本地会加入根路径 2.点击一次就销毁可以给标签设置一个值data-val="0" 某个函数只执行一次的方法,或者也可以考虑绑用on ...

  7. JSP执行过程

    JSP执行流程: @1.客户端发出请求. @2.Web容器将JSP转译成Servlet源代码. @3.Web容器将产生的源代码进行编译. @4.Web容器加载编译后的代码并执行. @5.把执行结果响应 ...

  8. K - 计算球体积

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description 根据输入的 ...

  9. 转:php使用websocket示例详解

    原文来自于:http://www.jb51.net/article/48019.htm 这篇文章主要介绍了php使用websocket示例,需要的朋友可以参考下   下面我画了一个图演示 client ...

  10. Application路径

    根目录:StreamingAssets文件夹 #if UNITY_EDITOR string filepath = Application.dataPath +"/StreamingAsse ...