BZOJ1507 [NOI2003]Editor
是一道裸的Splay(反正我不会Splay,快嘲笑我!)
只需要维护在数列上加点删点操作即可,不会写Splay的渣渣只好Orz iwtwiioi大神一下了。(后来发现程序直接抄来了。。。)
就当我的第一个Splay程序吧。
/**************************************************************
Problem: 1507
User: rausen
Language: C++
Result: Accepted
Time:2560 ms
Memory:43860 kb
****************************************************************/ #include <cstdio>
#include <cstring> using namespace std;
char strI[]; struct Splay{
struct node{
node *son[], *fa;
char key;
int size;
node(){
son[] = son[] = fa = NULL;
size = key = ;
}
void pushup(){
size = son[] -> size + son[] -> size + ;
} bool d(){
return this == fa -> son[];
} void setc(node *c, int d){
son[d] = c;
c -> fa = this;
}
} * null, *root; Splay(){
null = new node;
root = null;
root = newnode();
node *t = newnode();
root -> setc(t, );
root -> pushup();
} node *newnode(char K){
node *res = new node;
res -> son[] = res -> son[] = res -> fa = null;
res -> key = K, res -> size = ;
return res;
} void rotate(node *r){
node *fa = r -> fa;
bool d = r -> d();
fa -> fa -> setc(r, fa -> d());
fa -> setc(r -> son[!d], d);
r -> setc(fa, !d);
fa -> pushup();
if (fa == root) root = r;
} void splay(node *r, node *X){
while (r -> fa != X){
if (r -> fa -> fa == X) rotate(r);
else r -> d() == r -> fa -> d() ? (rotate(r -> fa), rotate(r)) : (rotate(r), rotate(r));
}
r -> pushup();
} node *sel(int k){
int s;
for(node *t = root; ;){
s = t -> son[] -> size;
if (s == k) return t;
t = t -> son[k > s];
if (k > s) k -= s + ;
}
} node *getrange(int l, int r){
node *left = sel(l); splay(left, null);
node *right = sel(r); splay(right, left);
return right;
} void insert(int w, int cur){
node *t = getrange(w, w + );
t -> setc(build(, cur), );
t -> pushup();
splay(t, null);
} void remove(int w, int n){
node *t = getrange(w, w + n + );
t -> setc(null, );
t -> pushup();
splay(t, null);
} node *build(int l, int r){
if (l >= r) return null;
int m = (l + r) >> ;
node *t = newnode(strI[m]);
t -> setc(build(l, m), );
t -> setc(build(m + , r), );
t -> pushup();
return t;
} void print(node *r){
if (r == null) return;
print(r -> son[]);
printf("%c", r -> key);
print(r -> son[]);
} void print(int w, int n){
node *t = getrange(w, w + n + );
print(t -> son[]);
printf("\n");
}
} splay; char s[]; int main(){
int n, T, cur, w = ;
scanf("%d", &T);
while (T--){
scanf("%s", s);
if (s[] == 'M')
scanf("%d", &w);
else if (s[] == 'P')
--w;
else if (s[] == 'N')
++w;
else if (s[] == 'D'){
scanf("%d", &n);
splay.remove(w, n);
} else
if (s[] == 'G'){
scanf("%d", &n);
splay.print(w, n);
} else
if (s[] = 'I'){
scanf("%d", &n);
cur = ;
while (n--){
while(strI[cur] = getchar(), strI[cur] == '\n');
cur++;
}
splay.insert(w, cur);
}
}
return ;
}
BZOJ1507 [NOI2003]Editor的更多相关文章
- BZOJ1507 [NOI2003]Editor 【splay】
1507: [NOI2003]Editor Time Limit: 5 Sec Memory Limit: 162 MB Submit: 4129 Solved: 1660 [Submit][St ...
- [BZOJ1507] [NOI2003] Editor (splay)
Description Input 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中可能会插入一些回车符,请忽略掉它 ...
- 【bzoj1507】[NOI2003]Editor /【bzoj1269】[AHOI2006]文本编辑器editor Splay
[bzoj1507][NOI2003]Editor 题目描述 输入 输入文件editor.in的第一行是指令条数t,以下是需要执行的t个操作.其中: 为了使输入文件便于阅读,Insert操作的字符串中 ...
- 【BZOJ-1507】Editor 块状链表
1507: [NOI2003]Editor Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 3397 Solved: 1360[Submit][Stat ...
- BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor
BZOJ_1269&&1507_[AHOI2006]文本编辑器editor&&[NOI2003]Editor 题意: 分析: splay模拟即可 注意1507的读入格式 ...
- 1507: [NOI2003]Editor(块状链表)
1507: [NOI2003]Editor Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 4157 Solved: 1677[Submit][Stat ...
- 1507: [NOI2003]Editor
1507: [NOI2003]Editor Time Limit: 5 Sec Memory Limit: 162 MB Submit: 3535 Solved: 1435 [Submit][St ...
- [NOI2003]Editor & [AHOI2006]文本编辑器editor BZOJ1507&BZOJ1269
分析: Splay区间操作裸题,维护出区间信息,按照要求模拟,注意读入格式,并且考虑内存回收(开不下) 附上代码: #include <cstdio> #include <algor ...
- 【bzoj1507】[NOI2003]Editor
第二次写rope了 rope大法好!!! #include<algorithm> #include<iostream> #include<ext/rope> #in ...
随机推荐
- Base64加密解密原理以及代码实现(VC++)
Base64加密解密原理以及代码实现 转自:http://blog.csdn.net/jacky_dai/article/details/4698461 1. Base64使用A--Z,a--z,0- ...
- Monocular Vision
Monocular Vision: a condition in which one eye is blind, seeing with only one eye Binocular Vision: ...
- 输出sed的重定向
默认情况下,sed编辑器会将进步的结果输出到STDOUT上,你可以在shll脚本中使用所有重订向sed编辑器输出的标准方法. 你可以在脚本中用反引号来将sed编辑器命令的输出重定向到一个变量中供后面使 ...
- MyEclipse manage Deployment 管理器失效
如果你使用的是10.7版本,可参考本文 出错: 1.Deployments 部署按钮失效; 2.MyEclipse 启动后初始化WorkSpaces抛出Deployment的空指针异常(java.la ...
- QQReg.java
import java.awt.*; import javax.swing.*; public class QQReg extends JFrame{ public static void main( ...
- EAPOL 协议
EAPOL 协议 一.基本概念 EAPOL 的全称为 Extensible Authentication Protocol Over LAN,即 EAP Over Lan,也即基于局域网的扩展认证协议 ...
- Jump Game II
Description: Given an array of non-negative integers, you are initially positioned at the first inde ...
- Android开源库--Gson谷歌官方json解析库
官方文档地址:http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/index.html 官方网站:http://code.go ...
- hdu 1142 用优先队列实现Dijkstra
之前很认真地看了用优先队列来实现Dijkstra这块,借鉴了小白书上的代码模板后,便拿这道题来试试水了.这道题的大意就是问你从地点1到地点2有多少条满足条件的路径(假设该路径经过 1->...- ...
- hdu 1005 简单题
今早水出的第一道题,带着情绪做的,竟然1Y了,确实惊奇.这道简单的线性递推取模,直接递推是不行的,因为n的规模达到了100,000,000,要么超时要么超内存.可以用矩阵快速幂来搞,根据题意构建出对应 ...