NOI2003 文本编辑器
练手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 文本编辑器的更多相关文章
- [NOI2003] 文本编辑器 (splay)
复制炸格式了,就不贴题面了 [NOI2003] 文本编辑器 Solution 对于光标的移动,我们只要记录一下现在在哪里就可以了 Insert操作:手动维护中序遍历结果,即每次取中点像线段树一样一样递 ...
- 洛谷 P4008 [NOI2003]文本编辑器 解题报告
P4008 [NOI2003]文本编辑器 题目描述 很久很久以前,\(DOS3.x\)的程序员们开始对 \(EDLIN\) 感到厌倦.于是,人们开始纷纷改用自己写的文本编辑器⋯⋯ 多年之后,出于偶然的 ...
- [NOI2003]文本编辑器 [Fhq Treap]
[NOI2003]文本编辑器 没啥好说的 就是个板子 #include <bits/stdc++.h> // #define int long long #define rep(a , b ...
- cogs 330. [NOI2003] 文本编辑器
★★★ 输入文件:editor2003.in 输出文件:editor2003.out 简单对比 时间限制:2 s 内存限制:128 MB [问题描述] 很久很久以前,DOS3.x的程序 ...
- luogu P4008 [NOI2003]文本编辑器 splay 块状链表
LINK:文本编辑器 这个东西感觉块状链表写细节挺多 (块状链表本来就难写 解释一下块状链表的做法:其实是一个个数组块 然后利用链表给链接起来 每个块的大小为sqrt(n). 这样插入删除的时候直接暴 ...
- NOI2003 文本编辑器editor
1507: [NOI2003]Editor Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 1908 Solved: 738[Submit][Statu ...
- 题解 P4008 【[NOI2003]文本编辑器】
块状链表及其应用 思路楼上已经说的很清楚了 看代码注释 代码很丑 #include<cstdio> #include<cctype> #include<cstring&g ...
- P4008 [NOI2003]文本编辑器
思路 FHQ Treap的板子 用FHQ Treap维护中序遍历序列即可 然后数组开够! 代码 #include <cstdio> #include <cstring> #in ...
- 【洛谷 P4008】 [NOI2003]文本编辑器 (Splay)
题目链接 \(Splay\)先练到这吧(好像还有道毒瘤的维护数列诶,算了吧) 记录下光标的编号,维护就是\(Splay\)基操了. 另外数据有坑,数据是\(Windows\)下生成了,回车是'\n\r ...
随机推荐
- UITableViewCell 左滑删除
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return Y ...
- IOI1994 北京2008的挂钟 迭代加深
总的来讲,这是一道很⑨的题,因为: (1)题目中有⑨个挂钟 (2)有⑨种操作方案 (3)这题因为解空间太小所以可以直接⑨重循环!! 这题可以用迭代加深搜索高效求解,剪枝的策略也很显然: >所求的 ...
- 【BZOJ1012】【树状数组求区间最值】最大数maxnumber
Description 现在请求你维护一个数列,要求提供以下两种操作: 1. 查询操作.语法:Q L 功能:查询当前数列中末尾L个数中的最大的数,并输出这个数的值.限制:L不超过当前数列的长度. 2. ...
- Cocos2dx开发(2)——Win8.1下Cocod2dx 3.2环境搭建
正式开始搭建cocos2dx环境,回到熟悉的VS 1.Python安装配置 这一步很简单,下载Python2.7.3,笔者直接用软件助手直接下载安装,最后配置环境变量 如下成功 2.cocos2dx ...
- JS判断终端(Android IOS)
function getMobileOperatingSystem() { var userAgent = navigator.userAgent || navigator.vendor || win ...
- WIN7下运行hadoop程序报:Failed to locate the winutils binary in the hadoop binary path
之前在mac上调试hadoop程序(mac之前配置过hadoop环境)一直都是正常的.因为工作需要,需要在windows上先调试该程序,然后再转到linux下.程序运行的过程中,报Failed to ...
- C#,新建的系统服务项目有些机器不能运行
检查了一下,是权限的问题 右键ProjectInstaller.cs 在设计界面里找到serviceProcessInstaller1右键属性 找到Account属性改为:LocalSystem
- HTML 动态显示系统当前时间
HTML 动态显示系统当前时间: <div id="time"> <script> document.getElementById('time').inne ...
- php之递归调用,递归创建目录
/* 递归自身调用自身,每次调用把问题简化,直到问题解决 即:把大的任务拆成相同性质的多个小任务完成 */ /* function recsum($n){ if($n>1){ return $n ...
- window 配置 sendmail
从http://glob.com.au/sendmail/下载sendmail.zip 解压sendmail.zip到目录下(最好使用短路径,长路径会导致问题的出现),我安装的路径是: E:\wamp ...