[NOI2005]维护数列

[NOI2005]维护数列

嘟嘟嘟


这题我写的时候还是挺顺的,边写边想为啥学姐说我是“勇士”。然后我用了大半天的debug时间理解了这句话……


先不说那几个把人坑到退役的点,光说这几个操作,其实都听基础的。
我感觉唯一要说一下的就是插入一串数:我们先把这些数建成一个splay,然后把这个splay的根节点连到对应的点的儿子节点即可。


然后有几个地方一定要注意:
1.这道题需要垃圾回收,就是重复利用删除的节点编号。对于删除的子树,遍历一下,用栈把编号存下来即可。
2.建树的时候一定要把标记清零,因为我们是重复利用节点。要不然就在垃圾回收的时候清零也行。
3.关于区间覆盖标记,不能将他设为要修改的值,因为可能会修改成0。导致这种情况会被判断成没有修改。因此这个标记开一个bool变量,1表示修改了,0表示没修改。


最后就是那个坑点了:pos后连续的k个字符,k可能是0!!因此这个必须特判掉,而且面对询问不能直接continue,而是得固输0……

#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 = 2e6 + 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];
char c[20];
struct Tree
{
  int ch[2], fa;
  int siz, val, sum, lmax, rmax, imax;
  int rev; bool add;
}t[maxn];
int root, tcnt = 0;
int st[maxn], top = 0;
#define ls t[now].ch[0]  //only for now!!!
#define rs t[now].ch[1]

In void _PrintTr(int now)
{
  if(!now) return;
  printf("no:%d val:%d ls:%d rs:%d
", now, t[now].val, t[ls].val, t[rs].val);
  _PrintTr(ls); _PrintTr(rs);
}

In void c_rev(int now)
{
  swap(t[now].lmax, t[now].rmax);
  swap(ls, rs); t[now].rev ^= 1;
}
In void c_cg(int now, int lzy)
{
  t[now].sum = t[now].siz * lzy;
  t[now].val = lzy; t[now].add = 1;
  t[now].lmax = t[now].rmax = max(0, t[now].sum);
  t[now].imax = max(t[now].sum, lzy);
}
In void pushdown(int now)
{ 
  if(t[now].add)
    {
      if(ls) c_cg(ls, t[now].val);
      if(rs) c_cg(rs, t[now].val);
      t[now].add = t[now].rev = 0;
    }
  if(t[now].rev)
    {
      if(ls) c_rev(ls);
      if(rs) c_rev(rs);
      t[now].rev = 0;
    }
}
In void pushup(int now)
{
  t[now].siz = t[ls].siz + t[rs].siz + 1;
  t[now].sum = t[ls].sum + t[rs].sum + t[now].val;
  t[now].lmax = max(t[ls].lmax, t[ls].sum + t[now].val + t[rs].lmax);
  t[now].rmax = max(t[rs].rmax, t[rs].sum + t[now].val + t[ls].rmax);
  t[now].imax = max(max(t[ls].imax, t[rs].imax), t[ls].rmax + t[now].val + t[rs].lmax);
}
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[y].ch[k]].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 = top ? st[top--] : ++tcnt;
  t[now].fa = _f;
  t[now].siz = 1;
  t[now].val = t[now].sum = t[now].imax = a[mid];
  t[now].lmax = t[now].rmax = max(0, a[mid]);
  t[now].add = t[now].rev = 0;  //must!!
  t[now].ch[0] = build(L, mid - 1, now);
  t[now].ch[1] = build(mid + 1, R, now);
  pushup(now);
  return now;
}
In int Find(int k)
{
  int now = root;
  while(1)
    {
      pushdown(now);
      if(t[ls].siz >= k) now = ls;
      else if(t[ls].siz + 1 == k) return now;
      else k -= (t[ls].siz + 1), now = rs;
    }
}
In int split(int L, int R)
{
  int a = Find(L), b = Find(R + 2);
  splay(a, 0); splay(b, a);
  pushdown(root); pushdown(t[root].ch[1]);
  return t[root].ch[1];
}
In void update_in(int L, int R)	    //all plus ONE  !!
{
  for(int i = 1; i <= R - L + 1; ++i) a[i] = read();
  int tp = build(1, R - L + 1, 0);
  int a = Find(L), b = Find(L + 1);
  splay(a, 0); splay(b, a);
  int now = t[root].ch[1];
  t[now].ch[0] = tp; t[tp].fa = now;
  pushup(now); pushup(root);
}
In void rec(int& now)
{
  if(!now) return;
  rec(ls); rec(rs);
  st[++top] = now;
}
In void del(int L, int R)
{
  int now = split(L, R);
  rec(t[now].ch[0]);
  t[now].ch[0] = t[t[now].ch[0]].fa = 0;
  pushup(now); pushup(root);
}
In void update_cg(int L, int R, int d)
{
  int now = split(L, R);
  c_cg(t[now].ch[0], d);
  pushup(now); pushup(root);
}
In void update_rev(int L, int R)
{
  int now = split(L, R);
  c_rev(t[now].ch[0]);
  pushup(now); pushup(root);
}
In int query_sum(int L, int R)
{
  int now = split(L, R);
  return t[t[now].ch[0]].sum;
}

int main()
{
  n = read(); m = read();
  t[0].imax = a[1] = a[n + 2] = -INF;
  for(int i = 2; i <= n + 1; ++i) a[i] = read();
  root = build(1, n + 2, 0);
  for(int i = 1; i <= m; ++i)
    {
      scanf("%s", c);
      if(c[0] == 'I')
	{
	  int pos = read(), k = read();
	  if(!k) continue;
	  update_in(pos + 1, pos + k);
	}
      else if(c[0] == 'D')
	{
	  int pos = read(), k = read();
	  if(!k) continue;
	  del(pos, pos + k - 1);
	}
      else if(c[2] == 'K')
	{
	  int pos = read(), k = read(), d = read();
	  if(!k) continue;
	  update_cg(pos, pos + k - 1, d);
	}
      else if(c[0] == 'R')
	{
	  int pos = read(), k = read();
	  if(!k) continue;
	  update_rev(pos, pos + k - 1);
	}
      else if(c[0] == 'G')
	{
	  int pos = read(), k = read();
	  if(!k) puts("0");
	  else write(query_sum(pos, pos + k - 1)), enter;
	}
      else write(t[root].imax), enter;
    }
  return 0;
}