跳到正文

题解

题解:P14089 [ICPC 2023 Seoul R] K-Lottery

SpaceBro约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年3月1日
收录
2026年8月16日
更新
2026年11月14日
篇幅
约 6 分钟
获赞
1
题目
P14089

版本

  1. v1当前

Solution

这里提供一个简化后的做法。

致谢

感谢 @rekcA_h 大佬帮助我理解并指出代码问题 qwq.

我在本地 Typora 完成了题解的编写,这里使用图片上传的形式吧。

可以拖动图片至浏览器,很清晰的。

本质上对于 rekcA_h 大佬题解的简化就是维护一个全局 tag 而不用线段树维护懒标记。

Code

C++
#include <bits/stdc++.h>
#define pr(x) (x - 1)
#define nd(x) (x + 1)
using namespace std;
typedef unsigned long long ull;

constexpr int K = 10005, M = 1005, N = 1000005;
constexpr ull Base = 1145147777ull;

int k, m, n, pp[K], tmp[N], a[N], pos[N];
vector <int> lot[M];
unordered_map <ull, int> mp;
ull pw[N], sum;

namespace Segment_Tree {
    constexpr int T = 4000005;

    #define ls (p << 1)
    #define rs (p << 1 | 1)

    int tag;
    struct node {
        int cnt;
        ull hsh;
    } tr[T];
    inline node operator + (const node &nl, const node &nr) { return {nl.cnt + nr.cnt, nl.hsh * pw[nr.cnt] + nr.hsh}; }

    void build(int p, int l, int r) {
        if (l == r) {
            if (pos[l]) tr[p].cnt = 1, tr[p].hsh = pos[l];
            return void();
        }
        int mid = (l + r) >> 1;
        return build(ls, l, mid), build(rs, nd(mid), r), tr[p] = tr[ls] + tr[rs], void();
    }
    void modify(int p, int l, int r, const int &x, const int val) {
        if (l == r) {
            if (val) tr[p].cnt = 1, tr[p].hsh = val;
            else tr[p].cnt = 0, tr[p].hsh = 0ull;
            return void();
        }
        int mid = (l + r) >> 1;
        if (x <= mid) modify(ls, l, mid, x, val);
        else modify(rs, nd(mid), r, x, val);
        return tr[p] = tr[ls] + tr[rs], void();
    }
} using namespace Segment_Tree;

int main() {
    cin.tie(0) -> sync_with_stdio(false);
    cin >> k >> m >> n;
    for (int i = 1; i <= m; ++i) {
        lot[i].resize(k);
        for (int j = 0; j < k; ++j) cin >> lot[i][j], pp[lot[i][j]] = nd(j);
        ull hsh = 0ull;
        for (int j = 1; j <= k; ++j) hsh = hsh * Base + pp[j];
        mp[hsh] = i;
    }

    for (int i = 1; i <= n; ++i) cin >> a[i], tmp[i] = a[i];
    sort(nd(tmp), nd(tmp + n));
    for (int i = 1; i <= n; ++i) a[i] = lower_bound(nd(tmp), nd(tmp + n), a[i]) - tmp;

    for (int i = 1; i <= k; ++i) pos[a[i]] = i;
    pw[0] = 1ull;
    for (int p = 1; p < N; ++p) pw[p] = pw[p - 1] * Base;
    for (int p = 0; p < k; ++p) sum += pw[p];
    build(1, 1, n);

    if (mp.count(tr[1].hsh)) {
        int id = mp[tr[1].hsh];
        for (const int &x : lot[id]) cout << x << ' ';
        return 0;
    }
    for (int ed = nd(k); ed <= n; ++ed) {
        modify(1, 1, n, a[ed - k], 0), ++tag, modify(1, 1, n, a[ed], k + tag);
        ull H = tr[1].hsh - sum * tag;
        if (mp.count(H)) {
            int id = mp[H];
            for (const int &x : lot[id]) cout << x << ' ';
            return 0;
        }
    }
    cout << 0;

    return 0;
}

感谢阅读。

原文
存档评论1 条抓取于