Submission details
Task:Kyselyt
Sender:ArktinenKarpalo
Submission time:2025-10-18 13:37:50 +0300
Language:C++ (C++17)
Status:READY
Result:10
Feedback
groupverdictscore
#1ACCEPTED10
#20
#30
#40
Test results
testverdicttimegroup
#1ACCEPTED0.03 s1, 2, 3, 4details
#2ACCEPTED0.03 s1, 2, 3, 4details
#3ACCEPTED0.04 s1, 3, 4details
#4ACCEPTED0.21 s2, 3, 4details
#5ACCEPTED0.30 s2, 3, 4details
#6--2, 3, 4details
#7--3, 4details
#8ACCEPTED0.41 s4details
#9ACCEPTED0.65 s4details
#10--4details
#11--3, 4details
#12--4details
#13ACCEPTED0.26 s3, 4details
#14ACCEPTED0.48 s4details
#15ACCEPTED0.52 s4details
#16ACCEPTED0.39 s4details

Code

#include <bits/stdc++.h>
using namespace std;
#define N (1 << 18)
typedef vector<int> VI;

map<int, int> cnts[2 * N + 10];
vector<pair<int, int>> cms[2 * N + 10];

void put(int i, int x) {
  i += N;
  cnts[i][x]++;
  i /= 2;
  while (i >= 1) {
    cnts[i][x]++;
    i /= 2;
  }
}
void init(int n, VI &x) {
  for (int i = 1; i <= n; i++) {
    put(i, x[i]);
  }
  for (int i = N; i >= 1; i--) {
    map<int, int> mp;
    for (auto u : cnts[i * 2]) {
      mp[u.first] += u.second;
    }
    for (auto u : cnts[i * 2 + 1]) {
      mp[u.first] += u.second;
    }
    for (auto u : mp)
      cms[i].push_back({u.second, u.first});
    sort(cms[i].rbegin(), cms[i].rend());
  }
  for (int i = N + 1; i <= N + n; i++)
    cms[i].push_back({1, x[i - N]});
  // for (auto u : cms[1])
  //   cout << u.first << " - " << u.second << endl;
}

int cq(int a, int b, int x, VI &xs) {
  int ret = 0;
  a += N;
  b += N;
  while (a <= b) {
    // cout<<a << " ja " << b <<endl;
    if (a % 2 == 1) {
      if (a > N)
        ret += xs[a - N] == x;
      else
        ret += cnts[a][x];
      a++;
    }
    if (b % 2 == 0) {
      if (b > N)
        ret += xs[b - N] == x;
      else
        ret += cnts[b][x];
      b--;
    }
    a /= 2;
    b /= 2;
  }
  return ret;
}

int sq(int A, int B, VI &x) {
  int a = A + N;
  int b = B + N;
  set<int> mc;
  int Q = 1;
  while (a <= b) {
    if (a % 2 == 1) {
      for (int i = 0; i < min(Q, (int)cms[a].size()); i++)
        mc.insert(cms[a][i].second);
      a++;
    }
    if (b % 2 == 0) {
      for (int i = 0; i < min(Q, (int)cms[b].size()); i++)
        mc.insert(cms[b][i].second);
      b--;
    }
    a /= 2;
    b /= 2;
  }
  for (auto u : mc) {
    int c = cq(A, B, u, x);
    if (c > (B - A + 1) / 2)
      return u;
  }
  return -1;
}

int query(int a, int b, VI &x) { return sq(a, b, x); }

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  int n, q;
  cin >> n >> q;
  vector<int> x(n + 1);
  for (int i = 1; i <= n; i++)
    cin >> x[i];
  init(n, x);
  int a, b;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    cout << query(a, b, x) << "\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:

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
(empty)

Test 7

Group: 3, 4

Verdict:

input
100000 100000
141307 493258596 365539511 222...

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

user output
(empty)

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:

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
(empty)

Test 12

Group: 4

Verdict:

input
200000 200000
613084013 1000000000 411999902...

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

user output
(empty)

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