/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 462ms 9.984 MiB
#4 Accepted 1ms 320.0 KiB
#5 Accepted 1ms 320.0 KiB
#6 Accepted 544ms 10.137 MiB
#7 Accepted 369ms 9.977 MiB
#8 Accepted 353ms 10.078 MiB
#9 Accepted 434ms 10.164 MiB
#10 Accepted 456ms 10.168 MiB
#11 Accepted 578ms 10.227 MiB
#12 Accepted 562ms 10.0 MiB
#13 Accepted 350ms 9.977 MiB
#14 Accepted 450ms 10.23 MiB
#15 Accepted 481ms 10.25 MiB
#16 Accepted 468ms 10.09 MiB

Code

#ifndef LOCAL
#include <bits/stdc++.h>
#define debug(...)
#endif

using namespace std;
#define int long long
#define cinv(v) for (auto &it:v) cin>>it;
#define coutv(v) for (auto &it:v) cout<< it<<' '; cout<<'\n';

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

int random(int a, int b) {
    if (a > b) return 0;
    return a + rng() % (b - a + 1);
}

const int INF = 1e15;

void shelby() {
    int n, q;
    cin >> n >> q;
    vector<int> v(n + 1);
    for (int i = 1; i <= n; ++i) cin >> v[i];

    vector<pair<pair<int,int>,int> > queries(q);
    for (int i = 0; i < q; ++i) cin >> queries[i].first.first >> queries[i].first.second, queries[i].second = i;
    int block_sz = sqrt(n);
    sort(queries.begin(), queries.end(), [&](auto i, auto j) -> bool {
        return make_pair(i.first.first / block_sz, i.first.second) <
               make_pair(j.first.first / block_sz, j.first.second);
    });

    vector<int> cnt(n + 1), ans(q);
    int curr_l = 1, curr_r = 1;
    cnt[v[curr_l]]++;
    for (auto &it: queries) {
        auto [l,r] = it.first;
        while (l < curr_l) curr_l--, cnt[v[curr_l]]++;
        while (l > curr_l) cnt[v[curr_l]]--, curr_l++;
        while (r > curr_r) curr_r++, cnt[v[curr_r]]++;
        while (r < curr_r) cnt[v[curr_r]]--, curr_r--;

        int res = INF;
        for (int i = 1; i <= 100; ++i) {
            int x = random(l, r);
            if (cnt[v[x]] > (r - l + 1) / 3) res = min(res, v[x]);
        }
        if (res == INF) res = -1;
        ans[it.second] = res;
    }
    for (auto &it: ans) cout << it << '\n';
}

signed main() {
    cin.tie(0)->sync_with_stdio(0);
    int t = 1;
    // cin >> t;
    for (int _ = 1; _ <= t; ++_) {
        // cout << "Case " << _ << ": ";
        shelby();
    }
}

Information

Submit By
Type
Submission
Problem
P1103 The Secret Garden of Numbers
Contest
Brain Booster #7
Language
C++17 (G++ 13.2.0)
Submit At
2024-11-05 16:25:02
Judged At
2024-11-11 02:27:28
Judged By
Score
100
Total Time
578ms
Peak Memory
10.25 MiB