嘟嘟嘟
这是一道splay基础题。
最坑的一点是,因为有些节点可能没有左儿子或右儿子,所以必须把t[0].Max赋成-INF!
因为这个调了半天,看来回头复习复习splay是对的……
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 5e4 + 5;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n, m, a[maxn];
struct Tree
{
int ch[2], fa;
int val, siz, Max;
int add, rev;
}t[maxn];
int root, tcnt = 0;
In void _PrintTr(int now)
{
if(!now) return;
printf("now:%d val:%d Max:%d ls:%d rs:%d
", now, t[now].val, t[now].Max, t[now].ch[0], t[now].ch[1]);
_PrintTr(t[now].ch[0]); _PrintTr(t[now].ch[1]);
}
void _Travel(int now, int dep)
{
if(t[now].ch[1]) _Travel(t[now].ch[1], dep + 1);
for(int i = 1; i < dep; ++i) putchar(' ');
printf(" 33[36m%d %d 33[0m
", t[now].val, t[now].Max);
if(t[now].ch[0]) _Travel(t[now].ch[0], dep + 1);
}
In void c_rev(int now)
{
swap(t[now].ch[0], t[now].ch[1]);
t[now].rev ^= 1;
}
In void c_add(int now, int lzy)
{
t[now].val += lzy; t[now].Max += lzy;
t[now].add += lzy;
}
In void pushdown(int now)
{
if(t[now].rev)
{
if(t[now].ch[0]) c_rev(t[now].ch[0]);
if(t[now].ch[1]) c_rev(t[now].ch[1]);
t[now].rev = 0;
}
if(t[now].add)
{
if(t[now].ch[0]) c_add(t[now].ch[0], t[now].add);
if(t[now].ch[1]) c_add(t[now].ch[1], t[now].add);
t[now].add = 0;
}
}
In void pushup(int now)
{
t[now].siz = t[t[now].ch[0]].siz + t[t[now].ch[1]].siz + 1;
t[now].Max = max(max(t[t[now].ch[0]].Max, t[t[now].ch[1]].Max), t[now].val);
}
In void rotate(int x)
{
int y = t[x].fa, z = t[y].fa, k = (t[y].ch[1] == x);
t[z].ch[t[z].ch[1] == y] = x; t[x].fa = z;
t[y].ch[k] = t[x].ch[k ^ 1]; t[t[x].ch[k ^ 1]].fa = y;
t[x].ch[k ^ 1] = y; t[y].fa = x;
pushup(y), pushup(x);
}
In void splay(int x, int s)
{
while(t[x].fa != s)
{
int y = t[x].fa, z = t[y].fa;
if(z != s) rotate(((t[y].ch[0] == x) ^ (t[z].ch[0] == y)) ? x : y);
rotate(x);
}
if(!s) root = x;
}
In int build(int L, int R, int _f)
{
if(L > R) return 0;
int mid = (L + R) >> 1, now = ++tcnt;
t[now].fa = _f;
t[now].Max = t[now].val = a[mid];
t[now].ch[0] = build(L, mid - 1, now);
t[now].ch[1] = build(mid + 1, R, now);
pushup(now);
return now;
}
In int getRank(int k)
{
int now = root;
while(1)
{
pushdown(now);
if(t[t[now].ch[0]].siz >= k) now = t[now].ch[0];
else if(t[t[now].ch[0]].siz + 1 == k) return now;
else k -= (t[t[now].ch[0]].siz + 1), now = t[now].ch[1];
}
}
In void split(int L, int R)
{
int a = getRank(L - 1), b = getRank(R + 1);
splay(a, 0); splay(b, a);
pushdown(root), pushdown(t[root].ch[1]);
}
int main()
{
n = read(); m = read();
a[1] = -INF; a[n + 2] = -INF;
root = build(1, n + 2, 0);
t[0].Max = -INF;
for(int i = 1; i <= m; ++i)
{
int op = read(), L = read() + 1, R = read() + 1, v;
split(L, R);
if(op == 1) v = read(), c_add(t[t[root].ch[1]].ch[0], v), pushup(t[root].ch[1]), pushup(root);
else if(op == 2) c_rev(t[t[root].ch[1]].ch[0]), pushup(t[root].ch[1]), pushup(root);
else write(t[t[t[root].ch[1]].ch[0]].Max), enter;
}
return 0;
}