#include <bits/stdc++.h>
#define DEBUG 0
using namespace std;
const int N = 2e5 + 10;
vector <int> idx_paper[N];
class Node {
public :
int sum;
Node *l, *r;
Node(const int &s) : sum(s), l(NULL), r(NULL) {}
Node(Node *L, Node *R) : sum(0), l(L), r(R) {
if(L != NULL) {
sum += L->sum;
}
if(R != NULL) {
sum += R->sum;
}
}
Node(Node *copy) : sum(copy->sum), l(copy->l), r(copy->r) {}
};
Node *root[N];
Node *build(const int &l, const int &r) {
if(l == r) {
return new Node(0);
}
int mid = (l + r) / 2;
return new Node(build(l, mid), build(mid + 1, r));
}
Node *update(Node *node, const int &l, const int &r, const int &q) {
if(l == r) {
return new Node(1);
}
int mid = (l + r) / 2;
if(q <= mid) {
return new Node(update(node->l, l, mid, q), node->r);
}
return new Node(node->l, update(node->r, mid + 1, r, q));
}
int query(Node *node, const int &l, const int &r, const int &ql, const int &qr) {
if(r < ql or qr < l) {
return 0;
}
if(ql <= l and r <= qr) {
return node->sum;
}
int mid = (l + r) / 2;
return query(node->l, l, mid, ql, qr) + query(node->r, mid + 1, r, ql, qr);
}
int main() {
int n, q;
scanf(" %d %d", &n, &q);
for(int i = 1; i <= n; i++) {
int p;
scanf(" %d", &p);
idx_paper[p].push_back(i);
}
root[n + 1] = build(1, n);
for(int i = n; i >= 1; i--) {
root[i] = new Node(root[i + 1]);
for(auto idx : idx_paper[i]) {
root[i] = update(root[i], 1, n, idx);
}
}
while(q--) {
int a, b;
scanf(" %d %d", &a, &b);
int range = b - a + 1;
int l = 1, r = range, pos = 1;
while(l <= r) {
int mid = (l + r) / 2;
if(query(root[mid], 1, n, a, b) < mid) {
r = mid - 1;
}
else {
l = mid + 1;
pos = mid;
}
}
printf("%d\n", pos);
}
return 0;
}
Compilation message
index.cpp: In function 'int main()':
index.cpp:61:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | scanf(" %d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~~
index.cpp:64:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | scanf(" %d", &p);
| ~~~~~^~~~~~~~~~~
index.cpp:78:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | scanf(" %d %d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5460 KB |
Output is correct |
2 |
Correct |
7 ms |
5460 KB |
Output is correct |
3 |
Correct |
6 ms |
5460 KB |
Output is correct |
4 |
Correct |
7 ms |
5460 KB |
Output is correct |
5 |
Correct |
6 ms |
5460 KB |
Output is correct |
6 |
Correct |
7 ms |
5468 KB |
Output is correct |
7 |
Correct |
6 ms |
5460 KB |
Output is correct |
8 |
Correct |
6 ms |
5460 KB |
Output is correct |
9 |
Correct |
6 ms |
5460 KB |
Output is correct |
10 |
Correct |
8 ms |
5460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5460 KB |
Output is correct |
2 |
Correct |
7 ms |
5460 KB |
Output is correct |
3 |
Correct |
6 ms |
5460 KB |
Output is correct |
4 |
Correct |
7 ms |
5460 KB |
Output is correct |
5 |
Correct |
6 ms |
5460 KB |
Output is correct |
6 |
Correct |
7 ms |
5468 KB |
Output is correct |
7 |
Correct |
6 ms |
5460 KB |
Output is correct |
8 |
Correct |
6 ms |
5460 KB |
Output is correct |
9 |
Correct |
6 ms |
5460 KB |
Output is correct |
10 |
Correct |
8 ms |
5460 KB |
Output is correct |
11 |
Correct |
461 ms |
37516 KB |
Output is correct |
12 |
Correct |
478 ms |
37428 KB |
Output is correct |
13 |
Correct |
474 ms |
37492 KB |
Output is correct |
14 |
Correct |
507 ms |
37488 KB |
Output is correct |
15 |
Correct |
541 ms |
37496 KB |
Output is correct |
16 |
Correct |
558 ms |
37480 KB |
Output is correct |
17 |
Correct |
555 ms |
37492 KB |
Output is correct |
18 |
Correct |
467 ms |
37612 KB |
Output is correct |
19 |
Correct |
452 ms |
37436 KB |
Output is correct |
20 |
Correct |
532 ms |
37440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
5460 KB |
Output is correct |
2 |
Correct |
7 ms |
5460 KB |
Output is correct |
3 |
Correct |
6 ms |
5460 KB |
Output is correct |
4 |
Correct |
7 ms |
5460 KB |
Output is correct |
5 |
Correct |
6 ms |
5460 KB |
Output is correct |
6 |
Correct |
7 ms |
5468 KB |
Output is correct |
7 |
Correct |
6 ms |
5460 KB |
Output is correct |
8 |
Correct |
6 ms |
5460 KB |
Output is correct |
9 |
Correct |
6 ms |
5460 KB |
Output is correct |
10 |
Correct |
8 ms |
5460 KB |
Output is correct |
11 |
Correct |
461 ms |
37516 KB |
Output is correct |
12 |
Correct |
478 ms |
37428 KB |
Output is correct |
13 |
Correct |
474 ms |
37492 KB |
Output is correct |
14 |
Correct |
507 ms |
37488 KB |
Output is correct |
15 |
Correct |
541 ms |
37496 KB |
Output is correct |
16 |
Correct |
558 ms |
37480 KB |
Output is correct |
17 |
Correct |
555 ms |
37492 KB |
Output is correct |
18 |
Correct |
467 ms |
37612 KB |
Output is correct |
19 |
Correct |
452 ms |
37436 KB |
Output is correct |
20 |
Correct |
532 ms |
37440 KB |
Output is correct |
21 |
Execution timed out |
2582 ms |
147192 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |