Submission details
Task:Kyselyt
Sender:Metabolix
Submission time:2025-10-18 15:32:24 +0300
Language:C++ (C++20)
Status:READY
Result:60
Feedback
groupverdictscore
#1ACCEPTED10
#2ACCEPTED20
#3ACCEPTED30
#40
Test results
testverdicttimegroup
#1ACCEPTED0.01 s1, 2, 3, 4details
#2ACCEPTED0.01 s1, 2, 3, 4details
#3ACCEPTED0.01 s1, 3, 4details
#4ACCEPTED0.50 s2, 3, 4details
#5ACCEPTED0.45 s2, 3, 4details
#6ACCEPTED0.34 s2, 3, 4details
#7ACCEPTED0.33 s3, 4details
#8--4details
#9--4details
#10ACCEPTED0.67 s4details
#11ACCEPTED0.45 s3, 4details
#12ACCEPTED0.95 s4details
#13ACCEPTED0.29 s3, 4details
#14ACCEPTED0.63 s4details
#15ACCEPTED0.70 s4details
#16ACCEPTED0.88 s4details

Code

#include <bits/stdc++.h>

using namespace std;

constexpr int N = (1 << 18);

int left(int i) { return 2 * i; }
int right(int i) { return 2 * i + 1; }
int parent(int i) { return i / 2; }
int is_left(int i) { return (~i) & 1; }
int is_right(int i) { return i & 1; }
int is_root(int i) { return i == 1; }
int is_leaf(int i) { return i >= N; }

int main() {
    ios::sync_with_stdio(false);
    int n, q;
    cin >> n >> q;
    vector<int> luvut(n+1, -1);
    unordered_map<int, int> luvut_alkuperäiseksi;
    unordered_map<int, int> luvut_tiivisteeksi;

    for (int i = 1; i <= n; i++) {
        int j0;
        cin >> j0;
        int j1 = luvut_tiivisteeksi[j0];
        luvut_tiivisteeksi[j0] = j1 = (j1 ? j1 : (int)luvut_tiivisteeksi.size());
        luvut[i] = j1;
        luvut_alkuperäiseksi[j1] = j0;
    }
    luvut_alkuperäiseksi[-1] = -1;

    const int M = luvut_tiivisteeksi.size() + 1;
    vector<int> yli_puolet(N + (n+2), -1);
    vector<map<int, int>> summataulu(M);
    vector<int> määrätaulu(M);

    for (int i = 1; i <= n; i++) {
        summataulu[luvut[i]][i] = 1;
        määrätaulu[luvut[i]] += 1;
    }
    for (auto& rivi : summataulu) {
        rivi[0] = 0;
        rivi[N+1] = 0;
        auto i = rivi.begin();
        int edellinen = 0;
        while (i != rivi.end()) {
            i->second += edellinen;
            edellinen = i->second;
            i++;
        }
    }

    auto summataulu_range_sum = [&](int luku, int a, int b) {
        auto it_a = prev(summataulu[luku].lower_bound(a));
        auto it_b = prev(summataulu[luku].upper_bound(b));
        int val_a = it_a->second;
        int val_b = it_b->second;
        return val_b - val_a;
    };

    auto summataulu_yli_puolet = [&](int luku, int a0, int b0) {
        if (luku == -1 || määrätaulu[luku] <= (b0 - a0 + 1) / 2) {
            return false;
        }
        return summataulu_range_sum(luku, a0, b0) > (b0 - a0 + 1) / 2;
    };

    auto binary_tree_result = [&](int a0, int b0) {
        int a = a0 + N, b = b0 + N;
        unordered_set<int> tutkitut;
        auto tutki = [&](int x) {
            if (!tutkitut.count(x) && summataulu_yli_puolet(x, a0, b0)) {
                return true;
            }
            tutkitut.insert(x);
            return false;
        };
        while (a <= b) {
            if (is_right(a)) {
                if (tutki(yli_puolet[a])) {
                    return yli_puolet[a];
                }
                a++;
            }
            if (is_left(b)) {
                if (tutki(yli_puolet[b])) {
                    return yli_puolet[b];
                }
                b--;
            }
            a = parent(a);
            b = parent(b);
        }
        return -1;
    };

    auto binary_tree_set = [&](int pos, int val) {
        int mask = 0;
        int i = pos + N;
        yli_puolet[i] = val;
        while ((i = parent(i))) {
            mask = 2 * mask + 1;
            int res_a = yli_puolet[left(i)];
            int res_b = yli_puolet[right(i)];
            int a = pos & ~mask, b = pos | mask;
            if (res_a == res_b || summataulu_yli_puolet(res_a, a, b)) {
                yli_puolet[i] = res_a;
            } else if (summataulu_yli_puolet(res_b, a, b)) {
                yli_puolet[i] = res_b;
            } else {
                yli_puolet[i] = -1;
            }
        }
    };

    for (int i = 1; i <= n; i++) {
        binary_tree_set(i, luvut[i]);
    }

    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        int vastaus = binary_tree_result(a, b);
        cout << luvut_alkuperäiseksi[vastaus] << "\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:

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
(empty)

Test 9

Group: 4

Verdict:

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
(empty)

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