| Task: | Kyselyt |
| Sender: | Yytsi |
| Submission time: | 2025-10-17 22:58:26 +0300 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | 0 |
| group | verdict | score |
|---|---|---|
| #1 | WRONG ANSWER | 0 |
| #2 | WRONG ANSWER | 0 |
| #3 | WRONG ANSWER | 0 |
| #4 | WRONG ANSWER | 0 |
| test | verdict | time | group | |
|---|---|---|---|---|
| #1 | ACCEPTED | 0.00 s | 1, 2, 3, 4 | details |
| #2 | WRONG ANSWER | 0.00 s | 1, 2, 3, 4 | details |
| #3 | ACCEPTED | 0.00 s | 1, 3, 4 | details |
| #4 | ACCEPTED | 0.15 s | 2, 3, 4 | details |
| #5 | WRONG ANSWER | 0.19 s | 2, 3, 4 | details |
| #6 | ACCEPTED | 0.21 s | 2, 3, 4 | details |
| #7 | ACCEPTED | 0.43 s | 3, 4 | details |
| #8 | ACCEPTED | 0.33 s | 4 | details |
| #9 | WRONG ANSWER | 0.41 s | 4 | details |
| #10 | TIME LIMIT EXCEEDED | -- | 4 | details |
| #11 | ACCEPTED | 0.26 s | 3, 4 | details |
| #12 | WRONG ANSWER | 0.58 s | 4 | details |
| #13 | WRONG ANSWER | 0.18 s | 3, 4 | details |
| #14 | WRONG ANSWER | 0.39 s | 4 | details |
| #15 | WRONG ANSWER | 0.38 s | 4 | details |
| #16 | WRONG ANSWER | 0.33 s | 4 | details |
Code
#include <bits/stdc++.h>
#include <random>
using namespace std;
using ll = long long;
#ifdef LOCAL
#include "debug.hpp"
#else
#define debug(...) 0xffff
#endif
#define N 202020
#define M (1<<18)
#define INF 2147483600
random_device rd;
mt19937 gen(rd());
int arr[N], rev[N];
int n, q;
// index version of persistent taken from usaco guide, because my pointer based is too slow
// https://usaco.guide/adv/persistent
class PersistentSegtree {
private:
struct Node {
int sum = 0;
int l = 0, r = 0;
};
const int n;
vector<Node> tree;
int timer = 1;
Node join(int l, int r) { return Node{tree[l].sum + tree[r].sum, l, r}; }
int build(int tl, int tr) {
if (tl == tr) {
tree[timer] = {0, 0, 0};
return timer++;
}
int mid = (tl + tr) / 2;
tree[timer] = join(build(tl, mid), build(mid + 1, tr));
return timer++;
}
int set(int v, int pos, int val, int tl, int tr) {
if (tl == tr) {
tree[timer] = {tree[v].sum + val, 0, 0};
return timer++;
}
int mid = (tl + tr) / 2;
if (pos <= mid) {
tree[timer] = join(set(tree[v].l, pos, val, tl, mid), tree[v].r);
} else {
tree[timer] = join(tree[v].l, set(tree[v].r, pos, val, mid + 1, tr));
}
return timer++;
}
int pointsum(int v, int v2, int pos, int tl, int tr) {
if (tl == tr) return tree[v2].sum - tree[v].sum;
int mid = (tl + tr) / 2;
if (pos <= mid) return pointsum(tree[v].l, tree[v2].l, pos, tl, mid);
else return pointsum(tree[v].r, tree[v2].r, pos, mid+1, tr);
}
public:
PersistentSegtree(int n, int MX_NODES) : n(n), tree(MX_NODES) {}
int build() { return build(0, n - 1); }
int set(int root, int pos, int val) { return set(root, pos, val, 0, n - 1); }
int pointsum(int root, int root2, int pos) { return pointsum(root, root2, pos, 0, n - 1); }
int add_copy(int root) {
tree[timer] = tree[root];
return timer++;
}
};
int roots[N];
int queryIndex = 0;
int usedInQuery[N];
int usedValueInQuery[N];
int valueCntInQuery[N];
void solveRange(int a, int b, PersistentSegtree& seg) {
// find 2 best candidates between [a..b] that could be dominant
// max iter? maybe 50-100 or something
int max_iter = 20;
queryIndex++;
int best1 = -1, best2 = -1;
int cnt1 = 0, cnt2 = 0;
for (int i = 0; i < max_iter; i++) {
int idxbetween = a + (int)(gen() % (b-a+1));
if (usedInQuery[idxbetween] == queryIndex) continue;
usedInQuery[idxbetween] = queryIndex;
int x = arr[idxbetween];
if (usedValueInQuery[x] != queryIndex) {
usedValueInQuery[x] = queryIndex;
valueCntInQuery[x] = 0;
}
valueCntInQuery[x]++;
if (best1 == -1 || valueCntInQuery[x] > cnt1) {
best2 = best1;
cnt2 = cnt1;
best1 = x;
cnt1 = valueCntInQuery[x];
} else if (x != best1 && (best2 == -1 || valueCntInQuery[x] > cnt2)) {
best2 = x;
cnt2 = valueCntInQuery[x];
}
}
// try 2 best
for (int i = 0; i < 2; i++) {
int val = (i == 0) ? best1 : best2;
if (val == -1) continue;
int cnt = seg.pointsum(roots[a-1], roots[b], val);
// cout << resFromPersistent.first << " " << resFromPersistent.second << "\n";
if (cnt > (b-a+1)/2) {
return void(cout << rev[val] << "\n");
}
}
cout << "-1\n";
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin>>n>>q;
PersistentSegtree seg(n, 33 * n);
roots[0] = seg.build();
map<int, int> z;
for (int i = 1; i <= n; i++) {
int x; cin>>x;
arr[i] = x;
z[arr[i]] = 1;
}
int idx = 0;
for (auto& p : z) {
z[p.first] = ++idx;
rev[idx] = p.first;
}
for (int i = 1; i <= n; i++) {
arr[i] = z[arr[i]];
}
for (int i = 1; i <= n; i++) {
roots[i] = seg.set(roots[i-1], arr[i], 1);
}
for (int i = 0; i < q; i++) {
int a, b; cin>>a>>b;
solveRange(a, b, seg);
}
}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 ... Truncated |
Test 2
Group: 1, 2, 3, 4
Verdict: WRONG ANSWER
| 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 ... Truncated |
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 ... Truncated |
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 ... Truncated |
Test 5
Group: 2, 3, 4
Verdict: WRONG ANSWER
| 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 -1 1 1 ... Truncated |
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 ... Truncated |
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 ... Truncated |
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 ... Truncated |
Test 9
Group: 4
Verdict: WRONG ANSWER
| 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 -1 -1 -1 2 ... Truncated |
Test 10
Group: 4
Verdict: TIME LIMIT EXCEEDED
| 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 ... Truncated |
Test 12
Group: 4
Verdict: WRONG ANSWER
| input |
|---|
| 200000 200000 613084013 1000000000 411999902... |
| correct output |
|---|
| 1000000000 1000000000 -1 1000000000 -1 ... |
| user output |
|---|
| 1000000000 1000000000 -1 1000000000 -1 ... Truncated |
Test 13
Group: 3, 4
Verdict: WRONG ANSWER
| input |
|---|
| 100000 100000 663307073 663307073 663307073 ... |
| correct output |
|---|
| 329574367 965067805 768744535 691214891 21873594 ... |
| user output |
|---|
| 329574367 965067805 768744535 691214891 21873594 ... Truncated |
Test 14
Group: 4
Verdict: WRONG ANSWER
| input |
|---|
| 200000 200000 663307073 663307073 663307073 ... |
| correct output |
|---|
| 107596959 249558965 679275202 760593154 725418770 ... |
| user output |
|---|
| 107596959 249558965 679275202 760593154 725418770 ... Truncated |
Test 15
Group: 4
Verdict: WRONG ANSWER
| input |
|---|
| 200000 200000 663307073 663307073 663307073 ... |
| correct output |
|---|
| 211070558 49212342 651109313 264549124 651109313 ... |
| user output |
|---|
| 211070558 49212342 651109313 264549124 651109313 ... Truncated |
Test 16
Group: 4
Verdict: WRONG ANSWER
| 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 ... Truncated |
