#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) {
mx[u] = max(mx[u] , val);
if(ul == ur) 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);
}
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 ++) {
while(st.size() && a[i] >= a[st.top()]) {
st.pop();
}
if(st.size())
lef[i] = st.top();
st.push(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:12:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
12 | int um = ul+ur >> 1;
| ~~^~~
sortbooks.cpp: In function 'int get(int, int, int, int, int)':
sortbooks.cpp:21:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
21 | int um = ul+ur >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
300 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 |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
300 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 |
1927 ms |
69820 KB |
Output is correct |
2 |
Incorrect |
1890 ms |
70892 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
142 ms |
6424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
300 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 |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
300 KB |
Output is correct |
3 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |