#include <bits/stdc++.h>
using namespace std;
// [3, 5, 1, 8, 4]
// [3, 5, 1, 8, 4]
// each number + the largest one to the left of it, only if it strictly larger
// e.g. 4 + 8 yes
// 8 + 5 no
// 1 + 5 yes
// 5 + 3 yes
// when adding a new entry, update values [i+1, next highest-1]
// but first, undo all changes from indices in the range [i+1, next highest-1]
// [3, 5, 1, 8, 4]
// [0, 0, 0, 0, 8]
// [0, 0, 5, 5, 8]
// [0, 3, 5, 5, 8]
// then do range maximum on that
struct Tree {
vector<int> tag, info;
Tree(int size) {
tag.resize(size*4);
info.resize(size*4);
}
void pushdown(int x) {
info[x] += tag[x];
if (x*2 < tag.size()) tag[x*2] += tag[x];
if (x*2+1 < tag.size()) tag[x*2+1] += tag[x];
tag[x] = 0;
}
void update(int l, int r, int x, int xl, int xr, int val) {
if (l > r) return;
if (l == xl && r == xr) {
tag[x] = val;
} else {
int xm = (xl + xr) / 2;
update(l, min(r, xm), x*2, xl, xm, val);
update(max(l, xm+1), r, x*2+1, xm+1, xr, val);
pushdown(x); pushdown(x*2); pushdown(x*2+1);
info[x] = max(info[x*2], info[x*2+1]);
}
}
int query(int l, int r, int x, int xl, int xr) {
if (l > r) return 0;
pushdown(x);
if (l == xl && r == xr) {
return info[x];
} else {
int xm = (xl + xr) / 2;
int left = query(l, min(r, xm), x*2, xl, xm);
int right = query(max(l, xm+1), r, x*2+1, xm+1, xr);
return max(left, right);
}
}
};
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, M;
cin >> N >> M;
vector<int> w(N);
for (int i=0; i<N; i++) cin >> w[i];
vector<int> next(N, N);
stack<int> S;
for (int i=0; i<N; i++) {
while (S.size() && w[S.top()] <= w[i]) {
next[S.top()] = i;
S.pop();
}
S.push(i);
}
vector<array<int,4>> queries(M);
for (int i=0; i<M; i++) {
int l, r, k;
cin >> l >> r >> k;
queries[i] = {{l-1, r-1, k, i}};
}
sort(queries.rbegin(), queries.rend());
Tree tree(N);
vector<int> ans(M);
int line = N;
set<int> changes;
set<int> todo;
for (int i=0; i<N; i++) todo.insert(i);
for (int i=0; i<M; i++) {
int l = queries[i][0], r = queries[i][1];
int k = queries[i][2], q = queries[i][3];
while (line > l) {
line--;
if (line+1 < next[line]) {
while (changes.size() && *changes.begin() < next[line]) {
int idx = *changes.begin();
tree.update(idx+1, next[idx]-1, 1, 0, N-1, -w[idx]);
changes.erase(idx);
}
vector<int> to_add;
for (auto it = todo.lower_bound(line+1); it != todo.end(); ++it) {
int idx = *it;
if (idx < next[line]) {
to_add.push_back(idx);
} else {
break;
}
}
for (int idx : to_add) {
tree.update(idx, idx, 1, 0, N-1, w[idx]);
todo.erase(idx);
}
tree.update(line+1, next[line]-1, 1, 0, N-1, w[line]);
changes.insert(line);
}
}
// for (int j=0; j<N; j++) cout << tree.query(j, j, 1, 0, N-1) << " "; cout << "\n";
int cost = tree.query(l, r, 1, 0, N-1);
if (cost <= k) ans[q] = 1;
}
for (int x : ans) cout << x << "\n";
return 0;
}
/*
9 9
1 2 3 2 3 4 3 4 5
9 9 0
8 8 0
7 7 0
6 6 0
5 5 0
4 4 0
3 3 0
2 2 0
1 1 0
*/
Compilation message
sortbooks.cpp: In member function 'void Tree::pushdown(int)':
sortbooks.cpp:28:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | if (x*2 < tag.size()) tag[x*2] += tag[x];
| ~~~~^~~~~~~~~~~~
sortbooks.cpp:29:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | if (x*2+1 < tag.size()) tag[x*2+1] += tag[x];
| ~~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
3 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 |
500 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
3 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 |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
5 ms |
860 KB |
Output is correct |
13 |
Correct |
6 ms |
860 KB |
Output is correct |
14 |
Correct |
7 ms |
1112 KB |
Output is correct |
15 |
Correct |
7 ms |
1116 KB |
Output is correct |
16 |
Correct |
5 ms |
860 KB |
Output is correct |
17 |
Correct |
4 ms |
860 KB |
Output is correct |
18 |
Correct |
5 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1991 ms |
107920 KB |
Output is correct |
2 |
Correct |
2031 ms |
107976 KB |
Output is correct |
3 |
Correct |
1980 ms |
107932 KB |
Output is correct |
4 |
Correct |
1962 ms |
108304 KB |
Output is correct |
5 |
Correct |
1168 ms |
107920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
161 ms |
11020 KB |
Output is correct |
2 |
Correct |
169 ms |
12968 KB |
Output is correct |
3 |
Correct |
85 ms |
13020 KB |
Output is correct |
4 |
Correct |
87 ms |
13140 KB |
Output is correct |
5 |
Correct |
97 ms |
13012 KB |
Output is correct |
6 |
Correct |
86 ms |
13144 KB |
Output is correct |
7 |
Correct |
86 ms |
12920 KB |
Output is correct |
8 |
Correct |
101 ms |
12888 KB |
Output is correct |
9 |
Correct |
39 ms |
3864 KB |
Output is correct |
10 |
Correct |
93 ms |
12940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
3 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 |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
5 ms |
860 KB |
Output is correct |
13 |
Correct |
6 ms |
860 KB |
Output is correct |
14 |
Correct |
7 ms |
1112 KB |
Output is correct |
15 |
Correct |
7 ms |
1116 KB |
Output is correct |
16 |
Correct |
5 ms |
860 KB |
Output is correct |
17 |
Correct |
4 ms |
860 KB |
Output is correct |
18 |
Correct |
5 ms |
860 KB |
Output is correct |
19 |
Correct |
355 ms |
28524 KB |
Output is correct |
20 |
Correct |
352 ms |
28496 KB |
Output is correct |
21 |
Correct |
346 ms |
28292 KB |
Output is correct |
22 |
Correct |
339 ms |
28300 KB |
Output is correct |
23 |
Correct |
338 ms |
28244 KB |
Output is correct |
24 |
Correct |
185 ms |
28312 KB |
Output is correct |
25 |
Correct |
188 ms |
28188 KB |
Output is correct |
26 |
Correct |
213 ms |
28312 KB |
Output is correct |
27 |
Correct |
188 ms |
28436 KB |
Output is correct |
28 |
Correct |
201 ms |
28396 KB |
Output is correct |
29 |
Correct |
195 ms |
28496 KB |
Output is correct |
30 |
Correct |
195 ms |
28432 KB |
Output is correct |
31 |
Correct |
193 ms |
28496 KB |
Output is correct |
32 |
Correct |
194 ms |
28500 KB |
Output is correct |
33 |
Correct |
202 ms |
28496 KB |
Output is correct |
34 |
Correct |
188 ms |
27984 KB |
Output is correct |
35 |
Correct |
186 ms |
27984 KB |
Output is correct |
36 |
Correct |
192 ms |
27900 KB |
Output is correct |
37 |
Correct |
186 ms |
27848 KB |
Output is correct |
38 |
Correct |
186 ms |
28056 KB |
Output is correct |
39 |
Correct |
227 ms |
26960 KB |
Output is correct |
40 |
Correct |
144 ms |
20432 KB |
Output is correct |
41 |
Correct |
210 ms |
26668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
500 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
3 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 |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
5 ms |
860 KB |
Output is correct |
13 |
Correct |
6 ms |
860 KB |
Output is correct |
14 |
Correct |
7 ms |
1112 KB |
Output is correct |
15 |
Correct |
7 ms |
1116 KB |
Output is correct |
16 |
Correct |
5 ms |
860 KB |
Output is correct |
17 |
Correct |
4 ms |
860 KB |
Output is correct |
18 |
Correct |
5 ms |
860 KB |
Output is correct |
19 |
Correct |
1991 ms |
107920 KB |
Output is correct |
20 |
Correct |
2031 ms |
107976 KB |
Output is correct |
21 |
Correct |
1980 ms |
107932 KB |
Output is correct |
22 |
Correct |
1962 ms |
108304 KB |
Output is correct |
23 |
Correct |
1168 ms |
107920 KB |
Output is correct |
24 |
Correct |
161 ms |
11020 KB |
Output is correct |
25 |
Correct |
169 ms |
12968 KB |
Output is correct |
26 |
Correct |
85 ms |
13020 KB |
Output is correct |
27 |
Correct |
87 ms |
13140 KB |
Output is correct |
28 |
Correct |
97 ms |
13012 KB |
Output is correct |
29 |
Correct |
86 ms |
13144 KB |
Output is correct |
30 |
Correct |
86 ms |
12920 KB |
Output is correct |
31 |
Correct |
101 ms |
12888 KB |
Output is correct |
32 |
Correct |
39 ms |
3864 KB |
Output is correct |
33 |
Correct |
93 ms |
12940 KB |
Output is correct |
34 |
Correct |
355 ms |
28524 KB |
Output is correct |
35 |
Correct |
352 ms |
28496 KB |
Output is correct |
36 |
Correct |
346 ms |
28292 KB |
Output is correct |
37 |
Correct |
339 ms |
28300 KB |
Output is correct |
38 |
Correct |
338 ms |
28244 KB |
Output is correct |
39 |
Correct |
185 ms |
28312 KB |
Output is correct |
40 |
Correct |
188 ms |
28188 KB |
Output is correct |
41 |
Correct |
213 ms |
28312 KB |
Output is correct |
42 |
Correct |
188 ms |
28436 KB |
Output is correct |
43 |
Correct |
201 ms |
28396 KB |
Output is correct |
44 |
Correct |
195 ms |
28496 KB |
Output is correct |
45 |
Correct |
195 ms |
28432 KB |
Output is correct |
46 |
Correct |
193 ms |
28496 KB |
Output is correct |
47 |
Correct |
194 ms |
28500 KB |
Output is correct |
48 |
Correct |
202 ms |
28496 KB |
Output is correct |
49 |
Correct |
188 ms |
27984 KB |
Output is correct |
50 |
Correct |
186 ms |
27984 KB |
Output is correct |
51 |
Correct |
192 ms |
27900 KB |
Output is correct |
52 |
Correct |
186 ms |
27848 KB |
Output is correct |
53 |
Correct |
186 ms |
28056 KB |
Output is correct |
54 |
Correct |
227 ms |
26960 KB |
Output is correct |
55 |
Correct |
144 ms |
20432 KB |
Output is correct |
56 |
Correct |
210 ms |
26668 KB |
Output is correct |
57 |
Correct |
1978 ms |
141572 KB |
Output is correct |
58 |
Correct |
1977 ms |
141532 KB |
Output is correct |
59 |
Correct |
1940 ms |
141704 KB |
Output is correct |
60 |
Correct |
2008 ms |
141444 KB |
Output is correct |
61 |
Correct |
1947 ms |
141700 KB |
Output is correct |
62 |
Correct |
1979 ms |
141680 KB |
Output is correct |
63 |
Correct |
1014 ms |
139860 KB |
Output is correct |
64 |
Correct |
1057 ms |
139840 KB |
Output is correct |
65 |
Correct |
1205 ms |
141652 KB |
Output is correct |
66 |
Correct |
1152 ms |
141608 KB |
Output is correct |
67 |
Correct |
1174 ms |
141540 KB |
Output is correct |
68 |
Correct |
1194 ms |
141664 KB |
Output is correct |
69 |
Correct |
1222 ms |
141648 KB |
Output is correct |
70 |
Correct |
1184 ms |
141624 KB |
Output is correct |
71 |
Correct |
1282 ms |
141656 KB |
Output is correct |
72 |
Correct |
1218 ms |
141748 KB |
Output is correct |
73 |
Correct |
1101 ms |
138200 KB |
Output is correct |
74 |
Correct |
1092 ms |
139024 KB |
Output is correct |
75 |
Correct |
1098 ms |
138108 KB |
Output is correct |
76 |
Correct |
1081 ms |
138036 KB |
Output is correct |
77 |
Correct |
1078 ms |
138420 KB |
Output is correct |
78 |
Correct |
1378 ms |
135456 KB |
Output is correct |
79 |
Correct |
782 ms |
89768 KB |
Output is correct |
80 |
Correct |
1368 ms |
132712 KB |
Output is correct |