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 int long long
#define f first
#define s second
#define pii pair<int,int>
using namespace std;
const int N = 1e6 + 5, mod = 1e9 + 7; // !
int t, a[N], tree[4 * N], lazy[4 * N], n, m;
vector<pair<pii,int> > q[N];
void push(int u,int l,int r) {
tree[u] += lazy[u];
if(l != r) {
lazy[2 * u] += lazy[u];
lazy[2 * u + 1] += lazy[u];
}
lazy[u] = 0;
}
void upd(int u,int st, int en, int l,int r,int val) {
push(u, l, r);
if(l > en || r < st) return;
if(st <= l && r <= en) {
lazy[u] = val; push(u, l, r);
return;
}
int mid = (l + r) / 2;
upd(2 * u, st, en, l, mid, val); upd(2 * u + 1, st, en, mid + 1, r, val);
tree[u] = max(tree[2 * u], tree[2 * u + 1]);
}
int get(int u,int st,int en,int l,int r) {
push(u, l, r);
if(l > en || r < st) return 0;
if(st <= l && r <= en) return tree[u];
int mid = (l + r) / 2;
return max(get(2 * u, st, en, l, mid), get(2 * u + 1, st, en, mid + 1, r));
}
main(){
ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin >> n >> m;
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;
q[l].push_back({{r, k}, i});
}
stack<pair<pii, pii> > st;
vector<int> ans(m + 5);
for(int i = n; i >= 1; i--) {
int r = i;
while(st.size() && st.top().f.f < a[i]) {
r = st.top().s.s;
if(st.top().f.s == -1) upd(1, st.top().s.f, st.top().s.s, 1, n, st.top().f.f + a[i]);
else
upd(1, st.top().s.f, st.top().s.s, 1, n, - st.top().f.s + a[i]);
st.pop();
}
if(r > i)
st.push({{a[i], a[i]}, {i + 1, r}});
st.push({{a[i], -1}, {i, i}});
for(int j = 0; j < q[i].size(); j++) {
ans[q[i][j].s] = (get(1,i,q[i][j].f.f,1,n) <= q[i][j].f.s);
}
}
for(int i = 1; i <= m; i++) cout << ans[i] <<" ";
}
Compilation message (stderr)
sortbooks.cpp:36:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
36 | main(){
| ^~~~
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:59:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int j = 0; j < q[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... |