/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 540.0 KiB
#2 Accepted 1ms 540.0 KiB
#3 Accepted 491ms 9.988 MiB
#4 Accepted 2ms 540.0 KiB
#5 Accepted 1ms 540.0 KiB
#6 Accepted 514ms 10.203 MiB
#7 Accepted 340ms 10.086 MiB
#8 Accepted 334ms 10.234 MiB
#9 Accepted 403ms 10.234 MiB
#10 Accepted 403ms 10.234 MiB
#11 Accepted 473ms 10.086 MiB
#12 Accepted 550ms 9.977 MiB
#13 Accepted 342ms 9.98 MiB
#14 Accepted 407ms 10.234 MiB
#15 Accepted 415ms 10.23 MiB
#16 Accepted 413ms 10.23 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-05 16:25:02
Judged By
Score
100
Total Time
550ms
Peak Memory
10.234 MiB