#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = array<int, 2>;
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
template <class Info, class Tag>
class LazySegtree {
private:
const int n;
vector<Info> tree;
vector<Tag> lazy;
void build(int v, int l, int r, const vector<Info> &a) {
if (l == r) {
tree[v] = a[l];
} else {
int m = (l + r) >> 1;
build(2 * v, l, m, a);
build(2 * v + 1, m + 1, r, a);
tree[v] = tree[2 * v] + tree[2 * v + 1];
}
}
void apply(int v, int l, int r, const Tag &x) {
tree[v].apply(x, l, r);
lazy[v].apply(x);
}
void push_down(int v, int l, int r) {
int m = (l + r) >> 1;
apply(2 * v, l, m, lazy[v]);
apply(2 * v + 1, m + 1, r, lazy[v]);
lazy[v] = Tag();
}
void upd(int v, int l, int r, int ql, int qr, const Tag &x) {
if (qr < l || ql > r) { return; }
if (ql <= l && r <= qr) {
apply(v, l, r, x);
} else {
push_down(v, l, r);
int m = (l + r) >> 1;
upd(2 * v, l, m, ql, qr, x);
upd(2 * v + 1, m + 1, r, ql, qr, x);
tree[v] = tree[2 * v] + tree[2 * v + 1];
}
}
Info qry(int v, int l, int r, int ql, int qr) {
if (qr < l || ql > r) { return Info(); }
if (l >= ql && r <= qr) { return tree[v]; }
push_down(v, l, r);
int m = (l + r) >> 1;
return qry(2 * v, l, m, ql, qr) + qry(2 * v + 1, m + 1, r, ql, qr);
}
public:
LazySegtree(const vector<Info> &a) : n(a.size()) {
tree.assign(4 << __lg(n), Info());
lazy.assign(4 << __lg(n), Tag());
build(1, 0, n - 1, a);
}
/** updates [ql, qr] with the arbitrary update chosen */
void upd(int ql, int qr, const Tag &x) {
upd(1, 0, n - 1, ql, qr, x);
}
/** @return result of range query on [ql, qr] */
Info qry(int ql, int qr) {
return qry(1, 0, n - 1, ql, qr);
}
};
struct Tag {
int st = -1;
/** applies another lazy update to this update */
void apply(const Tag &t) {
if (t.st != -1) {
st = t.st;
}
}
};
struct Info {
int mx = -1;
int applied = -1;
/** applies an update to this tree node */
void apply(const Tag &t, int l, int r) {
if (t.st != -1) {
applied = mx + t.st;
}
}
};
/** @return result of joining nodes a and b together */
Info operator+(const Info &a, const Info &b) {
return {max(a.mx, b.mx), max(a.applied, b.applied)};
}
int main() {
int n, q;
cin >> n >> q;
vector<int> w(n);
for (int &i : w) { cin >> i; }
vector<Info> arr(n);
for (int i = 0; i < n; i++) {
arr[i] = {w[i], 0};
}
LazySegtree<Info, Tag> st(arr);
vector<vector<array<int, 3>>> tests(n);
for (int i = 0; i < q; i++) {
int l, r, k;
cin >> l >> r >> k;
--l, --r;
tests[l].push_back({r, k, i});
}
vector<bool> res(q);
stack<int> stk;
for (int i = n - 1; i >= 0; i--) {
while (sz(stk) && w[stk.top()] < w[i]) {
stk.pop();
}
int b = stk.empty() ? n : stk.top();
stk.push(i);
st.upd(i + 1, b - 1, {w[i]});
for (auto [r, k, idx] : tests[i]) {
res[idx] = st.qry(i, r).applied <= k;
}
}
for (int i = 0; i < q; i++) {
cout << res[i] << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
604 KB |
Output is correct |
12 |
Correct |
5 ms |
756 KB |
Output is correct |
13 |
Correct |
5 ms |
860 KB |
Output is correct |
14 |
Correct |
7 ms |
860 KB |
Output is correct |
15 |
Correct |
7 ms |
1040 KB |
Output is correct |
16 |
Correct |
6 ms |
856 KB |
Output is correct |
17 |
Correct |
4 ms |
860 KB |
Output is correct |
18 |
Correct |
4 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1870 ms |
115928 KB |
Output is correct |
2 |
Correct |
1756 ms |
116980 KB |
Output is correct |
3 |
Correct |
1871 ms |
116388 KB |
Output is correct |
4 |
Correct |
1805 ms |
116840 KB |
Output is correct |
5 |
Correct |
1735 ms |
120944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
118 ms |
11296 KB |
Output is correct |
2 |
Correct |
113 ms |
11164 KB |
Output is correct |
3 |
Correct |
123 ms |
11604 KB |
Output is correct |
4 |
Correct |
121 ms |
11604 KB |
Output is correct |
5 |
Correct |
133 ms |
11856 KB |
Output is correct |
6 |
Correct |
107 ms |
10984 KB |
Output is correct |
7 |
Correct |
105 ms |
10832 KB |
Output is correct |
8 |
Correct |
105 ms |
10892 KB |
Output is correct |
9 |
Correct |
55 ms |
3340 KB |
Output is correct |
10 |
Correct |
95 ms |
10896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
604 KB |
Output is correct |
12 |
Correct |
5 ms |
756 KB |
Output is correct |
13 |
Correct |
5 ms |
860 KB |
Output is correct |
14 |
Correct |
7 ms |
860 KB |
Output is correct |
15 |
Correct |
7 ms |
1040 KB |
Output is correct |
16 |
Correct |
6 ms |
856 KB |
Output is correct |
17 |
Correct |
4 ms |
860 KB |
Output is correct |
18 |
Correct |
4 ms |
1116 KB |
Output is correct |
19 |
Correct |
343 ms |
24916 KB |
Output is correct |
20 |
Correct |
320 ms |
24912 KB |
Output is correct |
21 |
Correct |
278 ms |
24484 KB |
Output is correct |
22 |
Correct |
286 ms |
24488 KB |
Output is correct |
23 |
Correct |
275 ms |
24388 KB |
Output is correct |
24 |
Correct |
261 ms |
24996 KB |
Output is correct |
25 |
Correct |
257 ms |
24936 KB |
Output is correct |
26 |
Correct |
290 ms |
25424 KB |
Output is correct |
27 |
Correct |
273 ms |
25428 KB |
Output is correct |
28 |
Correct |
283 ms |
25272 KB |
Output is correct |
29 |
Correct |
314 ms |
25940 KB |
Output is correct |
30 |
Correct |
326 ms |
25940 KB |
Output is correct |
31 |
Correct |
320 ms |
25908 KB |
Output is correct |
32 |
Correct |
312 ms |
25684 KB |
Output is correct |
33 |
Correct |
325 ms |
25892 KB |
Output is correct |
34 |
Correct |
249 ms |
24144 KB |
Output is correct |
35 |
Correct |
250 ms |
24148 KB |
Output is correct |
36 |
Correct |
262 ms |
23860 KB |
Output is correct |
37 |
Correct |
265 ms |
23892 KB |
Output is correct |
38 |
Correct |
276 ms |
24144 KB |
Output is correct |
39 |
Correct |
254 ms |
23308 KB |
Output is correct |
40 |
Correct |
205 ms |
19672 KB |
Output is correct |
41 |
Correct |
277 ms |
22816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
4 ms |
604 KB |
Output is correct |
12 |
Correct |
5 ms |
756 KB |
Output is correct |
13 |
Correct |
5 ms |
860 KB |
Output is correct |
14 |
Correct |
7 ms |
860 KB |
Output is correct |
15 |
Correct |
7 ms |
1040 KB |
Output is correct |
16 |
Correct |
6 ms |
856 KB |
Output is correct |
17 |
Correct |
4 ms |
860 KB |
Output is correct |
18 |
Correct |
4 ms |
1116 KB |
Output is correct |
19 |
Correct |
1870 ms |
115928 KB |
Output is correct |
20 |
Correct |
1756 ms |
116980 KB |
Output is correct |
21 |
Correct |
1871 ms |
116388 KB |
Output is correct |
22 |
Correct |
1805 ms |
116840 KB |
Output is correct |
23 |
Correct |
1735 ms |
120944 KB |
Output is correct |
24 |
Correct |
118 ms |
11296 KB |
Output is correct |
25 |
Correct |
113 ms |
11164 KB |
Output is correct |
26 |
Correct |
123 ms |
11604 KB |
Output is correct |
27 |
Correct |
121 ms |
11604 KB |
Output is correct |
28 |
Correct |
133 ms |
11856 KB |
Output is correct |
29 |
Correct |
107 ms |
10984 KB |
Output is correct |
30 |
Correct |
105 ms |
10832 KB |
Output is correct |
31 |
Correct |
105 ms |
10892 KB |
Output is correct |
32 |
Correct |
55 ms |
3340 KB |
Output is correct |
33 |
Correct |
95 ms |
10896 KB |
Output is correct |
34 |
Correct |
343 ms |
24916 KB |
Output is correct |
35 |
Correct |
320 ms |
24912 KB |
Output is correct |
36 |
Correct |
278 ms |
24484 KB |
Output is correct |
37 |
Correct |
286 ms |
24488 KB |
Output is correct |
38 |
Correct |
275 ms |
24388 KB |
Output is correct |
39 |
Correct |
261 ms |
24996 KB |
Output is correct |
40 |
Correct |
257 ms |
24936 KB |
Output is correct |
41 |
Correct |
290 ms |
25424 KB |
Output is correct |
42 |
Correct |
273 ms |
25428 KB |
Output is correct |
43 |
Correct |
283 ms |
25272 KB |
Output is correct |
44 |
Correct |
314 ms |
25940 KB |
Output is correct |
45 |
Correct |
326 ms |
25940 KB |
Output is correct |
46 |
Correct |
320 ms |
25908 KB |
Output is correct |
47 |
Correct |
312 ms |
25684 KB |
Output is correct |
48 |
Correct |
325 ms |
25892 KB |
Output is correct |
49 |
Correct |
249 ms |
24144 KB |
Output is correct |
50 |
Correct |
250 ms |
24148 KB |
Output is correct |
51 |
Correct |
262 ms |
23860 KB |
Output is correct |
52 |
Correct |
265 ms |
23892 KB |
Output is correct |
53 |
Correct |
276 ms |
24144 KB |
Output is correct |
54 |
Correct |
254 ms |
23308 KB |
Output is correct |
55 |
Correct |
205 ms |
19672 KB |
Output is correct |
56 |
Correct |
277 ms |
22816 KB |
Output is correct |
57 |
Correct |
1778 ms |
118368 KB |
Output is correct |
58 |
Correct |
1779 ms |
118332 KB |
Output is correct |
59 |
Correct |
1697 ms |
116104 KB |
Output is correct |
60 |
Correct |
1623 ms |
116316 KB |
Output is correct |
61 |
Correct |
1658 ms |
116192 KB |
Output is correct |
62 |
Correct |
1794 ms |
116100 KB |
Output is correct |
63 |
Correct |
1348 ms |
113800 KB |
Output is correct |
64 |
Correct |
1341 ms |
113764 KB |
Output is correct |
65 |
Correct |
1665 ms |
120196 KB |
Output is correct |
66 |
Correct |
1713 ms |
120228 KB |
Output is correct |
67 |
Correct |
1698 ms |
120400 KB |
Output is correct |
68 |
Correct |
1692 ms |
122448 KB |
Output is correct |
69 |
Correct |
1705 ms |
122404 KB |
Output is correct |
70 |
Correct |
1757 ms |
121948 KB |
Output is correct |
71 |
Correct |
1729 ms |
122020 KB |
Output is correct |
72 |
Correct |
1789 ms |
121912 KB |
Output is correct |
73 |
Correct |
1275 ms |
110060 KB |
Output is correct |
74 |
Correct |
1325 ms |
110944 KB |
Output is correct |
75 |
Correct |
1366 ms |
110160 KB |
Output is correct |
76 |
Correct |
1355 ms |
110012 KB |
Output is correct |
77 |
Correct |
1289 ms |
110164 KB |
Output is correct |
78 |
Correct |
1415 ms |
110848 KB |
Output is correct |
79 |
Correct |
1106 ms |
86192 KB |
Output is correct |
80 |
Correct |
1340 ms |
107252 KB |
Output is correct |