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>
#define int long long
using namespace std;
const int N = 1e6 + 7;
int a[N], lg[N + 1], s[21][N + 1], st[21][N + 1], mx[N];
int get(int l, int r) {
if(l > r)return 0;
int le = lg[r - l + 1];
return max(s[l][le], s[r - (1 << le) + 1][le]);
}
int gett(int l, int r) {
if(l > r)return 0;
int le = lg[r - l + 1];
return max(st[l][le], st[r - (1 << le) + 1][le]);
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m;
cin>>n>>m;
for(int i = 1; i <= n; i++) {
cin>>a[i];
s[i][0] = a[i];
}
lg[1] = 0;
for (int i = 2; i <= N; i++)lg[i] = lg[i / 2] + 1;
for(int i = 1; i <= 20; i++){
for(int x = 1; x + (1 << i) - 1 <= n; x++){
s[x][i] = max(s[x][i - 1], s[x + (1 << (i - 1))][i - 1]);
}
}
set<int> ss;
for(int i = 1; i <= n; i++) {
if(get(i + 1, n) < a[i]) {
mx[i] = a[i] + get(i + 1, n);
ss.insert(-i);
st[i][0] = mx[i];
break;
}
int l = i + 1, r = n, res = i + 1;
while(l <= r) {
int m = (l + r) >> 1;
if(get(i + 1, m) >= a[i])r = m - 1, res = m;
else l = m + 1;
}
if((i + 1) == res)mx[i] = 0;
else {
mx[i] = a[i] + get(i + 1, res - 1);
ss.insert(-i);
}
st[i][0] = mx[i];
}
for(int i = 1; i <= 20; i++){
for(int x = 1; x + (1 << i) - 1 <= n; x++){
st[x][i] = max(st[x][i - 1], st[x + (1 << (i - 1))][i - 1]);
}
}
while(m --) {
int l, r, k;
cin>>l>>r>>k;
int pos = *ss.lower_bound(-r);
pos = -pos;
if(pos > r || pos < l) {
cout << "0\n";
continue;
}
int ans = gett(l, pos - 1);
ans = max(ans, a[pos] + get(pos + 1, r));
cout << ((ans > k) ? 0 : 1) << '\n';
}
}
# | 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... |