Submission details
Task:Kyselyt
Sender:ArktinenKarpalo
Submission time:2025-10-18 13:54:45 +0300
Language:C++ (C++17)
Status:READY
Result:30
Feedback
groupverdictscore
#1ACCEPTED10
#2ACCEPTED20
#30
#40
Test results
testverdicttimegroup
#1ACCEPTED0.03 s1, 2, 3, 4details
#2ACCEPTED0.03 s1, 2, 3, 4details
#3ACCEPTED0.03 s1, 3, 4details
#4ACCEPTED0.16 s2, 3, 4details
#5ACCEPTED0.21 s2, 3, 4details
#6ACCEPTED0.29 s2, 3, 4details
#7--3, 4details
#8ACCEPTED0.31 s4details
#9ACCEPTED0.44 s4details
#10--4details
#11ACCEPTED0.89 s3, 4details
#12--4details
#13ACCEPTED0.18 s3, 4details
#14ACCEPTED0.34 s4details
#15ACCEPTED0.38 s4details
#16ACCEPTED0.31 s4details

Code

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

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

void put(int i, int x) {
  i += N;
  cnts[i][x]++;
  lz[i]++;
  i /= 2;
  while (i >= 1) {
    lz[i]++;
    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;
    if (i * 2 > N) {
      if (i * 2 - N <= n)
        mp[x[i * 2 - N]]++;
    } else {
      for (auto u : cnts[i * 2]) {
        mp[u.first] += u.second;
      }
    }
    for (auto u : cnts[i * 2 + 1]) {
      mp[u.first] += u.second;
    }
    cnts[i] = mp;
    for (auto u : mp)
      if (u.second > lz[i] / 2)
        cms[i] = u.first;
  }
  for (int i = N + 1; i <= N + n; i++)
    cms[i] = x[i - N];
}

int cq(int a, int b, int x, VI &xs) {
  int ret = 0;
  a += N;
  b += N;
  while (a <= b) {
    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;
  vector<int> mc;
  while (a <= b) {
    if (a % 2 == 1) {
      if (cms[a] && find(mc.begin(), mc.end(), cms[a]) == mc.end())
        mc.push_back(cms[a]);
      a++;
    }
    if (b % 2 == 0) {
      if (cms[b] && find(mc.begin(), mc.end(), cms[b]) == mc.end())
        mc.push_back(cms[b]);
      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: 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:

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

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