Submission details
Task:Kyselyt
Sender:Gomhog
Submission time:2025-10-19 11:19:08 +0300
Language:C++ (C++11)
Status:READY
Result:60
Feedback
groupverdictscore
#1ACCEPTED10
#2ACCEPTED20
#3ACCEPTED30
#40
Test results
testverdicttimegroup
#1ACCEPTED0.04 s1, 2, 3, 4details
#2ACCEPTED0.04 s1, 2, 3, 4details
#3ACCEPTED0.04 s1, 3, 4details
#4ACCEPTED0.23 s2, 3, 4details
#5ACCEPTED0.29 s2, 3, 4details
#6ACCEPTED0.20 s2, 3, 4details
#7ACCEPTED0.54 s3, 4details
#8ACCEPTED0.45 s4details
#9ACCEPTED0.57 s4details
#10--4details
#11ACCEPTED0.49 s3, 4details
#12--4details
#13ACCEPTED0.24 s3, 4details
#14ACCEPTED0.48 s4details
#15ACCEPTED0.48 s4details
#16ACCEPTED0.45 s4details

Code

#include <bits/stdc++.h>
#define F first
#define S second
#define X real()
#define Y imag()
using namespace std;
typedef long long ll;
typedef long double ld;

const int N = 1<<18;
int n;

struct hashp {
    size_t operator()(const pair<int,int>& p) const
    {
        return p.F ^ (p.S + 2376487 + ((p.F) << 6) + ((p.F) >> 2));
    }
};

int x[N];
unordered_map<pair<int,int>,int, hashp> counts(18*N+1);
int dom[2*N];

pair<int,int> get_ancs(int a, int b) {
    int lono=N+a;
    int hino=N+b-1;
    int aa = lono;
    int bb = hino;
    while (aa/2 < bb/2) {
        if (aa%2==0) lono=aa/2;
        if (bb%2==1) hino=bb/2;
        aa /=2;
        bb /=2;
    }
    return {lono,hino};
}

int get_am(int ancl, int ancr, int val) {
    int su=0;
    auto xx = counts.find({ancl,val});
    if ( xx != counts.end()) {
        su += xx->S;
    }
    xx = counts.find({ancr,val});
    if (xx != counts.end()) {
        su += xx->S;
    }
    return su;
}

void make_trees() {
    int L = N/2;
    int lo = 0;
    int hi = N/2;
    for (int i=2;i<2*N;i++) {
        for (int j=lo; j<min(n,hi); j++) {
            int r = ++counts[{i,x[j]}];
            if (r>L/2) dom[i] = x[j];
        }
        lo+=L;
        hi+=L;
        if (lo==N) {
            L/=2;
            lo=0;
            hi=L;
        }
    }
}

int get_count(int a, int b, int v) {
    int aa = a+N;
    int bb = b-1+N;
    int su = 0;
    while (aa <=bb) {
        if (aa%2 == 1) {
            auto xx = counts.find({aa,v});
            if (xx != counts.end()) su += xx->S;
            ++aa;
        }
        if (bb%2 == 0) {
            auto xx = counts.find({bb,v});
            if (xx != counts.end()) su += xx->S;
            --bb;
        }
        aa/=2;
        bb/=2;
    }
    return su;
}

set<int> tried;

int answer(int a, int b) {
    tried.clear();
    pair<int,int> anc = get_ancs(a,b);
    int aa = a+N;
    int bb = b-1+N;
    while (aa<=bb) {
        if (aa%2 == 1) {
            int tar = dom[aa++];
            if (tar > 0 && !tried.count(tar) && get_am(anc.F,anc.S,tar) >= (b-a)/2 + 1) {
                tried.insert(tar);
                if (get_count(a,b,tar) >= ((b-a)/2) + 1) return tar;
            }
        }
        if (bb%2 == 0) {
            int tar = dom[bb--];
            if (tar > 0 && !tried.count(tar) && get_am(anc.F,anc.S,tar) >= (b-a)/2 + 1) {
                tried.insert(tar);
                if (get_count(a,b,tar) >= ((b-a)/2) + 1) return tar;
            }
        }
        aa/=2;
        bb/=2;
    }
    return -1;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    int q;
    cin>>n>>q;
    for (int i=0;i<n;i++) {
        cin>>x[i];
    }

    make_trees();

    for (int qu=0;qu<q;qu++) {
        int a, b;
        cin>>a>>b;
        cout << answer(a-1,b)<<"\n";
    }
}

Test details

Test 1

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
100 100
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
1
1
1
1
1
...

user output
1
1
1
1
1
...

Test 2

Group: 1, 2, 3, 4

Verdict: ACCEPTED

input
100 100
2 1 2 2 1 2 2 2 1 2 2 1 1 1 1 ...

correct output
2
1
1
2
1
...

user output
2
1
1
2
1
...

Test 3

Group: 1, 3, 4

Verdict: ACCEPTED

input
100 100
5 19 44 88 14 79 50 44 14 99 7...

correct output
-1
-1
-1
-1
-1
...

user output
-1
-1
-1
-1
-1
...

Test 4

Group: 2, 3, 4

Verdict: ACCEPTED

input
100000 100000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
1
1
1
1
1
...

user output
1
1
1
1
1
...

Test 5

Group: 2, 3, 4

Verdict: ACCEPTED

input
100000 100000
1 1 1 2 1 2 1 1 2 1 1 1 1 2 2 ...

correct output
1
1
2
1
1
...

user output
1
1
2
1
1
...

Test 6

Group: 2, 3, 4

Verdict: ACCEPTED

input
100000 100000
8 2 6 1 10 4 9 7 8 10 4 2 8 2 ...

correct output
-1
-1
-1
-1
-1
...

user output
-1
-1
-1
-1
-1
...

Test 7

Group: 3, 4

Verdict: ACCEPTED

input
100000 100000
141307 493258596 365539511 222...

correct output
-1
-1
-1
-1
-1
...

user output
-1
-1
-1
-1
-1
...

Test 8

Group: 4

Verdict: ACCEPTED

input
200000 200000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
1
1
1
1
1
...

user output
1
1
1
1
1
...

Test 9

Group: 4

Verdict: ACCEPTED

input
200000 200000
1 2 2 2 1 2 2 1 1 1 1 1 1 1 1 ...

correct output
2
2
2
2
2
...

user output
2
2
2
2
2
...

Test 10

Group: 4

Verdict:

input
200000 200000
286470749 280175209 741317063 ...

correct output
-1
-1
-1
-1
-1
...

user output
(empty)

Test 11

Group: 3, 4

Verdict: ACCEPTED

input
100000 100000
613084013 1000000000 411999902...

correct output
-1
-1
-1
-1
1000000000
...

user output
-1
-1
-1
-1
1000000000
...

Test 12

Group: 4

Verdict:

input
200000 200000
613084013 1000000000 411999902...

correct output
1000000000
1000000000
-1
1000000000
-1
...

user output
(empty)

Test 13

Group: 3, 4

Verdict: ACCEPTED

input
100000 100000
663307073 663307073 663307073 ...

correct output
329574367
965067805
768744535
691214891
21873594
...

user output
329574367
965067805
768744535
691214891
21873594
...

Test 14

Group: 4

Verdict: ACCEPTED

input
200000 200000
663307073 663307073 663307073 ...

correct output
107596959
249558965
679275202
760593154
725418770
...

user output
107596959
249558965
679275202
760593154
725418770
...

Test 15

Group: 4

Verdict: ACCEPTED

input
200000 200000
663307073 663307073 663307073 ...

correct output
211070558
49212342
651109313
264549124
651109313
...

user output
211070558
49212342
651109313
264549124
651109313
...

Test 16

Group: 4

Verdict: ACCEPTED

input
200000 200000
2 2 2 1 2 1 1 2 2 1 1 1 1 2 1 ...

correct output
1
2
1
1
1
...

user output
1
2
1
1
1
...