Submission details
Task:Kyselyt
Sender:Metabolix
Submission time:2025-10-19 10:20:04 +0300
Language:C++ (C++20)
Status:READY
Result:100
Feedback
groupverdictscore
#1ACCEPTED10
#2ACCEPTED20
#3ACCEPTED30
#4ACCEPTED40
Test results
testverdicttimegroup
#1ACCEPTED0.00 s1, 2, 3, 4details
#2ACCEPTED0.00 s1, 2, 3, 4details
#3ACCEPTED0.01 s1, 3, 4details
#4ACCEPTED0.19 s2, 3, 4details
#5ACCEPTED0.28 s2, 3, 4details
#6ACCEPTED0.23 s2, 3, 4details
#7ACCEPTED0.25 s3, 4details
#8ACCEPTED0.43 s4details
#9ACCEPTED0.64 s4details
#10ACCEPTED0.54 s4details
#11ACCEPTED0.29 s3, 4details
#12ACCEPTED0.64 s4details
#13ACCEPTED0.15 s3, 4details
#14ACCEPTED0.31 s4details
#15ACCEPTED0.32 s4details
#16ACCEPTED0.51 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);
    vector<pair<int, int>> kyselyt(q);
    vector<int> tulokset(q);
    vector<int> kyselyt_lopun_mukaan(q);

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

    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b;
        kyselyt[i] = {a, b};
        kyselyt_lopun_mukaan[i] = i;
    }
    sort(kyselyt_lopun_mukaan.begin(), kyselyt_lopun_mukaan.end(), [&](int a, int b) { return kyselyt[a].second < kyselyt[b].second; });

    vector<int> yli_puolet(N + (n+2), -1);
    unordered_map<int, map<int, int>> summataulu;

    auto summataulu_range_sum = [&](int luku, int a, int b) {
        auto it_a = prev(summataulu[luku].lower_bound(a));
        auto it_b = summataulu[luku].rbegin();
        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 || summataulu[luku].empty()) {
            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]);
    }

    int luku_i = 0;
    for (int i: kyselyt_lopun_mukaan) {
        int a, b;
        tie(a, b) = kyselyt[i];
        while (luku_i < b) {
            luku_i++;
            auto x = luvut[luku_i];
            auto &t = summataulu[x];
            if (t.empty()) {
                t[-1] = 0;
            }
            t[luku_i] = t.rbegin()->second + 1;
            binary_tree_set(luku_i, x);
        }
        int vastaus = binary_tree_result(a, b);
        tulokset[i] = vastaus;
    }
    for (int i: tulokset) {
        cout << i << "\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: 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
...