Task: | Kyselyt |
Sender: | jhuun |
Submission time: | 2025-10-19 13:35:56 +0300 |
Language: | C++ (C++17) |
Status: | READY |
Result: | 10 |
group | verdict | score |
---|---|---|
#1 | ACCEPTED | 10 |
#2 | TIME LIMIT EXCEEDED | 0 |
#3 | TIME LIMIT EXCEEDED | 0 |
#4 | TIME LIMIT EXCEEDED | 0 |
test | verdict | time | group | |
---|---|---|---|---|
#1 | ACCEPTED | 0.01 s | 1, 2, 3, 4 | details |
#2 | ACCEPTED | 0.01 s | 1, 2, 3, 4 | details |
#3 | ACCEPTED | 0.01 s | 1, 3, 4 | details |
#4 | ACCEPTED | 0.19 s | 2, 3, 4 | details |
#5 | TIME LIMIT EXCEEDED | -- | 2, 3, 4 | details |
#6 | ACCEPTED | 0.16 s | 2, 3, 4 | details |
#7 | ACCEPTED | 0.19 s | 3, 4 | details |
#8 | ACCEPTED | 0.48 s | 4 | details |
#9 | TIME LIMIT EXCEEDED | -- | 4 | details |
#10 | ACCEPTED | 0.49 s | 4 | details |
#11 | ACCEPTED | 0.24 s | 3, 4 | details |
#12 | ACCEPTED | 0.64 s | 4 | details |
#13 | ACCEPTED | 0.07 s | 3, 4 | details |
#14 | ACCEPTED | 0.13 s | 4 | details |
#15 | ACCEPTED | 0.14 s | 4 | details |
#16 | TIME LIMIT EXCEEDED | -- | 4 | details |
Code
#include <bits/stdc++.h> constexpr auto N = 2e5; constexpr auto BS = 450; struct Query { int a; int b; int idx; bool operator<(const Query& other) const { return a / BS != other.a / BS ? a < other.a : ((a / BS) & 1)^(b > other.b); } }; std::vector<Query> Q = [](){ std::vector<Query> Q(N); for (auto i = 0; i < N; ++i) { Q[i].idx = i; } return Q; }(); std::unordered_map<int, int> XM; std::vector<int> X(N); std::vector<int> XMB(N); std::vector<int> f(N); std::vector<int> fc(N + 1); std::vector<int> fr(N + 1); int bc = 0; inline int mode_brute(int a, int b) { const auto len = (b - a + 1); std::map<int, int> map; for (; a <= b; ++a) { ++map[X[a]]; } auto max_v = 0; auto max_c = 0; for (const auto [v, c] : map) { if (c > max_c) { max_c = c; max_v = v; } } return max_c >= len / 2 + 1 ? XMB[max_v] : -1; } inline int mode(int a, int b) { const auto len = (b - a + 1); if (bc >= len / 2 + 1) { return f[fr[bc]] != bc ? mode_brute(a, b) : XMB[fr[bc]]; } return -1; } inline void add(int v) { auto& fv = f[v]; --fc[fv]; ++fv; ++fc[fv]; fr[fv] = v; bc += fv > bc; } inline void del(int v) { auto& fv = f[v]; bc -= (fv == bc && fc[fv] == 1); --fc[fv]; --fv; ++fc[fv]; } inline void add_range(int a, int b) { for (; a <= b; ++a) { add(X[a]); } } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); int n, q; std::cin >> n >> q; for (int i = 0, mi = 0, x; i < n; ++i) { std::cin >> x; if (const auto it = XM.find(x); it == XM.end()) { X[i] = mi; XMB[mi] = x; XM[x] = mi++; } else { X[i] = it->second; } } for (int i = 0; i < q; ++i) { std::cin >> Q[i].a >> Q[i].b; --Q[i].a; --Q[i].b; } std::sort(Q.begin(), Q.begin() + q); std::vector<int> res(q, -1); auto ma = Q[0].a; auto mb = Q[0].b; add_range(ma, mb); const auto v = mode(ma, mb); if (v >= 0) { res[Q[0].idx] = v; } for (int i = 1; i < q; ++i) { const auto [a, b, idx] = Q[i]; for (; ma < a && ma <= mb; ++ma) { del(X[ma]); } for (; mb > b && mb >= ma; --mb) { del(X[mb]); } if (mb < ma) { ma = a; mb = b; add_range(ma, mb); } while (ma > a) { --ma; add(X[ma]); } while (mb < b) { ++mb; add(X[mb]); } const auto v = mode(a, b); if (v >= 0) { res[idx] = v; } } for (const auto r : res) { std::cout << r << '\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: TIME LIMIT EXCEEDED
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 |
---|
(empty) |
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: TIME LIMIT EXCEEDED
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: TIME LIMIT EXCEEDED
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 |
---|
(empty) |