跳到正文

题解

题解:P17738 [ICPC 2019 Xuzhou R] Yuuki and a problem

Ryc12345678910约 5 分钟
原文
文章信息与版本

文章信息

发布
2026年10月26日
收录
2026年10月26日
更新
2026年11月7日
篇幅
约 5 分钟

版本

  1. v1当前

P17738 [ICPC 2019 Xuzhou R] Yuuki and a problem 题解

题意

给定一个序列 aa,维护两种操作:

  • 单点修改;
  • 区间最小的不可表示值查询。

题解

首先考虑一个结论:假设现在可以表示出 [1,V][1,V] 的所有正整数,设 s=∑x∈[L,R],x≤V+1xs=\sum_{x\in [L,R],x\le V+1}x,当 s>Vs>V 的时候,说明可以拓展能够表示出的范围,拓展到 [1,s][1,s],然后令 V=sV=s 继续上述操作;当 s≤Vs\le V 的时候,说明无法表示出 s+1s+1 这个数,s+1s+1 即为我们要查询的答案。

使用树状数组套值域线段树维护这个操作。值域线段树维护值落在区间 [l,r][l,r] 的数的总和,树状数组维护下标。

Code
C++
#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;
}
原文