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 endl "\n"
template<class T, class T2> bool chkmax(T &a, const T2 &b) {return (a < b) ? a = b, 1 : 0;}
const int mod = 1e9 + 7;
const int MAX_N = 1e6 + 10;
struct Node : vector<int> {
int mxSum;
Node() : vector<int>(0), mxSum(0) {}
};
Node operator +(const Node &a, const Node &b) {
Node ret;
//cout << "Merging " << endl;
merge(a.begin(), a.end(), b.begin(), b.end(), back_inserter(ret));
auto curr = lower_bound(b.begin(), b.end(), a.back());
if(curr == b.begin()) {
ret.mxSum = 0;
} else {
curr --;
ret.mxSum = a.back() + (*curr);
}
chkmax(ret.mxSum, max(a.mxSum, b.mxSum));
return ret;
}
Node tree[4 * MAX_N];
int arr[MAX_N];
void build(int curr, int l, int r) {
//cout << curr << " " << l << " " << r << endl;
if(l == r) {
//cout << "1" << endl;
tree[curr].push_back(arr[l]);
//cout << 2 << endl;
return;
}
//cout << curr << " " << l << " " << r << endl;
int m = (l + r) / 2;
build(curr * 2, l, m);
build(curr * 2 + 1, m + 1, r);
//cout << curr << "!! " << l << " " << r << endl;
tree[curr] = tree[curr * 2] + tree[curr * 2 + 1];
//cout << curr << "! " << l << " " << r << endl;
}
pair<int, int> getMaxInvSum(int curr, int l, int r, int ql, int qr, int got) {
if(ql <= l && r <= qr) {
auto now = lower_bound(tree[curr].begin(), tree[curr].end(), got);
if(now == tree[curr].begin()) {
return {tree[curr].mxSum, tree[curr].back()};
} else {
now --;
return {max(tree[curr].mxSum, got + (*now)), tree[curr].back()};
}
} else if(l > qr || r < ql) {
return {-mod, -mod};
}
int m = (l + r) / 2;
auto retr = getMaxInvSum(curr * 2, l, m, ql, qr, got);
auto retl = getMaxInvSum(curr * 2 + 1, m + 1, r, ql, qr, max(got, retr.second));
pair<int, int> ret;
ret.first = max(retr.first, retl.first);
ret.second = max(retr.second, retl.second);
return ret;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
int n;
cin >> n;
int q;
cin >> q;
for(int i = 0; i < n; i ++) {
cin >> arr[i];
}
build(1, 0, n - 1);
//cout << "Here" << endl;
while(q --) {
int l, r, k;
cin >> l >> r >> k;
cout << (getMaxInvSum(1, 0, n - 1, l - 1, r - 1, -mod).first <= k) << endl;
//cout << l << " " << r << " " << k << endl;
}
}
# | 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... |