#include <cstdio>
#include <cstring>
#include <cassert>
#include <string>
#include <deque>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;
using ll=long long;
#define N 200005
#define ALL(x) x.begin(), x.end()
struct node
{
node *l, *r;
int a;
node(int b) : a(b), l(nullptr), r(nullptr) {}
node (node *l_, node *r_) : l(l_), r(r_), a((l_?l_->a:0) + (r_?r_->a:0)) {}
};
node *build(int l, int r)
{
if (l == r) return new node(0);
return new node(build(l, (l+r)/2), build((l+r)/2+1, r));
}
node *upd(node *v, int l, int r, int p)
{
if (l == r) return new node(v->a + 1);
if (p <= (l+r)/2) return new node(upd(v->l, l, (l+r)/2, p), v->r);
return new node(v->l, upd(v->r, (l+r)/2+1, r, p));
}
int count(node *vl, node *vr, int l, int r, int x, int y)
{
if (y < l || r < x) return 0;
if (x <= l && r <= y) return vr->a - vl->a;
return count(vl->l, vr->l, l, (l+r)/2, x, y) + count(vl->r, vr->r, (l+r)/2+1, r, x, y);
}
int n, q;
node *root[N];
int main()
{
scanf("%d%d", &n, &q);
root[0] = build(1, n);
for (int x, i = 1; i <= n; ++i)
scanf("%d", &x), root[i] = upd(root[i-1], 1, n, x);
for (int x, y; q--;)
{
scanf("%d%d", &x, &y);
int l = 0, r = y-x+1, z = -1;
while (l <= r)
{
int m = (l+r)/2;
if (count(root[x-1], root[y], 1, n, m, n) < m)
r = m - 1;
else
z = m, l = m + 1;
}
printf("%d\n", z);
}
return 0;
}
Compilation message
index.cpp: In constructor 'node::node(int)':
index.cpp:21:9: warning: 'node::a' will be initialized after [-Wreorder]
21 | int a;
| ^
index.cpp:20:11: warning: 'node* node::l' [-Wreorder]
20 | node *l, *r;
| ^
index.cpp:22:5: warning: when initialized here [-Wreorder]
22 | node(int b) : a(b), l(nullptr), r(nullptr) {}
| ^~~~
index.cpp: In function 'int main()':
index.cpp:51:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
index.cpp:54:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
54 | scanf("%d", &x), root[i] = upd(root[i-1], 1, n, x);
| ~~~~~^~~~~~~~~~
index.cpp:58:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
58 | scanf("%d%d", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
856 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
2 ms |
704 KB |
Output is correct |
5 |
Correct |
2 ms |
856 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
628 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
856 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
2 ms |
704 KB |
Output is correct |
5 |
Correct |
2 ms |
856 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
628 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
176 ms |
31080 KB |
Output is correct |
12 |
Correct |
179 ms |
30960 KB |
Output is correct |
13 |
Correct |
209 ms |
31048 KB |
Output is correct |
14 |
Correct |
174 ms |
31060 KB |
Output is correct |
15 |
Correct |
187 ms |
31176 KB |
Output is correct |
16 |
Correct |
168 ms |
31056 KB |
Output is correct |
17 |
Correct |
183 ms |
31092 KB |
Output is correct |
18 |
Correct |
210 ms |
30956 KB |
Output is correct |
19 |
Correct |
168 ms |
31060 KB |
Output is correct |
20 |
Correct |
173 ms |
31048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
856 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
2 ms |
704 KB |
Output is correct |
5 |
Correct |
2 ms |
856 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
2 ms |
628 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
176 ms |
31080 KB |
Output is correct |
12 |
Correct |
179 ms |
30960 KB |
Output is correct |
13 |
Correct |
209 ms |
31048 KB |
Output is correct |
14 |
Correct |
174 ms |
31060 KB |
Output is correct |
15 |
Correct |
187 ms |
31176 KB |
Output is correct |
16 |
Correct |
168 ms |
31056 KB |
Output is correct |
17 |
Correct |
183 ms |
31092 KB |
Output is correct |
18 |
Correct |
210 ms |
30956 KB |
Output is correct |
19 |
Correct |
168 ms |
31060 KB |
Output is correct |
20 |
Correct |
173 ms |
31048 KB |
Output is correct |
21 |
Correct |
1396 ms |
134592 KB |
Output is correct |
22 |
Correct |
1364 ms |
134976 KB |
Output is correct |
23 |
Correct |
1339 ms |
134772 KB |
Output is correct |
24 |
Correct |
1391 ms |
134864 KB |
Output is correct |
25 |
Correct |
1338 ms |
134564 KB |
Output is correct |
26 |
Correct |
1393 ms |
134868 KB |
Output is correct |
27 |
Correct |
1369 ms |
134880 KB |
Output is correct |
28 |
Correct |
1341 ms |
134808 KB |
Output is correct |
29 |
Correct |
1314 ms |
134740 KB |
Output is correct |
30 |
Correct |
1360 ms |
134704 KB |
Output is correct |