P3863 序列

· · 题解

一道挺不错的题目。

考虑我们将最终每个时间的序列写出来,容易发现这个东西形成了一个二维平面,如下图。

然后容易陷入一个误区,可能会去维护对于每个时刻的序列情况,然后直接回答。

但是其实可以考虑另一种方式,考虑查询和修改的情况。

你发现查询是查一个区间,但是修改是修改一个矩形。

所以可以直接考虑离线询问,然后扫描线扫序列,然后数据结构维护时间维。

然后对于一个修改,可以将他拆成两个形如 (l,t,val) 和 (r + 1,t,-val) 的形式。

然后就在扫到 l 的时候对维护的时间维上 t \sim q 的范围整体加 val 然后扫到 r + 1 时对 t \sim q 的范围整体加上 -val 消除贡献。

然后就是一个区间加区间查询排名问题,分块维护即可。

// 德丽莎你好可爱德丽莎你好可爱德丽莎你好可爱德丽莎你好可爱德丽莎你好可爱
// Problem: P3863 序列
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P3863
// Memory Limit: 500 MB
// Time Limit: 2000 ms
// The Author : Pitiless0514
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#define int long long
using namespace std;
namespace IO {
    int len = 0;
    char ibuf[(1 << 20) + 1], *iS, *iT, out[(1 << 25) + 1];
    #define gh()                                                                   \
      (iS == iT ? iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20) + 1, stdin),         \
       (iS == iT ? EOF : *iS++) : *iS++)
    inline int read() {
      char ch = gh();
      int x = 0;
      char t = 0;
      while (ch < '0' || ch > '9') t |= ch == '-', ch = gh();
      while (ch >= '0' && ch <= '9') x = x * 10 + (ch ^ 48), ch = gh();
      return t ? -x : x;
    }
    inline void putc(char ch) { out[len++] = ch; }
    template <class T> inline void write(T x) {
      if (x < 0) putc('-'), x = -x;
      if (x > 9) write(x / 10);
      out[len++] = x % 10 + 48;
    }
    string getstr(void) {
      string s = "";
      char c = gh();
      while (c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == EOF) c = gh();
      while (!(c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == EOF))s.push_back(c), c = gh();
      return s;
    }
    void putstr(string str, int begin = 0, int end = -1) {
      if (end == -1)
        end = str.size();
      for (int i = begin; i < end; i++)
        putc(str[i]);
      return;
    }
    inline void flush() {
      fwrite(out, 1, len, stdout);
      len = 0;
    }
} // namespace IO by Macesuted
using IO::flush;
using IO::getstr;
using IO::putc;
using IO::putstr;
using IO::read;
using IO::write;
const int N = 2e6;
int n, siz = 360, bel, q, num, cnt, ans[N], a[N], pos[N], LL[N], RR[N], s[N], t[N], tag[N];
struct update { 
  int x, tim, val; 
  update(int xx = 0,int yy = 0,int zz = 0) {
    x = xx, tim = yy, val = zz;
  }
}q1[N];
struct ask { 
  int x, tim, val, id; 
  ask(int xx = 0,int yy = 0,int zz = 0,int uu = 0) {
    x = xx, tim = yy, val = zz, id = uu;
  }
}q2[N];
int cmp1(update x, update y) { return (x.x != y.x) ? x.x < y.x : x.tim < y.tim; }
int cmp2(ask x, ask y) { return (x.x != y.x) ? x.x < y.x : x.tim < y.tim; }
void change(int l,int r,int k) {
  l = max(l, 0ll);
    r = min(r, q);
  int pl = pos[l], pr = pos[r];
  if(pl == pr) {
    for(int i = l; i <= r; i++) s[i] += k;
    int L = LL[pl], R = RR[pl];
    for(int i = L; i <= R; i++) t[i] = s[i];
    sort(t + L, t + R + 1);
    reverse(t + L, t + R + 1);
  }  else{
    for(int i = pl + 1; i <= pr - 1; i++) tag[i] += k;
    int L = LL[pl], R = RR[pl];
    for(int i = l; i <= R; i++) s[i] += k;
    for(int i = L; i <= R; i++) t[i] = s[i];
    sort(t + L, t + R + 1);
    reverse(t + L, t + R + 1);
    L = LL[pr], R = RR[pr];
    for(int i = L; i <= r; i++) s[i] += k;
    for(int i = L; i <= R; i++) t[i] = s[i];
    sort(t + L, t + R + 1);
    reverse(t + L, t + R + 1);
  }
}
int check(int l,int r,int k) {
  int pl = pos[l], pr = pos[r], cnt = 0;
  if(pl == pr) {
    for(int i = l; i <= r; i++) if(s[i] + tag[pl] >= k) cnt++;
    return cnt;
  }  else {
    for(int i = pl + 1; i <= pr - 1; i++) {
      int l1 = LL[i], r1 = RR[i];
      while(l1 < r1) {
        int mid = (l1 + r1) / 2 + 1;
        if(t[mid] + tag[i] >= k) l1 = mid;
        else r1 = mid - 1;
      }
      if(t[l1] + tag[i] >= k) cnt += l1 - LL[i] + 1;
    }
    int L = LL[pr], R = RR[pl];
    for(int i = l; i <= R; i++) if(s[i] + tag[pl] >= k) cnt++;
    for(int i = L; i <= r; i++) if(s[i] + tag[pr] >= k) cnt++;
    return cnt;
  }
}
signed main () {
  n = read(), q = read();
  for(int i = 1; i <= n; i++) a[i] = read();
  for(int i = 1; i <= q; i++) {
    int op = read(), l, r, x;
    if(op == 1) {
      l = read(), r = read(), x = read();
      ++cnt; q1[cnt] = update(l, i, x);
      ++cnt; q1[cnt] = update(r + 1, i, -x);
    }  else {
      l = read(), x = read();
      ++num; q2[num] = ask(l, i, x, num);
    }
  }
  bel = (q + 1 - 1) / siz + 1;
  for(int i = 1; i <= bel; i++) {
    LL[i] = (i - 1) * siz + 1;
    RR[i] = min(i * siz, q + 1);
    for(int j = LL[i]; j <= RR[i]; j++) {
      pos[j] = i;
    }
  }
  sort(q1 + 1, q1 + cnt + 1, cmp1);
  sort(q2 + 1, q2 + num + 1, cmp2);
  memset(ans, -1, sizeof(ans));
  for(int i = 1, j = 1; i <= num; i++) {
    while( (q1[j].x < q2[i].x || (q1[j].x == q2[i].x && q1[j].tim < q2[i].tim) ) && j <= cnt) {
      change(q1[j].tim + 1, q + 1, q1[j].val);
      ++j;
    }
    ans[q2[i].id] = check(1, q2[i].tim, q2[i].val - a[q2[i].x]);
  }
  for(int i = 1; i <= q; i++) {
    if(ans[i] != -1) write(ans[i]), putc('\n');
  }
  flush();
  return 0;
}