#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin() , v.end()
const int N = 1e6+55;
int a[N] , lef[N] , mx[4*N] , ans[N];
int n, m;
void upd(int pos , int val , int u = 1, int ul = 1, int ur = n) {
if(ul == ur) {
mx[u] = val;
return;
}
int um = ul+ur >> 1;
if(pos <= um) upd(pos , val , u<<1 , ul , um);
else upd(pos , val , u<<1|1 , um+1 , ur);
mx[u] = max(mx[u<<1] , mx[u<<1|1]);
}
int get(int l , int r , int u = 1 , int ul = 1 , int ur = n) {
if(l <= ul && ur <= r) return mx[u];
if(r < ul || ur < l) return 0;
int um = ul+ur >> 1;
return max(get(l , r , u<<1 , ul , um) , get(l , r , u<<1|1 , um+1 , ur));
}
int main() {
cin >> n >> m;
for(int i = 1; i <= n; i ++) {
cin >> a[i];
}
stack<int> st;
for(int i = 1; i <= n; i ++) {
lef[i] = i;
while(st.size() && a[i] >= a[st.top()]) {
lef[i] = lef[st.top()];
st.pop();
}
st.push(i);
}
for(int i = 1; i <= n; i ++) lef[i] --;
vector<array<int,4>> q;
for(int i = 1, l, r, k; i <= m; i ++) {
cin >> l >> r >> k;
q.push_back({r , l, k , i});
}
sort(all(q));
for(auto [r , l , k , i]: q) {
if(lef[r] != 0) {
upd(lef[r] , a[r] + a[lef[r]]);
lef[r] = 0;
}
ans[i] = (get(l , r) <= k);
}
for(int i = 1; i <= m; i ++) {
cout << ans[i] << '\n';
}
}
Compilation message
sortbooks.cpp: In function 'void upd(int, int, int, int, int)':
sortbooks.cpp:14:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
14 | int um = ul+ur >> 1;
| ~~^~~
sortbooks.cpp: In function 'int get(int, int, int, int, int)':
sortbooks.cpp:24:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
24 | int um = ul+ur >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1928 ms |
38000 KB |
Output is correct |
2 |
Incorrect |
1979 ms |
37904 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
171 ms |
4400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |