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;
const int maxn = 1000005;
int n, q;
int a[maxn];
struct query
{
int l;
int k;
int idx;
query()
{
}
query(int _l, int _k, int _idx)
{
l = _l;
k = _k;
idx = _idx;
}
};
vector<query> queries[maxn];
int answers[maxn];
struct segment_tree
{
struct state
{
int maxv;
int lazy;
};
state tree[4 * maxn];
void push_lazy(int node, int l, int r)
{
if (tree[node].lazy == 0)
{
return;
}
tree[node].maxv += tree[node].lazy;
if (l != r)
{
tree[2 * node].lazy = max(tree[2 * node].lazy, tree[node].lazy);
tree[2 * node + 1].lazy = max(tree[2 * node + 1].lazy, tree[node].lazy);
}
tree[node].lazy = 0;
}
void update(int node, int l, int r, int ql, int qr, int val)
{
push_lazy(node, l, r);
if (l > qr || r < ql)
{
return;
}
if (ql <= l && r <= qr)
{
tree[node].lazy = max(tree[node].lazy, val);
push_lazy(node, l, r);
return;
}
int mid = (l + r) / 2;
update(2 * node, l, mid, ql, qr, val);
update(2 * node + 1, mid + 1, r, ql, qr, val);
tree[node].maxv = max(tree[2 * node].maxv, tree[2 * node + 1].maxv);
}
int query(int node, int l, int r, int ql, int qr)
{
push_lazy(node, l, r);
if (l > qr || r < ql)
{
return 0;
}
if (ql <= l && r <= qr)
{
return tree[node].maxv;
}
int mid = (l + r) / 2;
return max(query(2 * node, l, mid, ql, qr), query(2 * node + 1, mid + 1, r, ql, qr));
}
void update(int l, int r, int val)
{
update(1, 1, n, l, r, val);
}
int query(int l, int r)
{
return query(1, 1, n, l, r);
}
};
segment_tree tree;
void fastIO()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int main()
{
fastIO();
cin >> n >> q;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
}
for (int i = 1; i <= q; ++i)
{
int l, r, k;
cin >> l >> r >> k;
queries[r].push_back(query(l, k, i));
}
stack<int> st;
for (int i = 1; i <= n; ++i)
{
while (!st.empty() && a[st.top()] <= a[i])
{
st.pop();
}
if (!st.empty())
{
tree.update(1, st.top(), a[i] + a[st.top()]);
}
for (int j = 0; j < queries[i].size(); ++j)
{
int l = queries[i][j].l;
int idx = queries[i][j].idx;
if (tree.query(l, i) <= queries[i][j].k)
{
answers[idx] = 1;
}
else
{
answers[idx] = 0;
}
}
st.push(i);
}
for (int i = 1; i <= q; ++i)
{
cout << answers[i] << "\n";
}
return 0;
}
Compilation message (stderr)
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:156:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<query>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
156 | for (int j = 0; j < queries[i].size(); ++j)
| ~~^~~~~~~~~~~~~~~~~~~
# | 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... |