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 ll long long
const int MAXN = 1e6+5;
int tree[4*MAXN];
void upd(int i, int v, int x, int lx, int rx) {
if (lx == rx) {
tree[x] = v;
return;
}
int mid = (lx + rx) / 2;
if (i <= mid) upd(i, v, 2*x, lx, mid);
else upd(i, v, 2*x+1, mid+1, rx);
tree[x] = max(tree[2*x], tree[2*x+1]);
}
int qry(int l, int r, int x, int lx, int rx) {
if (l <= lx && rx <= r) return tree[x];
if (r < lx || rx < l) return 0;
int mid = (lx+rx) /2;
return max(qry(l,r,2*x,lx,mid), qry(l,r,2*x+1,mid+1,rx));
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
vector<int> a(n+1);
vector<vector<tuple<int,int,int>>> queries(n+1);
vector<int> ans(n+1);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
for (int i = 1; i <= m; ++i) {
int l, r, k; cin >> l >> r >> k;
queries[r].push_back({l, k, i});
}
//cout << "OK"<<endl;
stack<int> st;
for (int i = 1; i <= n; ++i) { // iterate from 1 to n over Rs in queries
while(!st.empty() && a[st.top()] <= a[i]) st.pop();
if (!st.empty()) { // Greedily include swap(st.top(), i) at st.top(), because all future Rs are >= than i
upd(st.top(), a[st.top()] + a[i], 1, 1, n);
}
for (auto x : queries[i]) {
int l = get<0>(x);
int k = get<1>(x);
int j = get<2>(x);
if (qry(l, i, 1, 1, n) <= k) ans[j] = 1;
else ans[j] = 0;
}
st.push(i);
}
for (int i = 1; i <= m; ++i) cout << ans[i] << "\n";
return 0;
}
# | 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... |