_bzoj1500 [NOI2005]维修数列【真·Splay】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1500
注意MAX-SUM的时候,不可以是空串。
#include <cstdio>
#include <algorithm> const int maxn = 500005; int n, m, top, a[maxn], posi, tot, cc;
char opr[15];
struct Node {
Node * ch[2];
Node * fa;
int key, sm, mxl, mxr, mxs, siz, c, mx;
char rev;
Node(void);
int getsm(void) {
return c == -666666? sm: c * siz;
}
int getmxl(void) {
if (c == -666666) {
return rev? mxr: mxl;
}
if (c > 0) {
return c * siz;
}
return 0;
}
int getmxr(void) {
if (c == -666666) {
return rev? mxl: mxr;
}
if (c > 0) {
return c * siz;
}
return 0;
}
int getmxs(void) {
if (c == -666666) {
return mxs;
}
if (c > 0) {
return c * siz;
}
return 0;
}
int getmx(void) {
return c == -666666? mx: c;
}
} *nil, *root, *tnode;
Node * stk[maxn]; Node::Node(void) {
ch[0] = ch[1] = fa = nil;
key = sm = mxl = mxr = mxs = siz = rev = 0;
mx = c = -666666;
}
inline void pushup(Node * x) {
x->siz = x->ch[0]->siz + x->ch[1]->siz + 1;
x->sm = x->ch[0]->getsm() + x->ch[1]->getsm() + x->key;
x->mxl = std::max(x->ch[0]->getmxl(), x->ch[0]->getsm() + x->key + x->ch[1]->getmxl());
x->mxr = std::max(x->ch[1]->getmxr(), x->ch[1]->getsm() + x->key + x->ch[0]->getmxr());
x->mxs = std::max(x->ch[0]->getmxs(), x->ch[1]->getmxs());
x->mxs = std::max(x->mxs, x->ch[0]->getmxr() + x->key + x->ch[1]->getmxl());
x->mx = std::max(x->ch[0]->getmx(), x->ch[1]->getmx());
x->mx = std::max(x->mx, x->key);
}
inline void pushdown(Node * x) {
if (x->c != -666666) {
x->ch[0]->c = x->ch[1]->c = x->c;
x->key = x->c;
x->sm = x->c * x->siz;
if (x->c > 0) {
x->mxl = x->mxr = x->mxs = x->sm;
}
else {
x->mxl = x->mxr = x->mxs = 0;
}
x->c = -666666;
}
if (x->rev) {
x->rev = 0;
x->ch[0]->rev ^= 1;
x->ch[1]->rev ^= 1;
std::swap(x->ch[0], x->ch[1]);
}
}
inline void rotate(Node* x) {
Node *y = x->fa;
if (y == y->fa->ch[0]) {
y->fa->ch[0] = x;
}
else {
y->fa->ch[1] = x;
}
x->fa = y->fa;
int dir = x == y->ch[1];
y->ch[dir] = x->ch[dir ^ 1];
x->ch[dir ^ 1]->fa = y;
x->ch[dir ^ 1] = y;
y->fa = x;
pushup(y);
pushup(x);
}
inline void splay(Node * x, Node * rt) {
Node * p;
char flag1, flag2;
top = 0;
for (Node * i = x; i != rt; i = i->fa) {
stk[top++] = i;
}
for (int i = top - 1; ~i; --i) {
pushdown(stk[i]);
}
while (x->fa != rt) {
p = x->fa;
if (p->fa == rt) {
rotate(x);
}
else {
flag1 = p == p->fa->ch[1];
flag2 = x == p->ch[1];
if (flag1 ^ flag2) {
rotate(x);
}
else {
rotate(p);
}
rotate(x);
}
}
if (rt == nil) {
root = x;
}
}
inline void modify(int pos1, int pos2) {
Node * x = root;
pushdown(x);
while (pos1 != x->ch[0]->siz + 1) {
if (pos1 <= x->ch[0]->siz) {
x = x->ch[0];
}
else {
pos1 -= x->ch[0]->siz + 1;
x = x->ch[1];
}
pushdown(x);
}
splay(x, nil);
x = root;
pushdown(x);
while (pos2 != x->ch[0]->siz + 1) {
if (pos2 <= x->ch[0]->siz) {
x = x->ch[0];
}
else {
pos2 -= x->ch[0]->siz + 1;
x = x->ch[1];
}
pushdown(x);
}
splay(x, root);
}
inline Node* ist(int left, int right) {
if (left > right) {
return nil;
}
Node * rt = new Node, *t;
int mid = (left + right) >> 1;
rt->key = a[mid];
t = ist(left, mid - 1);
t->fa = rt;
rt->ch[0] = t;
t = ist(mid + 1, right);
t->fa = rt;
rt->ch[1] = t;
pushup(rt);
return rt;
}
void clear(Node * x) {
if (x == nil) {
return;
}
clear(x->ch[0]);
clear(x->ch[1]);
delete x;
} int main(void) {
//freopen("in.txt", "r", stdin);
nil = new Node;
root = nil;
scanf("%d%d", &n, &m);
for (int i = 2; i <= n + 1; ++i) {
scanf("%d", a + i);
}
root = ist(1, n = n + 2);
while (m--) {
scanf("%s", opr);
if (opr[2] == 'S') { // INSERT
scanf("%d%d", &posi, &tot);
n += tot;
++posi;
modify(posi, posi + 1);
for (int i = 1; i <= tot; ++i) {
scanf("%d", a + i);
}
tnode = ist(1, tot);
tnode->fa = root->ch[1];
root->ch[1]->ch[0] = tnode;
pushup(root->ch[1]);
pushup(root);
}
else if (opr[2] == 'L') { // DELETE
scanf("%d%d", &posi, &tot);
n -= tot;
++posi;
modify(posi - 1, posi + tot);
clear(root->ch[1]->ch[0]);
root->ch[1]->ch[0] = nil;
pushup(root->ch[1]);
pushup(root);
}
else if (opr[2] == 'K') { // MAKE-SAME
scanf("%d%d%d", &posi, &tot, &cc);
++posi;
modify(posi - 1, posi + tot);
root->ch[1]->ch[0]->c = cc;
pushup(root->ch[1]);
pushup(root);
}
else if (opr[2] == 'V') { // REVERSE
scanf("%d%d", &posi, &tot);
++posi;
modify(posi - 1, posi + tot);
root->ch[1]->ch[0]->rev ^= 1;
pushup(root->ch[1]);
pushup(root);
}
else if (opr[2] == 'T') { // GET-SUM
scanf("%d%d", &posi, &tot);
++posi;
modify(posi - 1, posi + tot);
printf("%d\n", root->ch[1]->ch[0]->getsm());
}
else { // MAX-SUM
modify(1, n);
printf("%d\n", root->ch[1]->ch[0]->getmx() > 0? root->getmxs(): root->ch[1]->ch[0]->getmx());
}
}
return 0;
}
_bzoj1500 [NOI2005]维修数列【真·Splay】的更多相关文章
- 【BZOJ】1500: [NOI2005]维修数列(splay+变态题)
http://www.lydsy.com/JudgeOnline/problem.php?id=1500 模板不打熟你确定考场上调试得出来? 首先有非常多的坑点...我遇到的第一个就是,如何pushu ...
- NOI2005维修数列(splay)
题目描述: Description 请写一个程序,要求维护一个数列,支持以下 6 种操作: 请注意,格式栏 中的下划线‘ _ ’表示实际输入文件中的空格 Input 输入的第1 行包含两个数N 和M( ...
- bzoj 1500: [NOI2005]维修数列 splay
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 6556 Solved: 1963[Submit][Status ...
- 【BZOJ1500】【NOI2005】维修数列(Splay)
[BZOJ1500][NOI2005]维修数列(Splay) 题面 不想再看见这种毒瘤题,自己去BZOJ看 题解 Splay良心模板题 真的很简单 我一言不发 #include<iostream ...
- [BZOJ1500][NOI2005]维修数列 解题报告 Splay
Portal Gun:[BZOJ1500][NOI2005]维修数列 有一段时间没写博客了,最近在刚数据结构......各种板子背得简直要起飞,题目也是一大堆做不完,这里就挑一道平衡树的题来写写好了 ...
- BZOJ 1500: [NOI2005]维修数列 (splay tree)
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 4229 Solved: 1283[Submit][Status ...
- 【BZOJ1500】[NOI2005]维修数列 Splay
[BZOJ1500][NOI2005]维修数列 Description Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行 ...
- [NOI2005] 维修数列
1500: [NOI2005]维修数列 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 8397 Solved: 2530 Description In ...
- [BZOJ1500][NOI2005]维修数列---解题报告
Portal Gun:[BZOJ1500][NOI2005]维修数列 有一段时间没写博客了,最近在刚数据结构......各种板子背得简直要起飞,题目也是一大堆做不完,这里就挑一道平衡树的题来写写好了 ...
随机推荐
- Node.js+Web TWAIN,实现Web文档扫描和图像上传
目录(?)[+] 通过Dynamic Web TWAIN SDK和Node.js的组合,只需要几行代码就可以实现在浏览器中控制扫描仪,获取图像后上传到远程服务器. 原文:Document Imag ...
- HDU 1017 A Mathematical Curiosity【看懂题意+穷举法】
//2014.10.17 01:19 //题意: //先输入一个数N,然后分块输入,每块输入每次2个数,n,m,直到n,m同一时候为零时 //结束,当a和b满足题目要求时那么这对a和b就是一组 ...
- INTERSECT(交集)集合运算
在集合论中,两个集合(记为集合A和B)的交集是由既属于A,也属于B的所有元素组成的集合. 在T-SQL 中,INTERSECT 集合运算对两个输入查询的结果集取其交集,只返回在两个查询结果集中都出现的 ...
- [转] When to use what language and why
Published by Seraphimsan Oct 13, 2010 (last update: Oct 14, 2010) When to use what language and why ...
- 解决Struts配置文件里无提示信息的问题
(1)在struts2配置文件编写的时候.有可能无法提示所有信息,在配置文件里打个"<" 后,并没有不论什么的提示信息(使用快捷键Alt+/ 也不提示) 原因是下边的 &q ...
- 嵌入式开发之davinci---dm8127 ipipe
http://blog.csdn.net/dog0138/article/details/4212576 http://e2e.ti.com/support/dsp/davinci_digital_m ...
- Linux常用服务安装部署
1,centos7默认是装有python的,检查python版本的命令 # 检查python版本 : python -V 2,centOS在安装python3以及tab补全功能 下载python3源码 ...
- 相关性系数缺点与证明 k阶矩
相关性系数 https://baike.baidu.com/item/相关系数/3109424?fr=aladdin 缺点 需要指出的是,相关系数有一个明显的缺点,即它接近于1的程度与数据组数n相关, ...
- handsontable整理
hansontable简介 hansontable是一个在线类似Excel的表格编辑器,支持丰富的展现和交互,有多样的单元格类型供配置. 核心是由原生JavaScript构建,充分模块化,支持自定义b ...
- YTU 2412: 帮警长数一数【循环、分支简单综合】
2412: 帮警长数一数[循环.分支简单综合] 时间限制: 1 Sec 内存限制: 64 MB 提交: 323 解决: 169 题目描述 黑猫警长在犯罪现场发现了一些血迹,现已经委托检验机构确定了 ...