#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> t;
vector<Tag> lz;
void build(int v, int l, int r, const vector<Info> &a) {
if (l == r) {
t[v] = a[l];
} else {
int m = (l + r) >> 1;
build(2 * v, l, m, a);
build(2 * v + 1, m + 1, r, a);
t[v] = t[2 * v] + t[2 * v + 1];
}
}
void apply(int v, int l, int r, const Tag &x) {
t[v].apply(x, l, r);
lz[v].apply(x);
}
void pushdown(int v, int l, int r) {
int m = (l + r) >> 1;
apply(2 * v, l, m, lz[v]);
apply(2 * v + 1, m + 1, r, lz[v]);
lz[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 {
pushdown(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);
t[v] = t[2 * v] + t[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 t[v];
pushdown(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() {}
LazySegtree(int n) : n(n) {
t.assign(4 << __lg(n), Info());
lz.assign(4 << __lg(n), Tag());
}
LazySegtree(const vector<Info> &a) : n(a.size()) {
t.assign(4 << __lg(n), Info());
lz.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;
void apply(const Tag &t) {
if (t.st != -1) {
st = t.st;
}
}
};
struct Info {
int mx = -1, applied = -1;
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() {
cin.tie(0) -> sync_with_stdio(0);
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 |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
440 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 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 |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
440 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
860 KB |
Output is correct |
13 |
Correct |
5 ms |
860 KB |
Output is correct |
14 |
Correct |
6 ms |
856 KB |
Output is correct |
15 |
Correct |
4 ms |
856 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
3 ms |
860 KB |
Output is correct |
18 |
Correct |
3 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1267 ms |
115832 KB |
Output is correct |
2 |
Correct |
1160 ms |
116820 KB |
Output is correct |
3 |
Correct |
1105 ms |
116856 KB |
Output is correct |
4 |
Correct |
1223 ms |
116796 KB |
Output is correct |
5 |
Correct |
1179 ms |
120912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
109 ms |
11352 KB |
Output is correct |
2 |
Correct |
72 ms |
11132 KB |
Output is correct |
3 |
Correct |
73 ms |
11656 KB |
Output is correct |
4 |
Correct |
90 ms |
11860 KB |
Output is correct |
5 |
Correct |
79 ms |
11864 KB |
Output is correct |
6 |
Correct |
65 ms |
11052 KB |
Output is correct |
7 |
Correct |
71 ms |
10832 KB |
Output is correct |
8 |
Correct |
77 ms |
10896 KB |
Output is correct |
9 |
Correct |
25 ms |
3600 KB |
Output is correct |
10 |
Correct |
87 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
440 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
860 KB |
Output is correct |
13 |
Correct |
5 ms |
860 KB |
Output is correct |
14 |
Correct |
6 ms |
856 KB |
Output is correct |
15 |
Correct |
4 ms |
856 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
3 ms |
860 KB |
Output is correct |
18 |
Correct |
3 ms |
860 KB |
Output is correct |
19 |
Correct |
186 ms |
25168 KB |
Output is correct |
20 |
Correct |
193 ms |
25108 KB |
Output is correct |
21 |
Correct |
178 ms |
24472 KB |
Output is correct |
22 |
Correct |
172 ms |
24400 KB |
Output is correct |
23 |
Correct |
168 ms |
24340 KB |
Output is correct |
24 |
Correct |
177 ms |
25028 KB |
Output is correct |
25 |
Correct |
160 ms |
25080 KB |
Output is correct |
26 |
Correct |
177 ms |
25292 KB |
Output is correct |
27 |
Correct |
179 ms |
25424 KB |
Output is correct |
28 |
Correct |
170 ms |
25536 KB |
Output is correct |
29 |
Correct |
174 ms |
25936 KB |
Output is correct |
30 |
Correct |
180 ms |
25940 KB |
Output is correct |
31 |
Correct |
179 ms |
25948 KB |
Output is correct |
32 |
Correct |
174 ms |
25940 KB |
Output is correct |
33 |
Correct |
246 ms |
25936 KB |
Output is correct |
34 |
Correct |
146 ms |
24032 KB |
Output is correct |
35 |
Correct |
171 ms |
24124 KB |
Output is correct |
36 |
Correct |
143 ms |
23888 KB |
Output is correct |
37 |
Correct |
143 ms |
23892 KB |
Output is correct |
38 |
Correct |
158 ms |
24112 KB |
Output is correct |
39 |
Correct |
160 ms |
23396 KB |
Output is correct |
40 |
Correct |
116 ms |
19720 KB |
Output is correct |
41 |
Correct |
146 ms |
22788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
440 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
860 KB |
Output is correct |
13 |
Correct |
5 ms |
860 KB |
Output is correct |
14 |
Correct |
6 ms |
856 KB |
Output is correct |
15 |
Correct |
4 ms |
856 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
3 ms |
860 KB |
Output is correct |
18 |
Correct |
3 ms |
860 KB |
Output is correct |
19 |
Correct |
1267 ms |
115832 KB |
Output is correct |
20 |
Correct |
1160 ms |
116820 KB |
Output is correct |
21 |
Correct |
1105 ms |
116856 KB |
Output is correct |
22 |
Correct |
1223 ms |
116796 KB |
Output is correct |
23 |
Correct |
1179 ms |
120912 KB |
Output is correct |
24 |
Correct |
109 ms |
11352 KB |
Output is correct |
25 |
Correct |
72 ms |
11132 KB |
Output is correct |
26 |
Correct |
73 ms |
11656 KB |
Output is correct |
27 |
Correct |
90 ms |
11860 KB |
Output is correct |
28 |
Correct |
79 ms |
11864 KB |
Output is correct |
29 |
Correct |
65 ms |
11052 KB |
Output is correct |
30 |
Correct |
71 ms |
10832 KB |
Output is correct |
31 |
Correct |
77 ms |
10896 KB |
Output is correct |
32 |
Correct |
25 ms |
3600 KB |
Output is correct |
33 |
Correct |
87 ms |
10844 KB |
Output is correct |
34 |
Correct |
186 ms |
25168 KB |
Output is correct |
35 |
Correct |
193 ms |
25108 KB |
Output is correct |
36 |
Correct |
178 ms |
24472 KB |
Output is correct |
37 |
Correct |
172 ms |
24400 KB |
Output is correct |
38 |
Correct |
168 ms |
24340 KB |
Output is correct |
39 |
Correct |
177 ms |
25028 KB |
Output is correct |
40 |
Correct |
160 ms |
25080 KB |
Output is correct |
41 |
Correct |
177 ms |
25292 KB |
Output is correct |
42 |
Correct |
179 ms |
25424 KB |
Output is correct |
43 |
Correct |
170 ms |
25536 KB |
Output is correct |
44 |
Correct |
174 ms |
25936 KB |
Output is correct |
45 |
Correct |
180 ms |
25940 KB |
Output is correct |
46 |
Correct |
179 ms |
25948 KB |
Output is correct |
47 |
Correct |
174 ms |
25940 KB |
Output is correct |
48 |
Correct |
246 ms |
25936 KB |
Output is correct |
49 |
Correct |
146 ms |
24032 KB |
Output is correct |
50 |
Correct |
171 ms |
24124 KB |
Output is correct |
51 |
Correct |
143 ms |
23888 KB |
Output is correct |
52 |
Correct |
143 ms |
23892 KB |
Output is correct |
53 |
Correct |
158 ms |
24112 KB |
Output is correct |
54 |
Correct |
160 ms |
23396 KB |
Output is correct |
55 |
Correct |
116 ms |
19720 KB |
Output is correct |
56 |
Correct |
146 ms |
22788 KB |
Output is correct |
57 |
Correct |
1220 ms |
118320 KB |
Output is correct |
58 |
Correct |
1219 ms |
118368 KB |
Output is correct |
59 |
Correct |
1136 ms |
116308 KB |
Output is correct |
60 |
Correct |
1139 ms |
116308 KB |
Output is correct |
61 |
Correct |
1133 ms |
116268 KB |
Output is correct |
62 |
Correct |
992 ms |
116564 KB |
Output is correct |
63 |
Correct |
788 ms |
113944 KB |
Output is correct |
64 |
Correct |
781 ms |
113924 KB |
Output is correct |
65 |
Correct |
1035 ms |
120400 KB |
Output is correct |
66 |
Correct |
1046 ms |
120400 KB |
Output is correct |
67 |
Correct |
1064 ms |
120384 KB |
Output is correct |
68 |
Correct |
1145 ms |
122376 KB |
Output is correct |
69 |
Correct |
1158 ms |
122580 KB |
Output is correct |
70 |
Correct |
1164 ms |
121936 KB |
Output is correct |
71 |
Correct |
1153 ms |
122112 KB |
Output is correct |
72 |
Correct |
1245 ms |
122016 KB |
Output is correct |
73 |
Correct |
741 ms |
110160 KB |
Output is correct |
74 |
Correct |
767 ms |
110928 KB |
Output is correct |
75 |
Correct |
750 ms |
110160 KB |
Output is correct |
76 |
Correct |
821 ms |
109988 KB |
Output is correct |
77 |
Correct |
830 ms |
110160 KB |
Output is correct |
78 |
Correct |
970 ms |
111160 KB |
Output is correct |
79 |
Correct |
635 ms |
86516 KB |
Output is correct |
80 |
Correct |
914 ms |
107312 KB |
Output is correct |