BZOJ #2989. 数列 [树套树]

考虑转化问题模型,这个没必要可持久化,直接加点就可以了,还不用删点
每次的问题是求 曼哈顿距离,变成切比雪夫距离然后求解
然后我们考虑将这玩意旋转 45度, 然后原坐标的 \((x,y)\) 会变成 \((\frac{x{-}y}{\sqrt 2} , \frac{x+y}{\sqrt 2})\)
发现 \(\sqrt 2\) 是可以到最后抵消掉的,就……没了?
暴力树套树就过了啊

随便抓两个图
BZOJ #2989. 数列 [树套树]_第1张图片
BZOJ #2989. 数列 [树套树]_第2张图片

// powered by c++11
// by Isaunoya
#include 
#define rep(i, x, y) for (register int i = (x); i <= (y); ++i)
#define Rep(i, x, y) for (register int i = (x); i >= (y); --i)
using namespace std;
using db = double;
using ll = long long;
using uint = unsigned int;
// #define int long long
using pii = pair;
#define ve vector
#define Tp template
#define all(v) v.begin(), v.end()
#define sz(v) ((int)v.size())
#define pb emplace_back
#define fir first
#define sec second
// the cmin && cmax
Tp void cmax(T& x, const T& y) {
  if (x < y) x = y;
}
Tp void cmin(T& x, const T& y) {
  if (x > y) x = y;
}
// sort , unique , reverse
Tp void sort(ve& v) { sort(all(v)); }
Tp void unique(ve& v) {
  sort(all(v));
  v.erase(unique(all(v)), v.end());
}
Tp void reverse(ve& v) { reverse(all(v)); }
const int SZ = 0x191981;
struct FILEIN {
  ~FILEIN() {}
  char qwq[SZ], *S = qwq, *T = qwq, ch;
  char GETC() { return (S == T) && (T = (S = qwq) + fread(qwq, 1, SZ, stdin), S == T) ? EOF : *S++; }
  FILEIN& operator>>(char& c) {
    while (isspace(c = GETC()))
      ;
    return *this;
  }
  FILEIN& operator>>(string& s) {
    while (isspace(ch = GETC()))
      ;
    s = ch;
    while (!isspace(ch = GETC())) s += ch;
    return *this;
  }
  Tp void read(T& x) {
    bool sign = 1;
    while ((ch = GETC()) < 0x30)
      if (ch == 0x2d) sign = 0;
    x = (ch ^ 0x30);
    while ((ch = GETC()) > 0x2f) x = x * 0xa + (ch ^ 0x30);
    x = sign ? x : -x;
  }
  FILEIN& operator>>(int& x) { return read(x), *this; }
  // FILEIN& operator>>(signed& x) { return read(x), *this; }
  FILEIN& operator>>(unsigned& x) { return read(x), *this; }
} in;
struct FILEOUT {
  const static int LIMIT = 0x114514;
  char quq[SZ], ST[0x114];
  signed sz, O;
  ~FILEOUT() { sz = O = 0; }
  void flush() {
    fwrite(quq, 1, O, stdout);
    fflush(stdout);
    O = 0;
  }
  FILEOUT& operator<<(char c) { return quq[O++] = c, *this; }
  FILEOUT& operator<<(string str) {
    if (O > LIMIT) flush();
    for (char c : str) quq[O++] = c;
    return *this;
  }
  Tp void write(T x) {
    if (O > LIMIT) flush();
    if (x < 0) {
      quq[O++] = 0x2d;
      x = -x;
    }
    do {
      ST[++sz] = x % 0xa ^ 0x30;
      x /= 0xa;
    } while (x);
    while (sz) quq[O++] = ST[sz--];
    return;
  }
  FILEOUT& operator<<(int x) { return write(x), *this; }
  // FILEOUT& operator<<(signed x) { return write(x), *this; }
  FILEOUT& operator<<(unsigned x) { return write(x), *this; }
} out;

int n, q;
const int maxn = 6e4 + 46;
int a[maxn];
const int maxm = 2e6 + 62;
const int maxp = 2e7 + 72;
int rt[maxm], ls[maxp], rs[maxp], sum[maxp];
int cnt = 0;
void upd(int& p, int l, int r, int x) {
  if (!p) p = ++cnt;
  sum[p]++;
  if (l == r) return;
  int mid = l + r >> 1;
  if (x <= mid)
    upd(ls[p], l, mid, x);
  else
    upd(rs[p], mid + 1, r, x);
}
int ql, qr;
int qry(int p, int l, int r) {
  if (!p) return 0;
  if (ql <= l && r <= qr) return sum[p];
  int mid = l + r >> 1, ans = 0;
  if (ql <= mid) ans = qry(ls[p], l, mid);
  if (qr > mid) ans += qry(rs[p], mid + 1, r);
  return ans;
}
const int up = 300000;
const int lim = 600000;
int low(int x) { return x & -x; }
void add(int x, int y) {
  for (; x <= lim; x += low(x)) upd(rt[x], 1, lim, y);
}
int qry(int x) {
  int ans = 0;
  for (; x; x ^= low(x)) ans += qry(rt[x], 1, lim);
  return ans;
}
signed main() {
#ifdef _WIN64
  freopen("testdata.in", "r", stdin);
#endif
  // code begin.
  in >> n >> q;
  rep(i, 1, n) { in >> a[i], add(i - a[i] + up, i + a[i] + up); }
  rep(i, 1, q) {
    string s;
    int x, y;
    in >> s >> x >> y;
    if (s == "Modify")
      add(x - y + up, x + y + up), a[x] = y;
    else {
      ql = x + a[x] - y + up;
      qr = x + a[x] + y + up;
      out << qry(x - a[x] + y + up) - qry(x - a[x] - y + up - 1) << '\n';
    }
  }
  return out.flush(), 0;
  // code end.
}

你可能感兴趣的:(BZOJ #2989. 数列 [树套树])