#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
struct BIT {
int n;
vector<int> tree;
void config(int _n) {
n = _n + 10;
tree.resize(_n+60);
}
void update(int p, int v) {
for(p++; p<n; p+=p&-p) tree[p] += v;
}
int query(int p) {
int ans = 0;
for(p++; p>0; p-=p&-p) ans += tree[p];
return ans;
}
int query(int l, int r) { return query(r) - query(l-1); }
void reset() { for(int &x : tree) x = 0; }
};
stack<int> to_change[200005];
vector<int> v[200005];
int32_t main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> L(q), R(q, 2e5), ANS(q, 1);
for(int i=1; i<=n; i++) {
int x;
cin >> x;
v[x].push_back(i);
}
vector<pii> qus(q);
for(pii &x : qus) cin >> x.first >> x.second;
bool changed = 1;
BIT bit; bit.config(2e5);
while(changed) {
changed = 0;
for(int i=0; i<q; i++)
if(L[i] <= R[i]) to_change[(L[i] + R[i]) / 2].push(i);
for(int i=2e5; i>=1; i--) {
for(int &x : v[i]) bit.update(x, 1);
while(!to_change[i].empty()) {
changed = 1;
int id = to_change[i].top();
to_change[i].pop();
if(bit.query(qus[id].first, qus[id].second) >= i) ANS[id] = i, L[id] = i + 1;
else R[id] = i - 1;
}
}
bit.reset();
}
for(int &x : ANS) cout << x << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
140624 KB |
Output is correct |
2 |
Correct |
105 ms |
140564 KB |
Output is correct |
3 |
Correct |
99 ms |
140620 KB |
Output is correct |
4 |
Correct |
105 ms |
140620 KB |
Output is correct |
5 |
Correct |
103 ms |
140448 KB |
Output is correct |
6 |
Correct |
103 ms |
140628 KB |
Output is correct |
7 |
Correct |
109 ms |
140700 KB |
Output is correct |
8 |
Correct |
106 ms |
140436 KB |
Output is correct |
9 |
Correct |
99 ms |
140624 KB |
Output is correct |
10 |
Correct |
104 ms |
140428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
140624 KB |
Output is correct |
2 |
Correct |
105 ms |
140564 KB |
Output is correct |
3 |
Correct |
99 ms |
140620 KB |
Output is correct |
4 |
Correct |
105 ms |
140620 KB |
Output is correct |
5 |
Correct |
103 ms |
140448 KB |
Output is correct |
6 |
Correct |
103 ms |
140628 KB |
Output is correct |
7 |
Correct |
109 ms |
140700 KB |
Output is correct |
8 |
Correct |
106 ms |
140436 KB |
Output is correct |
9 |
Correct |
99 ms |
140624 KB |
Output is correct |
10 |
Correct |
104 ms |
140428 KB |
Output is correct |
11 |
Correct |
205 ms |
143904 KB |
Output is correct |
12 |
Correct |
189 ms |
143884 KB |
Output is correct |
13 |
Correct |
178 ms |
143924 KB |
Output is correct |
14 |
Correct |
178 ms |
143692 KB |
Output is correct |
15 |
Correct |
193 ms |
143840 KB |
Output is correct |
16 |
Correct |
185 ms |
143864 KB |
Output is correct |
17 |
Correct |
186 ms |
143892 KB |
Output is correct |
18 |
Correct |
208 ms |
143888 KB |
Output is correct |
19 |
Correct |
197 ms |
143700 KB |
Output is correct |
20 |
Correct |
182 ms |
143700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
140624 KB |
Output is correct |
2 |
Correct |
105 ms |
140564 KB |
Output is correct |
3 |
Correct |
99 ms |
140620 KB |
Output is correct |
4 |
Correct |
105 ms |
140620 KB |
Output is correct |
5 |
Correct |
103 ms |
140448 KB |
Output is correct |
6 |
Correct |
103 ms |
140628 KB |
Output is correct |
7 |
Correct |
109 ms |
140700 KB |
Output is correct |
8 |
Correct |
106 ms |
140436 KB |
Output is correct |
9 |
Correct |
99 ms |
140624 KB |
Output is correct |
10 |
Correct |
104 ms |
140428 KB |
Output is correct |
11 |
Correct |
205 ms |
143904 KB |
Output is correct |
12 |
Correct |
189 ms |
143884 KB |
Output is correct |
13 |
Correct |
178 ms |
143924 KB |
Output is correct |
14 |
Correct |
178 ms |
143692 KB |
Output is correct |
15 |
Correct |
193 ms |
143840 KB |
Output is correct |
16 |
Correct |
185 ms |
143864 KB |
Output is correct |
17 |
Correct |
186 ms |
143892 KB |
Output is correct |
18 |
Correct |
208 ms |
143888 KB |
Output is correct |
19 |
Correct |
197 ms |
143700 KB |
Output is correct |
20 |
Correct |
182 ms |
143700 KB |
Output is correct |
21 |
Correct |
539 ms |
154268 KB |
Output is correct |
22 |
Correct |
522 ms |
154368 KB |
Output is correct |
23 |
Correct |
543 ms |
154276 KB |
Output is correct |
24 |
Correct |
535 ms |
154688 KB |
Output is correct |
25 |
Correct |
541 ms |
154260 KB |
Output is correct |
26 |
Correct |
566 ms |
154420 KB |
Output is correct |
27 |
Correct |
550 ms |
154340 KB |
Output is correct |
28 |
Correct |
560 ms |
154396 KB |
Output is correct |
29 |
Correct |
583 ms |
154244 KB |
Output is correct |
30 |
Correct |
567 ms |
154264 KB |
Output is correct |