1251: 序列终结者
Time Limit: 20 Sec Memory Limit: 162 MB
Description
网上有许多题,就是给定一个序列,要你支持几种操作:A、B、C、D。一看另一道题,又是一个序列 要支持几种操作:D、C、B、A。尤其是我们这里的某人,出模拟试题,居然还出了一道这样的,真是没技术含量……这样 我也出一道题,我出这一道的目的是为了让大家以后做这种题目有一个“库”可以依靠,没有什么其他的意思。这道题目 就叫序列终结者吧。 【问题描述】 给定一个长度为N的序列,每个序列的元素是一个整数(废话)。要支持以下三种操作: 1. 将[L,R]这个区间内的所有数加上V。 2. 将[L,R]这个区间翻转,比如1 2 3 4变成4 3 2 1。 3. 求[L,R]这个区间中的最大值。 最开始所有元素都是0。
Input
第一行两个整数N,M。M为操作个数。 以下M行,每行最多四个整数,依次为K,L,R,V。K表示是第几种操作,如果不是第1种操作则K后面只有两个数。
Output
对于每个第3种操作,给出正确的回答。
Sample Input
4 4
1 1 3 2
1 2 4 -1
2 1 3
3 2 4
Sample Output
2
【数据范围】
N<=50000,M<=100000。
题解
写了很久的splay练习题。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define nd(x) (nil + (x))
using namespace std;
const int N = 50000 + 10, inf = 0x7fffffff;
struct Node{
int a, tag, siz, rev, mx, id;
Node *c[2], *f;
int d() { return f->c[1] == this; }
int sc(Node *x, int d) { (c[d] = x) -> f = this; }
void add(int x) { tag += x; a += x; mx += x; }
void pup() { siz = c[0]->siz + c[1]->siz + 1; mx = max(a, max(c[0]->mx, c[1]->mx)); }
void pdw();
} nil[N], *root;
void Node::pdw(){
if(tag){
if(c[0] != nil) c[0]->add(tag);
if(c[1] != nil) c[1]->add(tag);
tag = 0;
}
if(rev){
c[0]->rev ^= 1;
c[1]->rev ^= 1;
swap(c[0], c[1]);
rev = 0;
}
}
int tot, n, m;
void outit(Node *p){
if(p == nil) return;
p->pdw();
printf("%d %d %d %d %d %d\n", p->id, p->c[0]->id, p->c[1]->id, p->siz, p->a, p->mx);
outit(p->c[0]); outit(p->c[1]);
}
void rotate(Node *x, Node *&k){
int d = x->d();
Node* p = x->f;
p->sc(x->c[!d], d);
if(p == k){
x->f = k->f;
k = x;
}
else p->f->sc(x, p->d());
x->sc(p, !d);
p->pup(); x->pup();
}
void splay(Node *x, Node *&k){
for(Node *y; x != k;){
if((y = x->f) != k) y->f->pdw();
y->pdw(), x->pdw();
if(y != k) (x->d() ^ y->d()) ? rotate(x, k) : rotate(y, k);
rotate(x, k);
}
x->pup();
}
Node* select(int k){
Node *p = root;
while(true){
p->pdw();
int t = p->c[0]->siz;
if(t >= k) p = p->c[0];
else if(t + 1 < k) k -= t + 1, p = p->c[1];
else break;
}
return p;
}
void build(int l, int r, Node *f, int d){
if(l > r){
f->c[d] = nil;
return;
}
if(l == r){
Node *u = nd(l);
u->id = l; u->f = f;
u->siz = 1; u->c[0] = u->c[1] = nil;
f->c[d] = u;
return;
}
int mid = (l + r) / 2;
Node *u = nd(mid);
build(l, mid-1, u, 0);
build(mid+1, r, u, 1);
u->id = l; u->f = f; u->id = mid; u->pup();
f->c[d] = u;
}
void update(int l, int r, int val){
Node *u = select(l-1), *v = select(r+1);
splay(u, root); splay(v, root->c[1]);
Node *w = v->c[0];
w->add(val);
}
void rever(int l, int r){
Node *u = select(l-1), *v = select(r+1);
splay(u, root); splay(v, root->c[1]);
Node *w = v->c[0];
w->rev ^= 1;
}
void query(int l, int r){
Node *u = select(l-1), *v = select(r+1);
splay(u, root); splay(v, root->c[1]);
Node *w = v->c[0];
printf("%d\n", w->mx);
}
inline void in(int &x);
void work(){
scanf("%d%d", &n, &m);
nil->c[0] = nil->c[1] = nil->f = nil;
nil->a = nil->mx = -inf;
build(1, n+2, nil, 0);
root = nd((n+3)/2);
int f, l, r, v;
while(m--){
in(f);
switch(f){
case 1: in(l); in(r); in(v); update(l+1, r+1, v); break;
case 2: in(l); in(r); rever(l+1, r+1); break;
case 3: in(l); in(r); query(l+1, r+1); break;
}
}
}
int main(){
freopen("bzoj1251.in", "r", stdin);
freopen("bzoj1251.out", "w", stdout);
work();
return 0;
}
inline void in(int &x){
x = 0; char c = getchar(); int f = 1;
while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }
while(c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); }
x *= f;
}