题解:P17738 [ICPC 2019 Xuzhou R] Yuuki and a problem
Ryc12345678910 · · 题解
P17738 [ICPC 2019 Xuzhou R] Yuuki and a problem 题解
题意
给定一个序列
- 单点修改;
- 区间最小的不可表示值查询。
题解
首先考虑一个结论:假设现在可以表示出
使用树状数组套值域线段树维护这个操作。值域线段树维护值落在区间
:::success[Code]
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define fir first
#define sec second
using namespace std;
namespace Lemon {
using ll = long long;
const int N = 2e5 + 5;
const int V = 2e5 + 5;
int n, q;
int a[N];
int tot = 0;
int rt[N];
struct SGT {
struct node {
int l, r;
ll w;
} tr[N * 80];
inline void update(int& p, int l, int r, int x, int k) {
if (!p) p = ++tot;
tr[p].w += (ll)x * k;
if (l == r) return;
int mid = (l + r) >> 1;
if (x <= mid) update(tr[p].l, l, mid, x, k);
else update(tr[p].r, mid + 1, r, x, k);
}
inline ll query(int p, int l, int r, int K) {
if (!p || K < l) return 0;
if (r <= K) return tr[p].w;
int mid = (l + r) >> 1;
ll res = query(tr[p].l, l, mid, K);
if (K > mid) res += query(tr[p].r, mid + 1, r, K);
return res;
}
} seg;
struct BIT {
inline void upd(int u, int w, int k) {
for (int i = u; i <= n; i += i & -i) seg.update(rt[i], 1, V, w, k);
}
inline ll query(int u, int K) {
ll res = 0;
for (int i = u; i > 0; i -= i & -i) res += seg.query(rt[i], 1, V, K);
return res;
}
} bit;
inline void solve() {
cin >> n >> q;
for (int i = 1; i <= n; i++) {
cin >> a[i];
bit.upd(i, a[i], 1);
}
while (q--) {
int opt; cin >> opt;
if (opt == 1) {
int x, y; cin >> x >> y;
bit.upd(x, a[x], -1);
bit.upd(x, y, 1);
a[x] = y;
} else {
int l, r; cin >> l >> r;
ll ans = 0;
while (1) {
int k = min(ans + 1, (ll)V);
ll w = bit.query(r, k) - bit.query(l - 1, k);
if (w <= ans) break;
ans = w;
}
cout << ans + 1 << '\n';
}
}
}
}
signed main() {
IOS;
int t = 1;
// cin >> t;
while (t--) Lemon::solve();
return 0;
}
:::