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 IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << "Line(" << __LINE__ << ") -> " << #x << " is " << (x) << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, N = 1e6 + 7;
int a[N], ans[N], mx[N][20];
V<array<int, 3>> G[N];
int bit[N];
void add(int pos, int val) {
pos++;
for(; pos < N; pos += pos & -pos)
bit[pos] = max(bit[pos], val);
}
int qry(int pos) {
pos++;
int res = 0;
for(; pos; pos -= pos & -pos)
res = max(res, bit[pos]);
return res;
}
int query(int l, int r, int less_than) {
int ans = -INF;
for(int i = l; i <= r; i++) {
if(a[i] < less_than)
ans = max(ans, a[i]);
}
return ans;
}
signed main()
{
IO_OP;
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i++)
cin >> a[i];
for(int i = 0; i < m; i++) {
int l, r, k;
cin >> l >> r >> k;
l--, r--;
G[l].PB({r, k, i});
}
a[n] = INF;
deque<int> cut;
cut.PB(n);
for(int i = n - 1; i >= 0; i--) {
while(a[i] >= a[cut[0]]) cut.pop_front();
int r = cut[0], mx = query(i, r, a[i]);
add(r, a[i] + mx);
cut.push_front(i);
for(auto qq:G[i]) {
int r = qq[0], k = qq[1], qi = qq[2];
int tt = qry(r);
int l = cut[upper_bound(ALL(cut), r) - cut.begin() - 1];
int mx = query(l, r, a[l]), a_mx = a[l];
tt = max(tt, a_mx + mx);
if(tt <= k) ans[qi] = 1;
else ans[qi] = 0;
}
}
for(int i = 0; i < m; i++)
cout << ans[i] << '\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... |