Submission details
Task:Kyselyt
Sender:Yytsi
Submission time:2025-10-18 10:57:47 +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.00 s1, 3, 4details
#4ACCEPTED0.29 s2, 3, 4details
#5ACCEPTED0.31 s2, 3, 4details
#6ACCEPTED0.45 s2, 3, 4details
#7ACCEPTED0.43 s3, 4details
#8ACCEPTED0.72 s4details
#9ACCEPTED0.75 s4details
#10ACCEPTED0.91 s4details
#11ACCEPTED0.44 s3, 4details
#12ACCEPTED0.99 s4details
#13ACCEPTED0.23 s3, 4details
#14ACCEPTED0.51 s4details
#15ACCEPTED0.57 s4details
#16ACCEPTED0.50 s4details

Compiler report

input/code.cpp: In function 'void solveRange(int, int)':
input/code.cpp:58:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   58 |     if (i == freq.size()) return void(cout << "-1\n");
      |         ~~^~~~~~~~~~~~~~

Code

#include <bits/stdc++.h>
#include <random>

using namespace std;
using ll = long long;
 
#ifdef LOCAL
#include "debug.hpp"
#include "pbds.h"
#else
#define debug(...) 0xffff
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#endif
 
#define N 202020
#define M (1<<18)
#define INF 2147483600
 
random_device rd;
mt19937 gen(rd());
unordered_map<int, ordered_set<int>> h;
 
int arr[N], usedInQuery[N], cnts[N];
 
int n, q, queryIndex = 0;
 
void solveRange(int a, int b) {
  // find 2 best candidates between [a..b] that could be dominant
  // max iter? maybe 50-100 or something
  int max_iter = 25;
  unordered_map<int, int> cnts;
  cnts.reserve(64);
  cnts.max_load_factor(0.7f);
  queryIndex++;
 
  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];
    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 2 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];
    int val = p.second;

    int cnt = h[val].order_of_key(b+1) - h[val].order_of_key(a);

    if (cnt > (b-a+1)/2) {
      return void(cout << val << "\n");
    }
  }
 
  cout << "-1\n";
}
 
int main() {
  ios_base::sync_with_stdio(0); cin.tie(0);
  cin>>n>>q;
 
  for (int i = 1; i <= n; i++) {
    cin>>arr[i];
    h[arr[i]].insert(i);
  }
 
  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: 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
...