Submission details
Task:Kyselyt
Sender:Yytsi
Submission time:2025-10-17 20:39:23 +0300
Language:C++ (C++20)
Status:READY
Result:10
Feedback
groupverdictscore
#1ACCEPTED10
#20
#30
#40
Test results
testverdicttimegroup
#1ACCEPTED0.00 s1, 2, 3, 4details
#2ACCEPTED0.00 s1, 2, 3, 4details
#3ACCEPTED0.00 s1, 3, 4details
#4ACCEPTED0.30 s2, 3, 4details
#50.32 s2, 3, 4details
#6ACCEPTED0.39 s2, 3, 4details
#7ACCEPTED0.61 s3, 4details
#8ACCEPTED0.61 s4details
#90.66 s4details
#10--4details
#110.47 s3, 4details
#120.93 s4details
#130.34 s3, 4details
#140.71 s4details
#150.68 s4details
#160.54 s4details

Compiler report

input/code.cpp: In constructor 'Node::Node(int)':
input/code.cpp:26:9: warning: 'Node::r' will be initialized after [-Wreorder]
   26 |   Node* r;
      |         ^
input/code.cpp:24:7: warning:   'int Node::val' [-Wreorder]
   24 |   int val;
      |       ^~~
input/code.cpp:27:3: warning:   when initialized here [-Wreorder]
   27 |   Node(int v) : l(nullptr), r(nullptr), val(v) {}
      |   ^~~~
input/code.cpp: In constructor 'Node::Node(Node*, Node*)':
input/code.cpp:26:9: warning: 'Node::r' will be initialized after [-Wreorder]
   26 |   Node* r;
      |         ^
input/code.cpp:24:7: warning:   'int Node::val' [-Wreorder]
   24 |   int val;
      |       ^~~
input/code.cpp:28:3: warning:   when initialized here [-Wreorder]
   28 |   Node(Node* a, Node* b) : l(a), r(b), val(0) {
      |   ^~~~
input/code.cpp: In function 'void solveRange(int, int)':
input/code.cpp:90:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >...

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<<30)
#define INF 2147483600

random_device rd;
mt19937 gen(rd());

int arr[N];

int n, q;

struct Node {
  int val;
  Node* l;
  Node* r;
  Node(int v) : l(nullptr), r(nullptr), val(v) {}
  Node(Node* a, Node* b) : l(a), r(b), val(0) {
    if (l) val += l->val;
    if (r) val += r->val;
  }
};

Node* roots[202020];

// add 1 to number: cnt
Node* update(Node* cur, int pos, int tl, int tr) {
  if (tl == tr) return new Node((cur ? cur->val : 0) + 1);
  int tm = (tl + tr) / 2;

  Node* left_tree = cur ? cur->l : nullptr;
  Node* right_tree = cur ? cur->r : nullptr;

  if (pos <= tm) return new Node(update(left_tree, pos, tl, tm), right_tree);
  else return new Node(left_tree, update(right_tree, pos, tm+1, tr));
}

pair<int, int> query(Node* a, Node* b, int pos, int tl, int tr) {
  if (tl == tr) {
    int bv = b ? b->val : 0;
    int av = a ? a->val : 0;
    return make_pair(bv - av, tl);
  }
  int tm = (tl + tr) / 2;

  Node* a_left = a ? a->l : nullptr;
  Node* b_left = b ? b->l : nullptr;

  if (pos <= tm) return query(a_left, b_left, pos, tl, tm);

  Node* a_right = a ? a->r : nullptr;
  Node* b_right = b ? b->r : nullptr;

  return query(a_right, b_right, pos, tm+1, tr);
}

void solveRange(int a, int b) {
  // find 3 best candidates between [a..b] that could be dominant
  // max iter? maybe 50-100 or something
  int max_iter = 10;
  uniform_int_distribution<> dst(a, b);
  unordered_map<int, int> cnts;

  for (int i = 0; i < max_iter; i++) {
    int idxbetween = dst(gen);
    int x = arr[idxbetween];
    cnts[x]++;
  }

  vector<pair<int, int>> freq;
  for (const auto& p : cnts) {
    freq.push_back({p.second, p.first});
  }

  sort(freq.begin(), freq.end(), [](const auto& l, const auto& r) { return l.first > r.first; });

  // try 3 best
  int TRYDIFF = 2;
  for (int i = 0; i < TRYDIFF; i++) {
    if (i == freq.size()) return void(cout << "-1\n");
    pair<int, int> p = freq[i];
    // cout << "for range "<<a<<" "<<b<<" one candidate is element "<<p.second<<" which was sampled "<<p.first<<" times\n";
    pair<int, int> resFromPersistent = query(roots[a-1], roots[b], p.second, 0, M-1);
    // cout << resFromPersistent.first << " " << resFromPersistent.second << "\n";
    if (resFromPersistent.first > (b-a+1)/2) {
      return void(cout << resFromPersistent.second << "\n");
    }
  }

  cout << "-1\n";
}

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);
  cin>>n>>q;
  

  roots[0] = new Node(0);
  for (int i = 1; i <= n; i++) {
    int x; cin>>x;
    arr[i] = x;
    roots[i] = update(roots[i-1], x, 0, M-1);
  }

  for (int i = 0; i < q; i++) {
    int a, b; cin>>a>>b;
    solveRange(a, b);
  }
}

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:

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:

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:

input
200000 200000
286470749 280175209 741317063 ...

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

user output
(empty)

Test 11

Group: 3, 4

Verdict:

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:

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:

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:

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:

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:

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