题解 P5324 【[BJOI2019]删数】
万能的线段树 QwQ
首先来分析一下这个问题。
对于一个长度为
所以现在问题变成了如何快速计算
我们发现这个东西的值域很小,是
这样单点修改的操作就可以直接在数轴上做
同时我们设置一个指针
这样整体修改的操作就可以直接
这里有一个问题,就是超过
每次查询时只需要查询
于是这道题就做完了~
复杂度是
注意因为
代码:
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
using namespace std;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline int qread() {
register char c = getchar();
register int x = 0, f = 1;
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') {
x = (x << 3) + (x << 1) + c - 48;
c = getchar();
}
return x * f;
}
inline int Abs(const int& x) {return (x > 0 ? x : -x);}
inline int Max(const int& x, const int& y) {return (x > y ? x : y);}
inline int Min(const int& x, const int& y) {return (x < y ? x : y);}
struct Segtree {
int minv[2000005], minvcnt[2000005], tag[2000005];
inline void Pushup(int p) {
minv[p] = Min(minv[p << 1], minv[p << 1 | 1]);
minvcnt[p] = 0;
if (minv[p << 1] == minv[p]) minvcnt[p] += minvcnt[p << 1];
if (minv[p << 1 | 1] == minv[p]) minvcnt[p] += minvcnt[p << 1 | 1];
return;
}
inline void Pushdown(int p, int pl, int pr) {
if (pl == pr || !tag[p]) return;
tag[p << 1] += tag[p];
minv[p << 1] += tag[p];
tag[p << 1 | 1] += tag[p];
minv[p << 1 | 1] += tag[p];
tag[p] = 0;
}
inline void Build(int p, int pl, int pr) {
if (pl == pr) {
minv[p] = 0;
minvcnt[p] = 1;
return;
}
register int mid = pl + pr >> 1;
Build(p << 1, pl, mid); Build(p << 1 | 1, mid + 1, pr);
Pushup(p);
}
inline void Modify(int p, int pl, int pr, int l, int r, int v) {
if (l > r) return;
if (pl == l && pr == r) {
tag[p] += v;
minv[p] += v;
return;
}
Pushdown(p, pl, pr);
register int mid = pl + pr >> 1;
if (mid >= r) Modify(p << 1, pl, mid, l, r, v);
else if (mid + 1 <= l) Modify(p << 1 | 1, mid + 1, pr, l, r, v);
else {
Modify(p << 1, pl, mid, l, mid, v);
Modify(p << 1 | 1, mid + 1, pr, mid + 1, r, v);
}
Pushup(p);
}
inline pair <int, int> Query(int p, int pl, int pr, int l, int r) {
if (pl == l && pr == r) return make_pair(minv[p], minvcnt[p]);
Pushdown(p, pl, pr);
register int mid = pl + pr >> 1;
if (mid >= r) return Query(p << 1, pl, mid, l, r);
else if (mid + 1 <= l) return Query(p << 1 | 1, mid + 1, pr, l, r);
else {
pair <int, int> ans1 = Query(p << 1, pl, mid, l, mid);
pair <int, int> ans2 = Query(p << 1 | 1, mid + 1, pr, mid + 1, r);
pair <int, int> ans3 = make_pair(0, 0);
ans3.first = Min(ans1.first, ans2.first);
if (ans1.first == ans3.first) ans3.second += ans1.second;
if (ans2.first == ans3.first) ans3.second += ans2.second;
return ans3;
}
}
};
Segtree sgt;
int n, a[150005], cnt[450020], m, zero = 150005, maxv = 450015;
inline void Read() {
n = qread(); m = qread();
for (register int i = 1;i <= n;i++) a[i] = qread() + zero;
}
inline void Add(int x) {
if (x <= zero + n) sgt.Modify(1, 1, maxv, x - cnt[x], x - cnt[x], 1);
cnt[x]++;
}
inline void Del(int x) {
cnt[x]--;
if (x <= zero + n) sgt.Modify(1, 1, maxv, x - cnt[x], x - cnt[x], -1);
}
inline void Addall(int x) {
sgt.Modify(1, 1, maxv, x - cnt[x] + 1, x, 1);
}
inline void Delall(int x) {
sgt.Modify(1, 1, maxv, x - cnt[x] + 1, x, -1);
}
inline void Solve() {
sgt.Build(1, 1, maxv);
for (register int i = 1;i <= n;i++) {
Add(a[i]);
}
pair <int, int> ans = sgt.Query(1, 1, maxv, zero + 1, zero + n);
//printf("%d %d\n", ans.first, ans.second);
while (m--) {
register int p = qread(), x = qread();
if (p) {
x += zero;
Del(a[p]);
Add(x);
a[p] = x;
} else if (~x) {
Delall(zero + n);
zero--;
} else {
zero++;
Addall(zero + n);
}
pair <int, int> ans = sgt.Query(1, 1, maxv, zero + 1, zero + n);
if (ans.first) puts("0");
else printf("%d\n", ans.second);
}
}
int main() {
Read();
Solve();
#ifndef ONLINE_JUDGE
while (1);
#endif
return 0;
}