This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |