Submission details
Task:Kyselyt
Sender:Kuha
Submission time:2025-10-20 09:40:20 +0300
Language:C++ (C++17)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED10
#2ACCEPTED20
#3ACCEPTED30
#4ACCEPTED40
Test results
testverdicttimegroup
#1ACCEPTED0.07 s1, 2, 3, 4details
#2ACCEPTED0.07 s1, 2, 3, 4details
#3ACCEPTED0.07 s1, 3, 4details
#4ACCEPTED0.35 s2, 3, 4details
#5ACCEPTED0.36 s2, 3, 4details
#6ACCEPTED0.36 s2, 3, 4details
#7ACCEPTED0.46 s3, 4details
#8ACCEPTED0.67 s4details
#9ACCEPTED0.70 s4details
#10ACCEPTED0.92 s4details
#11ACCEPTED0.41 s3, 4details
#12ACCEPTED0.80 s4details
#13ACCEPTED0.37 s3, 4details
#14ACCEPTED0.69 s4details
#15ACCEPTED0.67 s4details
#16ACCEPTED0.53 s4details

Code

#include <bits/stdc++.h>
 
#define pii pair<int, int>
#define pdd pair<double, double>
#define ull unsigned long long
#define uint unsigned int
#define ll long long
#define INF 999999999
#define LINF 999999999999999999
#define M 1000000007
#define E 0.0000001
#define N (1<<19)
 
using namespace std;
 
map<int, vector<int>> m;
 
int v[N + 2][19];
int a[N + 2][19];
 
pii ans (int l, int r, int x, int y, int i, int j) {
    if (y < l || x > r) return {0, 0};
    //cout<<l<<" "<<r<<" "<<x<<" "<<y<<" "<<i<<" "<<j<<endl;
    if (l == x && r == y) return {v[i][j], a[i][j]};
    int d = r - l + 1;
    d /= 2;
    pii n = ans(l, r - d, x, min(y, r - d), 2 * i, j - 1);
    pii m = ans(l + d, r, max(x, l + d), y, 2 * i + 1, j - 1);
    //cout<<"DEBUG "<<m.first<<" "<<m.second<<endl;
    if (n.first == m.first) return {n.first, n.second + m.second};
    if (n.second == m.second) return {0, 0};
    if (n.second > m.second) return {n.first, n.second - m.second};
    return {m.first, m.second - n.second};
}
 
int main () {
    cin.sync_with_stdio(false);
    cin.tie(0);
    int n, k;
    cin>>n>>k;
    
    for (int i = 0; i < N; i++) v[i][0] = -1 - i, a[i][0] = 0;
 
    for (int i = 0; i < n; i++) {
        ll k;
        cin>>k;
        v[i][0] = k;
        a[i][0] = 1;
        m[k].push_back(i);
    }
 
    int l = N;
    for (int i = 1; i < 19; i++) {
        l /= 2;
        for (int j = 0; j < l; j++) {
            if (v[2 * j][i - 1] == v[2 * j + 1][i - 1]) {
                v[j][i] = v[2 * j][i - 1];
                a[j][i] = a[2 * j][i - 1] + a[2 * j + 1][i - 1];
            } else if (a[2 * j][i - 1] == a[2 * j + 1][i - 1]) {
                v[j][i] = 0;
                a[j][i] = 0;
            } else if (a[2 * j][i - 1] > a[2 * j + 1][i - 1]) {
                v[j][i] = v[2 * j][i - 1];
                a[j][i] = a[2 * j][i - 1] - a[2 * j + 1][i - 1];
            } else {
                v[j][i] = v[2 * j + 1][i - 1];
                a[j][i] = a[2 * j + 1][i - 1] - a[2 * j][i - 1];
            }
        }
    }
 
    //for (int i = 0; i < 4; i++) {
    //  for (int j = 0; j < 8; j++) cout<<v[j][i]<<" ";
    //  cout<<endl;
    //}
 
    for (int i = 0; i < k; i++) {
        int a, b;
        cin>>a>>b;
        a--;
        b--;
        int c = ans(0, N - 1, a, b, 0, 19).first;
        if (c == 0) cout<<-1<<endl;
        else {
            auto x = lower_bound(m[c].begin(), m[c].end(), a);
            auto y = upper_bound(m[c].begin(), m[c].end(), b);
            if (2 * (int)(y - x) > b - a + 1) cout<<c<<endl;
            else cout<<-1<<endl;
        }
    }
}

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: ACCEPTED

input
200000 200000
286470749 280175209 741317063 ...

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

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

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: ACCEPTED

input
200000 200000
613084013 1000000000 411999902...

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

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

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
...