#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
int32_t main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
int n, q;
cin >> n >> q;
vector<int> v(n+1), L(q), R(q, 2e5), ANS(q, 1);
for(int i=1; i<=n; i++) cin >> v[i];
vector<pii> qus(q);
for(pii &x : qus) cin >> x.first >> x.second;
bool changed = 1;
while(changed) {
changed = 0;
map<int, stack<int> > to_change;
for(int i=0; i<q; i++)
if(L[i] <= R[i]) to_change[(L[i] + R[i]) / 2].push(i);
for(auto &[mid, st] : to_change) {
vector<int> pref(n+1);
for(int i=1; i<=n; i++) pref[i] = pref[i-1] + (v[i] >= mid);
while(!st.empty()) {
changed = 1;
int id = st.top();
st.pop();
if(pref[qus[id].second] - pref[qus[id].first-1] >= mid) ANS[id] = mid, L[id] = mid + 1;
else R[id] = mid - 1;
}
}
}
for(int &x : ANS) cout << x << '\n';
return 0;
}
Compilation message
index.cpp: In function 'int32_t main()':
index.cpp:27:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
27 | for(auto &[mid, st] : to_change) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
384 ms |
2752 KB |
Output is correct |
12 |
Correct |
377 ms |
2772 KB |
Output is correct |
13 |
Correct |
384 ms |
2704 KB |
Output is correct |
14 |
Correct |
376 ms |
2700 KB |
Output is correct |
15 |
Correct |
380 ms |
2768 KB |
Output is correct |
16 |
Correct |
380 ms |
2748 KB |
Output is correct |
17 |
Correct |
384 ms |
2764 KB |
Output is correct |
18 |
Correct |
388 ms |
2700 KB |
Output is correct |
19 |
Correct |
384 ms |
2956 KB |
Output is correct |
20 |
Correct |
383 ms |
2700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
384 ms |
2752 KB |
Output is correct |
12 |
Correct |
377 ms |
2772 KB |
Output is correct |
13 |
Correct |
384 ms |
2704 KB |
Output is correct |
14 |
Correct |
376 ms |
2700 KB |
Output is correct |
15 |
Correct |
380 ms |
2768 KB |
Output is correct |
16 |
Correct |
380 ms |
2748 KB |
Output is correct |
17 |
Correct |
384 ms |
2764 KB |
Output is correct |
18 |
Correct |
388 ms |
2700 KB |
Output is correct |
19 |
Correct |
384 ms |
2956 KB |
Output is correct |
20 |
Correct |
383 ms |
2700 KB |
Output is correct |
21 |
Execution timed out |
2566 ms |
8416 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |