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;
using i64 = long long;
const int MAXN = 1e6 + 5;
struct event {
int l, r, x, p;
event(int l = 0, int r = 0, int x = 0, int p = 0) : l(l), r(r), x(x), p(p) {}
};
int N, Q;
int a[MAXN];
event query[MAXN];
int ans[MAXN];
struct segment_tree {
int N;
int tree[(1 << 21) + 5];
void init(int _N) {
N = _N;
}
void update(int p, int x) {
int id = 1, l = 1, r = N;
while(l < r) {
int m = (l + r) / 2;
if(p <= m) {
id = id * 2;
r = m;
} else {
id = id * 2 + 1;
l = m + 1;
}
}
tree[id] = x;
while(id >= 1) {
id >>= 1;
tree[id] = max(tree[id * 2], tree[id * 2 + 1]);
}
}
int get(int id, int l, int r, int u, int v) {
if(r < u or l > v) return 0;
if(u <= l and r <= v) return tree[id];
int m = (l + r) / 2;
return max(get(id * 2, l, m, u, v), get(id * 2 + 1, m + 1, r, u, v));
}
int get(int l, int r) {
return get(1, 1, N, l, r);
}
} ST;
signed main() {
#define TASK "code"
if (fopen(TASK ".inp", "r")) {
freopen(TASK ".inp", "r", stdin);
freopen(TASK ".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> N >> Q;
for(int i = 1; i <= N; i++) cin >> a[i];
for(int i = 1; i <= Q; i++) {
auto &[l, r, x, p] = query[i];
cin >> l >> r >> x;
p = i;
}
sort(query + 1, query + Q + 1, [&](const event a, const event b) {
return a.r < b.r;
});
for(int i = 1; i <= Q; i++) {
auto [l, r, x, p] = query[i];
// cerr << l << " " << r << " " << x << " " << p << "\n";
}
int j = 1;
ST.init(N);
stack<int> S;
for(int i = 1; i <= N; i++) {
while(!S.empty() and a[S.top()] <= a[i])
S.pop();
if(!S.empty()) ST.update(S.top(), a[S.top()] + a[i]);
S.push(i);
while(j <= Q and query[j].r < i) j++;
while(j <= Q and query[j].r == i) {
ans[query[j].p] = (ST.get(query[j].l, query[j].r) <= query[j].x);
j++;
}
}
for(int i = 1; i <= Q; i++) cout << ans[i] << "\n";
return (0 ^ 0);
}
Compilation message (stderr)
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:78:14: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
78 | auto [l, r, x, p] = query[i];
| ^~~~~~~~~~~~
sortbooks.cpp:59:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | freopen(TASK ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen(TASK ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |