SP11470 TTM - To the moon

嘟嘟嘟


主席树+区间修改。


以为是水题,写着写着发现区间修改标记下传会出问题,然后想了想发现以前做的只是单点修改。
那怎么办咧?
然后题解交了我标记永久化这个神奇的东西。
特别好理解,就是修改的时候直接把多的就加到这个区间上,直到找到区间满足l == L && r == R,这时候再打个标记。然后查询的时候每一次应该在加上lzy[now] * (R - L + 1)就吼了!
这么看来还是一个水题

#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 = 4e6 + 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');
}

char s[2];
int n, m;
struct Tree
{
  int ls, rs;
  ll sum, lzy;
}t[maxn];
int root[maxn], tcnt = 0, tim = 0;
In void build(int& now, int L, int R)
{
  now = ++tcnt;
  if(L == R) {t[now].sum = read(); return;}
  int mid = (L + R) >> 1;
  build(t[now].ls, L, mid);
  build(t[now].rs, mid + 1, R);
  t[now].sum = t[t[now].ls].sum + t[t[now].rs].sum;
}
In void update(int old, int& now, int l, int r, int L, int R, ll d)
{
  t[now = ++tcnt] = t[old];
  t[now].sum += d * (R - L + 1);
  if(l == L && r == R) {t[now].lzy += d; return;}
  int mid = (l + r) >> 1;
  if(R <= mid) update(t[old].ls, t[now].ls, l, mid, L, R, d);
  else if(L > mid) update(t[old].rs, t[now].rs, mid + 1, r, L, R, d);
  else update(t[old].ls, t[now].ls, l, mid, L, mid, d), update(t[old].rs, t[now].rs, mid + 1, r, mid + 1, R, d);
}
In ll query(int now, int l, int r, int L, int R)
{
  if(l == L && r == R) return t[now].sum;
  int mid = (l + r) >> 1;
  ll ret = t[now].lzy * (R - L + 1);
  if(R <= mid) ret += query(t[now].ls, l, mid, L, R);
  else if(L > mid) ret += query(t[now].rs, mid + 1, r, L, R);
  else ret += query(t[now].ls, l, mid, L, mid) + query(t[now].rs, mid + 1, r, mid + 1, R);
  return ret;
}

int main()
{
  n = read(), m = read();
  build(root[0], 1, n);
  for(int i = 1; i <= m; ++i)
    {
      scanf("%s", s);
      if(s[0] == 'C')
	{
	  int L = read(), R = read(), d = read();
	  ++tim;
	  update(root[tim - 1], root[tim], 1, n, L, R, d);
	}
      else if(s[0] == 'Q')
	{
	  int L = read(), R = read();
	  write(query(root[tim], 1, n, L, R)), enter;
	}
      else if(s[0] == 'H')
	{
	  int L = read(), R = read(), t = read();
	  write(query(root[t], 1, n, L, R)), enter;
	}
      else
	{
	  int t = read();
	  if(t ^ tim) tim = t, tcnt = root[tim + 1] - 1;
	  //这么写算是垃圾回收
	}
    }
  return 0;
}
posted @ 2019-04-26 09:40  mrclr  阅读(111)  评论(0编辑  收藏  举报