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, C = 10;
int a[N], ans[N], mx[N][20];
V<array<int, 3>> G[N];
int bit[N];
void add(int pos, int val) {
for(; pos < N; pos += pos & -pos)
bit[pos] = max(bit[pos], val);
}
int qry(int pos) {
int res = 0;
for(; pos; pos -= pos & -pos)
res = max(res, bit[pos]);
return res;
}
struct node {
node *l, *r;
int sum;
node() {
l = r = nullptr;
sum = 0;
}
node(node* _l, node* _r) {
l = _l, r = _r;
sum = (l ? l -> sum : 0) + (r ? r -> sum : 0);
}
};
node* ver[N];
node* upd(node* t, int pos, int tl = 0, int tr = C) {
if(!t) t = new node();
if(tr - tl == 1) {
node* res = new node();
res -> sum = t -> sum + 1;
return res;
}
int tm = (tl + tr) / 2;
if(pos < tm) return new node(upd(t -> l, pos, tl, tm), t -> r);
else return new node(t -> l, upd(t -> r, pos, tm, tr));
}
int sum(node* t) {
return t ? t -> sum : 0;
}
int qry(node* t1, node* t2, int less_than, int tl = 0, int tr = C) {
if(!t1) t1 = new node();
if(!t2) t2 = new node();
if(sum(t1) - sum(t2) == 0) return -INF;
if(tr <= less_than) {
if(tr - tl == 1) return tl;
int tm = (tl + tr) / 2;
if(sum(t1 -> r) - sum(t2 -> r) > 0) return qry(t1 -> r, t2 -> r, less_than, tm, tr);
else return qry(t1 -> l, t2 -> l, less_than, tl, tm);
}
int tm = (tl + tr) / 2;
if(less_than <= tm) {
return qry(t1 -> l, t2 -> l, less_than, tl, tm);
} else {
return max(qry(t1 -> l, t2 -> l, less_than, tl, tm), qry(t1 -> r, t2 -> r, less_than, tm, tr));
}
}
// int query(int l, int r, int less_than) {
// int ans = qry(ver[r], ver[l - 1], less_than);
// return ans;
// }
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;
ver[0] = nullptr;
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++) {
cin >> a[i];
ver[i] = upd(ver[i - 1], a[i]);
}
for(int i = 0; i < m; i++) {
int l, r, k;
cin >> l >> r >> k;
G[l].PB({r, k, i});
}
a[n + 1] = INF;
deque<int> cut;
cut.PB(n + 1);
for(int i = n; i >= 1; i--) {
while(a[i] >= a[cut[0]]) cut.pop_front();
int r = cut[0], mx = query(i, r - 1, 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... |