Submission #74122302


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

struct Query {
    ll t;
    int idx;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N, M;
    cin >> N >> M;
    
    vector<int> A(N);
    vector<ll> cnt(M + 1);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
        cnt[A[i]]++;
    }
    
    int Q;
    cin >> Q;
    
    vector<ll> ans(Q, -1);
    vector<Query> queries;
    queries.reserve(Q);
    
    for (int i = 0; i < Q; i++) {
        ll X; cin >> X;
        if (X <= N) {
            ans[i] = A[X - 1];
        } else {
            queries.push_back({X - N, i});
        }
    }
    
    sort(queries.begin(), queries.end(), [](const Query& a, const Query& b) {
        return a.t < b.t;
    });
    
    map<ll, vector<int>> groups;
    for (int i = 1; i <= M; i++) {
        groups[cnt[i]].push_back(i);
    }
    for (auto& [k, v] : groups) {
        sort(v.begin(), v.end());
    }
    
    ll cur_t = 1;
    size_t qptr = 0;
    
    while (qptr < queries.size()) {
        auto it = groups.begin();
        
        auto it2 = next(it);
        if (it2 == groups.end()) {
            ll cycle_start = cur_t;
            while (qptr < queries.size()) {
                ll t = queries[qptr].t;
                ans[queries[qptr].idx] = (t - cycle_start) % M + 1;
                qptr++;
            }
            break;
        }
        
        ll mn = it->first;
        ll next_mn = it2->first;
        ll k = next_mn - mn;
        
        vector<int> per = move(it->second);
        int perlen = per.size();
        ll seg_len = k * (ll)perlen;
        ll seg_r = cur_t + seg_len - 1;
        
        while (qptr < queries.size() && queries[qptr].t <= seg_r) {
            ll t = queries[qptr].t;
            ll pos = (t - cur_t) % perlen;
            ans[queries[qptr].idx] = per[pos];
            qptr++;
        }
        
        groups.erase(it);
        
        auto& target = groups[next_mn];
        vector<int> merged;
        merged.reserve(per.size() + target.size());
        merge(per.begin(), per.end(), target.begin(), target.end(), back_inserter(merged));
        target = move(merged);
        
        cur_t = seg_r + 1;
    }
    
    for (ll x : ans) {
        cout << x << "\n";
    }
    
    return 0;
}

Submission Info

Submission Time
Task E - A += v
User Eason0709
Language C++23 (GCC 15.2.0)
Score 475
Code Size 2420 Byte
Status AC
Exec Time 409 ms
Memory 18672 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 475 / 475
Status
AC × 2
AC × 34
Set Name Test Cases
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
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3604 KiB
00_sample_01.txt AC 1 ms 3616 KiB
01_handmade_00.txt AC 30 ms 8104 KiB
01_handmade_01.txt AC 61 ms 17928 KiB
01_handmade_02.txt AC 31 ms 7976 KiB
01_handmade_03.txt AC 35 ms 13676 KiB
01_handmade_04.txt AC 65 ms 16036 KiB
01_handmade_05.txt AC 66 ms 17120 KiB
01_handmade_06.txt AC 78 ms 17816 KiB
01_handmade_07.txt AC 73 ms 17792 KiB
01_handmade_08.txt AC 78 ms 18224 KiB
01_handmade_09.txt AC 66 ms 16968 KiB
01_handmade_10.txt AC 78 ms 18284 KiB
01_handmade_11.txt AC 37 ms 9908 KiB
01_handmade_12.txt AC 51 ms 11560 KiB
01_handmade_13.txt AC 61 ms 17576 KiB
01_handmade_14.txt AC 85 ms 18204 KiB
01_handmade_15.txt AC 60 ms 16436 KiB
01_handmade_16.txt AC 85 ms 18040 KiB
01_handmade_17.txt AC 72 ms 16688 KiB
01_handmade_18.txt AC 87 ms 17732 KiB
01_handmade_19.txt AC 45 ms 14604 KiB
01_handmade_20.txt AC 45 ms 14584 KiB
01_handmade_21.txt AC 45 ms 14600 KiB
01_handmade_22.txt AC 46 ms 16448 KiB
01_handmade_23.txt AC 46 ms 16396 KiB
01_handmade_24.txt AC 46 ms 16440 KiB
01_handmade_25.txt AC 82 ms 18672 KiB
01_handmade_26.txt AC 195 ms 18668 KiB
01_handmade_27.txt AC 409 ms 18632 KiB
01_handmade_28.txt AC 302 ms 18608 KiB
01_handmade_29.txt AC 42 ms 16716 KiB
01_handmade_30.txt AC 46 ms 16712 KiB
01_handmade_31.txt AC 46 ms 16592 KiB