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 ...
随机推荐
- 查看perl及其模块
Perl本身自带了很丰富的文档,如果把它们都打印出来,恐怕要耗费大量纸墨.我们试图清点过,但数到2000页左右的时候就数不清了(不用担心,我们用虚拟打印机计算页面数量,这样不会对树木造成威胁,挺环保的 ...
- 简单的poi导出excel文件
/**** 创建excel文件**/ 1 import java.io.FileOutputStream; import java.io.IOException; import java.util.C ...
- RelativeLayout用到的一些重要的属性:
下面是常用的一些属性 RelativeLayout用到的一些重要的属性: 第一类:属性值为true或falseandroid:layout_centerHrizontal 水平居中android:la ...
- TAROT.
/* * * */ #include<stdio.h> #include<stdlib.h> #include<time.h> int main() { int t ...
- Linux_常用命令_03_磁盘/挂载_信息查看
1. 1.1. mount 不带参数的话,显示的是 当前已经挂载的情况 1.2. df 不带参数的话,硬盘分区状况查询 2. 2.1. cat /proc/partitions 2.2. fdisk ...
- Yii2.0 依赖注入(DI)和依赖注入容器的原理
依赖注入和依赖注入容器 为了降低代码耦合程度,提高项目的可维护性,Yii采用多许多当下最流行又相对成熟的设计模式,包括了依赖注入(Denpdency Injection, DI)和服务定位器(Serv ...
- 三大平衡树(Treap + Splay + SBT)总结+模板[转]
Treap树 核心是 利用随机数的二叉排序树的各种操作复杂度平均为O(lgn) Treap模板: #include <cstdio> #include <cstring> #i ...
- redhat 6.4 yum 本地配置简记
准备工作 ----------------------------------------------------------------------------- 1. 加载光驱 将iso镜像文件 ...
- IIS_Mvc发布
网站发布步骤: 这部分是转载文章 在此标明出处,以前有文章是转的没标明的请谅解,因为有些已经无法找到出处,或者与其它原因. 如有冒犯请联系本人,或删除,或标明出处. 因为好的文章,以前只想收藏,但连接 ...
- 转 java List 与ArrasyList 区别
转 List是一个接口,而ListArray是一个类, ListArray继承并实现了List. 为什么要用 List list = new ArrayList() ,而不用 ArrayList al ...