跳到正文

题解

P14989

aword约 12 分钟
原文
文章信息与版本

文章信息

发布
2026年8月22日
收录
2026年8月22日
更新
2026年9月16日
篇幅
约 12 分钟
题目
P14989

版本

  1. v2当前
  2. v1
C++
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
using l64 = long double;
typedef pair<int, int> PII;
const int inf = 1e9;
#define int i64
template <class Info, class Tag>
struct LazySegmentTree
{
    int n;
    std::vector<Info> info;
    std::vector<Tag> tag;
    LazySegmentTree() : n(0) {}
    LazySegmentTree(int n_, Info v_ = Info())
    {
        init(n_, v_);
    }
    template <class T>
    LazySegmentTree(std::vector<T> init_)
    {
        init(init_);
    }
    void init(int n_, Info v_ = Info())
    {
        init(std::vector<Info>(n_, v_));
    }
    template <class T>
    void init(std::vector<T> init_)
    {
        n = init_.size() - 1;
        info.assign(4 * n + 10, Info());
        tag.assign(4 * n + 10, Tag());
        std::function<void(int, int, int)> build = [&](int p, int l, int r)
        {
            if (l == r)
            {
                info[p] = init_[l];
                return;
            }
            int mid = (l + r) / 2;
            build(2 * p, l, mid);
            build(2 * p + 1, mid + 1, r);
            pull(p);
        };
        build(1, 1, n);
    }
    void pull(int p)
    {
        info[p] = info[2 * p] + info[2 * p + 1];
    }
    void apply(int p, const Tag &v)
    {
        info[p].apply(v);
        tag[p].apply(v);
    }
    void push(int p)
    {
        apply(2 * p, tag[p]);
        apply(2 * p + 1, tag[p]);
        tag[p] = Tag();
    }
    void modify(int p, int l, int r, int x, const Info &v)
    {
        if (r == l)
        {
            info[p] = v;
            return;
        }
        int mid = (l + r) / 2;
        push(p);
        if (x <= mid)
        {
            modify(2 * p, l, mid, x, v);
        }
        else
        {
            modify(2 * p + 1, mid + 1, r, x, v);
        }
        pull(p);
    }
    void modify(int p, const Info &v)
    {
        modify(1, 1, n, p, v);
    }
    Info rangeQuery(int p, int l, int r, int x, int y)
    {
        if (l > y || r < x)
        {
            return Info();
        }
        if (l >= x && r <= y)
        {
            return info[p];
        }
        int mid = (l + r) / 2;
        push(p);
        return rangeQuery(2 * p, l, mid, x, y) + rangeQuery(2 * p + 1, mid + 1, r, x, y);
    }
    Info rangeQuery(int l, int r)
    {
        return rangeQuery(1, 1, n, l, r);
    }
    void rangeApply(int p, int l, int r, int x, int y, const Tag &v)
    {
        if (l > y || r < x)
        {
            return;
        }
        if (l >= x && r <= y)
        {
            apply(p, v);
            return;
        }
        int mid = (l + r) / 2;
        push(p);
        rangeApply(2 * p, l, mid, x, y, v);
        rangeApply(2 * p + 1, mid + 1, r, x, y, v);
        pull(p);
    }
    void rangeApply(int l, int r, const Tag &v)
    {
        return rangeApply(1, 1, n, l, r, v);
    }
};

struct Tag
{
    int v = 0;
    void apply(const Tag &t) &
    {
        v = max(v, t.v);
    }
};
struct Info
{
    int mx = 0;
    void apply(const Tag &t) &
    {
        mx = max(mx, t.v);
    }
};
Info operator+(const Info &a, const Info &b)
{
    return {max(a.mx, b.mx)};
}
void solve()
{
    int n, m, v;
    cin >> n >> m >> v;
    vector<int> c(v + 1, 0), a(n + m + 1, 0), tme(n + m + 1, 0);
    vector<set<int>> s(v + 1);
    vector<array<int, 4>> q;
    for (int i = 1; i <= n; ++i)
    {
        cin >> a[i];
    }
    int cur_n = n;
    for (int i = 1; i <= m; ++i)
    {
        int flag;
        cin >> flag;
        if (flag == 1)
        {
            int l, r, x;
            cin >> l >> r >> x;
            q.push_back({l, r, x, i});
        }
        else if (flag == 2)
        {
            int x;
            cin >> x;
            cur_n++;
            a[cur_n] = x;
            tme[cur_n] = i;
        }
        else
        {
            int l;
            cin >> l;
            q.push_back({l, 0, 0, i});
        }
    }
    for (int i = 1; i <= cur_n; ++i)
    {
        s[a[i]].insert(i);
    }
    vector<Info> pos(cur_n + 1);
    int l = 1, r = 0, cnt = 0;
    while (l <= cur_n)
    {
        while (r < cur_n && cnt < v)
        {
            r++;
            if (c[a[r]] == 0)
                cnt++;
            c[a[r]]++;
        }
        if (cnt == v)
        {
            pos[l] = {r};
        }
        else
        {
            pos[l] = {inf};
        }
        c[a[l]]--;
        if (c[a[l]] == 0)
            cnt--;
        l++;
    }
    LazySegmentTree<Info, Tag> seg(pos);
    for (auto [ql, qr, x, i] : q)
    {
        if (x == 0)
        {
            int ans = seg.rangeQuery(ql, ql).mx;
            if (ans >= inf || tme[ans] > i)
            {
                cout << -1 << endl;
            }
            else
            {
                cout << ans << endl;
            }
        }
        else
        {
            auto itl = s[x].lower_bound(ql);
            auto itr = s[x].upper_bound(qr);
            auto ll = itl, rr = itr;
            int pl, pr;
            if (itl == s[x].begin())
            {
                pl = 1;
            }
            else
            {
                auto tmp = itl;
                tmp--;
                pl = *tmp + 1;
            }
            if (itr == s[x].end())
            {
                pr = cur_n;
                seg.rangeApply(pl, pr, {inf});
            }
            else
            {
                pr = *itr - 1;
                seg.rangeApply(pl, pr, {*itr});
            }
            if (ll != rr)
            {
                s[x].erase(ll, rr);
            }
        }
    }
}
//=====================================
signed main()
{
#ifdef LOCAL
    bool _be;
    freopen("in.in", "r", stdin);
    freopen("out.out", "w", stdout);
    clock_t __c1 = clock();
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    //=====================================
    int t = 1;
    while (t--)
    {
        solve();
    }
//=====================================
#ifdef LOCAL
    bool _ed;
    cerr << fabs(&_ed - &_be) / 1048576.0 << "MB" << endl;
    cerr << "Time used: " << clock() - __c1 << " ms" << endl;
#endif
    return 0;
}
原文