/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 468.0 KiB
#2 Accepted 1ms 512.0 KiB
#3 Accepted 272ms 9.922 MiB
#4 Accepted 1ms 404.0 KiB
#5 Accepted 1ms 332.0 KiB
#6 Wrong Answer 292ms 10.137 MiB
#7 Accepted 159ms 9.98 MiB
#8 Accepted 158ms 10.25 MiB
#9 Accepted 224ms 10.227 MiB
#10 Accepted 228ms 10.23 MiB
#11 Accepted 238ms 10.254 MiB
#12 Wrong Answer 253ms 10.176 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 <= 20; ++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:24:12
Judged At
2024-11-11 02:27:30
Judged By
Score
50
Total Time
292ms
Peak Memory
10.254 MiB