1500: [NOI2005]维修数列
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 14759 Solved: 4832
[ Submit][ Status][ Discuss]
Description
Input
输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目。
第2行包含N个数字,描述初始时的数列。
以下M行,每行一条命令,格式参见问题描述中的表格。
任何时刻数列中最多含有500 000个数,数列中任何一个数字均在[-1 000, 1 000]内。
插入的数字总数不超过4 000 000个,输入文件大小不超过20MBytes。
Output
对于输入数据中的GET-SUM和MAX-SUM操作,向输出文件依次打印结果,每个答案(数字)占一行。
Sample Input
9 8
2 -6 3 5 1 -5 -3 6 3
GET-SUM 5 4
MAX-SUM
INSERT 8 3 -5 7 2
DELETE 12 1
MAKE-SAME 3 3 2
REVERSE 3 6
GET-SUM 5 4
MAX-SUM
2 -6 3 5 1 -5 -3 6 3
GET-SUM 5 4
MAX-SUM
INSERT 8 3 -5 7 2
DELETE 12 1
MAKE-SAME 3 3 2
REVERSE 3 6
GET-SUM 5 4
MAX-SUM
Sample Output
-1
10
1
10
10
1
10
HINT
Source
最全的一个splay区间操作模板,写了这个之后其他基本都过了。。zzy(张子韫)大神居然用这道题打模板,还是用指针写的orz
区间操作老套路,把l-1拧到根,r+1拧到根的右结点,然后显然l~r就都在r+1的左子树里,插入的就插入一棵完美二叉树,删除的就删除,然后要打两个标记(rev和tag)
空间会爆,要手开内存池(就是把删除节点编号扔到一个栈里,新加结点的时候若栈不为空直接把栈顶的编号给这个新结点)
其他就没什么了,就是比较烦吧
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
#define ls(p) t[p].ch[0]
#define rs(p) t[p].ch[1]
#define maxn 550000
#define inf 0x3f3f3f3f
using namespace std;
int read()
{
char ch = getchar(), pre = '0'; int x = 0, f = 1;
while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}
char readchar()
{
char ch = getchar();
while(ch < 'A' || ch > 'Z') ch = getchar();
if(ch == 'M') {ch = getchar(); ch = getchar();}
char pre = getchar(); pre = getchar(); pre = getchar(); pre = getchar();
return ch;
}
int a[maxn], n, m;
struct node {
int size, sum, mx, lm, rm, v;
int tag, rev;
int f, ch[2];
node() {}
node(int val) {ch[0] = ch[1] = tag = rev = f = 0; size = 1; v = sum = mx = lm = rm = val;}
}t[maxn]; int st[maxn], sz, top, root;
int newnode() {return top ? st[top--] : ++sz;}
int wh(int p) {return t[t[p].f].ch[1] == p;}
void rever(int p)
{
if(t[p].tag) return;
t[p].rev ^= 1;
swap(ls(p), rs(p)); swap(t[p].lm, t[p].rm);
}
void paint(int p, int val)
{
t[p].tag = 1; t[p].v = val;
t[p].sum = val * t[p].size;
t[p].lm = t[p].rm = t[p].mx = max(val, t[p].sum);
t[p].rev = 0;
}
void pushdown(int p)
{
if(t[p].rev)
{
if(ls(p)) rever(ls(p));
if(rs(p)) rever(rs(p));
t[p].rev = 0;
}
if(t[p].tag)
{
if(ls(p)) paint(ls(p), t[p].v);
if(rs(p)) paint(rs(p), t[p].v);
t[p].tag = 0;
}
}
void update(int p)
{
t[p].size = t[ls(p)].size + t[rs(p)].size + 1;
t[p].sum = t[ls(p)].sum + t[rs(p)].sum + t[p].v;
t[p].mx = max(max(t[ls(p)].mx, t[rs(p)].mx), max(0, t[ls(p)].rm) + t[p].v + max(0, t[rs(p)].lm));
t[p].lm = max(t[ls(p)].lm, t[ls(p)].sum + t[p].v + max(0, t[rs(p)].lm));
t[p].rm = max(t[rs(p)].rm, t[rs(p)].sum + t[p].v + max(0, t[ls(p)].rm));
}
void rotate(int p)
{
int f = t[p].f, g = t[f].f, c = wh(p);
if(g) t[g].ch[wh(f)] = p; t[p].f = g;
t[f].ch[c] = t[p].ch[c ^ 1]; if(t[f].ch[c]) t[t[f].ch[c]].f = f;
t[p].ch[c ^ 1] = f; t[f].f = p;
update(f); update(p);
}
void Splay(int p, int cur)
{
for(;t[p].f != cur; rotate(p))
if(t[t[p].f].f != cur) rotate(wh(p) == wh(t[p].f) ? t[p].f : p);
if(cur == 0) root = p;
}
void build_tree(int &p, int L, int R, int fa)
{
int mid = L + R >> 1;
p = newnode(); t[p] = node(a[mid]); t[p].f = fa;
if(L == R) return;
if(L < mid) build_tree(ls(p), L, mid - 1, p);
if(mid < R) build_tree(rs(p), mid + 1, R, p);
update(p);
}
int Kth(int k)
{
int p = root, lsize = 0;
while(p)
{
pushdown(p);
int cur = lsize + t[ls(p)].size;
if(k == cur + 1) return p;
if(k <= cur) p = ls(p);
else {
lsize = cur + 1;
p = rs(p);
}
}
return -1;
}
void Ins(int k, int tot)
{
for(int i = 1;i <= tot; ++i) a[i] = read();
int f = Kth(k + 1); Splay(f, 0);
int p = Kth(k + 2); Splay(p, f);
build_tree(ls(p), 1, tot, p);
update(p); update(f);
}
void erase(int p)
{
if(!p) return;
st[++top] = p;
erase(ls(p)); erase(rs(p));
}
void Del(int k, int tot)
{
int f = Kth(k); Splay(f, 0);
int p = Kth(k + tot + 1); Splay(p, f);
erase(ls(p)); ls(p) = 0;
update(p); update(f);
}
void Mak(int k, int tot)
{
int f = Kth(k); Splay(f, 0);
int p = Kth(k + tot + 1); Splay(p, f);
paint(ls(p), read());
update(p); update(f);
}
void Rev(int k, int tot)
{
int f = Kth(k); Splay(f, 0);
int p = Kth(k + tot + 1); Splay(p, f);
rever(ls(p));
update(p); update(f);
}
int Sum(int k, int tot)
{
int f = Kth(k); Splay(f, 0);
int p = Kth(k + tot + 1); Splay(p, f);
return t[ls(p)].sum;
}
void init()
{
n = read(); m = read();
for(int i = 2;i <= n + 1; ++i) a[i] = read(); a[1] = a[n + 2] = -inf;
t[0] = node(-inf); t[0].sum = t[0].size = 0;
build_tree(root, 1, n + 2, 0);
}
void solve()
{
for(int i = 1;i <= m; ++i)
{
char ch = readchar();
if(ch == 'X') printf("%d\n", t[root].mx);
else
{
int k = read(), tot = read();
if(ch == 'I') Ins(k, tot);
if(ch == 'D') Del(k, tot);
if(ch == 'K') Mak(k, tot);
if(ch == 'R') Rev(k, tot);
if(ch == 'G') printf("%d\n", Sum(k, tot));
}
}
}
int main()
{
init();
solve();
return 0;
}