Submission details
Task:Ohjelmat
Sender:Kuha
Submission time:2025-10-18 12:41:24 +0300
Language:text (C++17)
Status:READY
Result:0
Feedback
groupverdictscore
#10
Test results
testverdicttimescore
#10.00 s0details

Code

#include <bits/stdc++.h>

#define N (1<<18)

using namespace std;

int v[N];
int comp[N];
vector<int> mp[N];
map<int, int> enc;
map<int, int> de;

int main() {
    cin.sync_with_stdio(false);
    cin.tie(0);
    srand(time(0));
    int n, q;
    cin>>n>>q;
    vector<int> vals;
    for (int i = 1; i <= n; i++) {
        cin>>v[i];
        vals.push_back(v[i]);
    }
    vals.push_back(-1);
    sort(vals.begin(), vals.end());
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
        if (vals[i] != vals[i - 1]) cnt++;
        enc[vals[i]] = cnt;
        de[cnt] = vals[i];
    }
    for (int i = 1; i <= n; i++) {
        comp[i] = enc[v[i]];
        mp[comp[i]].push_back(i);
    }
    
    for (int i = 1; i <= n; i++) {
        if (mp[comp[i]].back() != n + 1) mp[comp[i]].push_back(n + 1);
    }
    while (q --> 0) {
        int l, r;
        cin>>l>>r;
        for (int t = 0; t < 10; t++) {
            int len = r - l + 1;
            int i = l + (rand() % len);
            int li = -1;
            int ri = -1;
            int wl = mp[comp[i]].size();
            for (int inc = 1<<17; inc >= 1; inc /= 2) {
                if (li + inc < wl && mp[comp[i]][li + inc] < l) li += inc;
                if (ri + inc < wl && mp[comp[i]][ri + inc] <= r) ri += inc;
            }
            if (2 * (ri - li) > len) {
                cout<<de[comp[i]]<<endl;
                goto end;
            }
        }
        cout<<-1<<endl;
        end:;
    }
}

Test details

Test 1

Verdict:

input
100
156798
300372
911450
236249
...

correct output
21
20
23
22
24
...

user output
#include <bits/stdc++.h>

#define N (1<<18)

using namespace std;
...