提出 #74114837


ソースコード 拡げる

#include <bits/stdc++.h>

using namespace std;

void update(vector<int> &fenwick, int index, int value) {
    while (index < (int)fenwick.size()) {
        fenwick[index] += value;
        index += index & -index;
    }
}
int query(const vector<int> &fenwick, int index) {
    int sum = 0;
    while (index) {
        sum += fenwick[index];
        index -= index & -index;
    }
    return sum;
}

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> a(1+n);
    for (int i = 1; i <= n; ++i) cin >> a[i];
    int q; cin >> q;
    vector<pair<long long, int>> queries(q);
    for (int i = 0; i < q; ++i) {
        queries[i].second = i;
        cin >> queries[i].first;
    }
    sort(queries.begin(), queries.end());

    map<int, int> freq;
    for (int i = 1; i <= n; ++i) ++freq[a[i]];
    const int missingVals = m - freq.size();
    map<int, vector<int>> fs;
    for (auto [x, f] : freq) fs[f].push_back(x);
    vector<pair<int, vector<int>>> byFreq;
    for (auto &[x, v] : fs) byFreq.emplace_back(x, v);

    vector<int> qAns(q);
    int nextQ = 0;
    while (nextQ < q && queries[nextQ].first <= n) {
        qAns[queries[nextQ].second] = a[queries[nextQ].first];
        ++nextQ;
    }

    // after pos lastEnd every value has frequency >= lastFreq
    long long lastEnd = n, lastFreq = 0, vals = missingVals;
    vector<int> fenwick(1+m);
    for (int v = 1; v <= m; ++v) {
        if (!freq.count(v)) update(fenwick, v, +1);
    }
    auto kthSmallest = [&](int k) { // find kth smallest value in fenwick tree
        assert(1 <= k && k <= vals);
        int l = 0, r = m;
        while (l+1 < r) {
            int mid = (l+r) / 2;
            if (query(fenwick, mid) >= k) r = mid;
            else l = mid;
        }
        return r;
    };

    for (auto &[f, v] : byFreq) {
        int df = f-lastFreq;
        long long blockSize = df * vals;

        while (nextQ < q && queries[nextQ].first <= lastEnd + blockSize) {
            int valInBlock = (queries[nextQ].first - lastEnd - 1) % vals + 1;
            qAns[queries[nextQ].second] = kthSmallest(valInBlock);
            ++nextQ;
        }
        lastEnd += blockSize;
        lastFreq = f;
        vals += v.size();
        for (int x : v) update(fenwick, x, +1);
    }
    // infinite block at the end
    assert(vals == m);
    while (nextQ < q) {
        int valInBlock = (queries[nextQ].first - lastEnd - 1) % vals + 1;
        qAns[queries[nextQ].second] = kthSmallest(valInBlock);
        ++nextQ;
    }

    for (int x : qAns) cout << x << '\n';
    return 0;
}

提出情報

提出日時
問題 E - A += v
ユーザ erekle
言語 C++ IOI-Style(GNU++20) (GCC 14.2.0)
得点 475
コード長 2643 Byte
結果 AC
実行時間 384 ms
メモリ 36772 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 475 / 475
結果
AC × 2
AC × 34
セット名 テストケース
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 01_handmade_00.txt, 01_handmade_01.txt, 01_handmade_02.txt, 01_handmade_03.txt, 01_handmade_04.txt, 01_handmade_05.txt, 01_handmade_06.txt, 01_handmade_07.txt, 01_handmade_08.txt, 01_handmade_09.txt, 01_handmade_10.txt, 01_handmade_11.txt, 01_handmade_12.txt, 01_handmade_13.txt, 01_handmade_14.txt, 01_handmade_15.txt, 01_handmade_16.txt, 01_handmade_17.txt, 01_handmade_18.txt, 01_handmade_19.txt, 01_handmade_20.txt, 01_handmade_21.txt, 01_handmade_22.txt, 01_handmade_23.txt, 01_handmade_24.txt, 01_handmade_25.txt, 01_handmade_26.txt, 01_handmade_27.txt, 01_handmade_28.txt, 01_handmade_29.txt, 01_handmade_30.txt, 01_handmade_31.txt
ケース名 結果 実行時間 メモリ
00_sample_00.txt AC 0 ms 1644 KiB
00_sample_01.txt AC 0 ms 1644 KiB
01_handmade_00.txt AC 46 ms 5612 KiB
01_handmade_01.txt AC 211 ms 9524 KiB
01_handmade_02.txt AC 46 ms 5612 KiB
01_handmade_03.txt AC 86 ms 9964 KiB
01_handmade_04.txt AC 315 ms 22836 KiB
01_handmade_05.txt AC 262 ms 22776 KiB
01_handmade_06.txt AC 346 ms 26860 KiB
01_handmade_07.txt AC 311 ms 25440 KiB
01_handmade_08.txt AC 346 ms 26860 KiB
01_handmade_09.txt AC 249 ms 21968 KiB
01_handmade_10.txt AC 346 ms 26956 KiB
01_handmade_11.txt AC 110 ms 10732 KiB
01_handmade_12.txt AC 238 ms 13804 KiB
01_handmade_13.txt AC 189 ms 17712 KiB
01_handmade_14.txt AC 358 ms 26960 KiB
01_handmade_15.txt AC 190 ms 17712 KiB
01_handmade_16.txt AC 356 ms 27052 KiB
01_handmade_17.txt AC 255 ms 21956 KiB
01_handmade_18.txt AC 354 ms 26868 KiB
01_handmade_19.txt AC 242 ms 36768 KiB
01_handmade_20.txt AC 241 ms 36764 KiB
01_handmade_21.txt AC 242 ms 36772 KiB
01_handmade_22.txt AC 126 ms 9452 KiB
01_handmade_23.txt AC 126 ms 9452 KiB
01_handmade_24.txt AC 127 ms 9452 KiB
01_handmade_25.txt AC 384 ms 27048 KiB
01_handmade_26.txt AC 113 ms 9708 KiB
01_handmade_27.txt AC 134 ms 9708 KiB
01_handmade_28.txt AC 132 ms 9708 KiB
01_handmade_29.txt AC 109 ms 9580 KiB
01_handmade_30.txt AC 132 ms 9580 KiB
01_handmade_31.txt AC 129 ms 9580 KiB