#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), v.end()
int mx;
vector<int> a, st, vis;
vector<vector<int>> v;
void bld(int nd, int l, int r) {
if (l == r) {
vis[nd] = 0; st[nd] = a[l]; v[nd] = {a[l]};
return;
}
int ch = (nd << 1) + 1, md = l + r >> 1;
bld(ch, l, md); bld(ch + 1, md + 1, r);
st[nd] = max(st[ch], st[ch + 1]);
v[nd].assign(sz(v[ch]) + sz(v[ch + 1]), 0);
merge(all(v[ch]), all(v[ch + 1]), v[nd].begin());
int mx = 0;
for (int i = l; i <= r; i++) {
if (mx > a[i]) vis[nd] = max(vis[nd], mx + a[i]);
else mx = a[i];
}
}
bool fnd(int nd, int l, int r, int x, int y, int k) {
if (r < x || y < l) return true;
if (x <= l && r <= y) {
bool tr = (vis[nd] <= k && (mx > v[nd][0] ? mx + *--lower_bound(all(v[nd]), mx) : 0) <= k);
mx = max(mx, st[nd]);
return tr;
}
int ch = (nd << 1) + 1, md = l + r >> 1;
return (!fnd(ch, l, md, x, y, k) || !fnd(ch + 1, md + 1, r, x, y, k) ? false : true);
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n, q;
cin >> n >> q;
a.assign(n, 0);
for (auto &i : a)
cin >> i;
v.assign(n << 2, {});
st = vis = vector<int>(n << 2);
bld(0, 0, n - 1);
while (q--) {
int l, r, k;
cin >> l >> r >> k; l--; r--;
mx = 0;
cout << fnd(0, 0, n - 1, l, r, k) << endl;
}
}
Compilation message
sortbooks.cpp: In function 'void bld(int, int, int)':
sortbooks.cpp:21:33: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
21 | int ch = (nd << 1) + 1, md = l + r >> 1;
| ~~^~~
sortbooks.cpp: In function 'bool fnd(int, int, int, int, int, int)':
sortbooks.cpp:45:33: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | int ch = (nd << 1) + 1, md = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
5 ms |
860 KB |
Output is correct |
12 |
Correct |
6 ms |
1628 KB |
Output is correct |
13 |
Correct |
6 ms |
1600 KB |
Output is correct |
14 |
Correct |
10 ms |
1628 KB |
Output is correct |
15 |
Correct |
9 ms |
1628 KB |
Output is correct |
16 |
Correct |
9 ms |
1628 KB |
Output is correct |
17 |
Correct |
8 ms |
1372 KB |
Output is correct |
18 |
Correct |
11 ms |
1628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2185 ms |
262144 KB |
Output is correct |
2 |
Correct |
2198 ms |
262144 KB |
Output is correct |
3 |
Correct |
2139 ms |
262144 KB |
Output is correct |
4 |
Correct |
2173 ms |
262144 KB |
Output is correct |
5 |
Correct |
2801 ms |
262144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
186 ms |
26960 KB |
Output is correct |
2 |
Correct |
187 ms |
26848 KB |
Output is correct |
3 |
Correct |
198 ms |
26796 KB |
Output is correct |
4 |
Correct |
212 ms |
26964 KB |
Output is correct |
5 |
Correct |
203 ms |
26808 KB |
Output is correct |
6 |
Correct |
153 ms |
26836 KB |
Output is correct |
7 |
Correct |
163 ms |
26816 KB |
Output is correct |
8 |
Correct |
191 ms |
26532 KB |
Output is correct |
9 |
Correct |
122 ms |
1872 KB |
Output is correct |
10 |
Correct |
188 ms |
26632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
5 ms |
860 KB |
Output is correct |
12 |
Correct |
6 ms |
1628 KB |
Output is correct |
13 |
Correct |
6 ms |
1600 KB |
Output is correct |
14 |
Correct |
10 ms |
1628 KB |
Output is correct |
15 |
Correct |
9 ms |
1628 KB |
Output is correct |
16 |
Correct |
9 ms |
1628 KB |
Output is correct |
17 |
Correct |
8 ms |
1372 KB |
Output is correct |
18 |
Correct |
11 ms |
1628 KB |
Output is correct |
19 |
Correct |
436 ms |
56912 KB |
Output is correct |
20 |
Correct |
424 ms |
56912 KB |
Output is correct |
21 |
Correct |
434 ms |
56660 KB |
Output is correct |
22 |
Correct |
398 ms |
57024 KB |
Output is correct |
23 |
Correct |
386 ms |
56656 KB |
Output is correct |
24 |
Correct |
404 ms |
56600 KB |
Output is correct |
25 |
Correct |
378 ms |
56668 KB |
Output is correct |
26 |
Correct |
477 ms |
56936 KB |
Output is correct |
27 |
Correct |
436 ms |
56728 KB |
Output is correct |
28 |
Correct |
456 ms |
56916 KB |
Output is correct |
29 |
Correct |
477 ms |
56888 KB |
Output is correct |
30 |
Correct |
483 ms |
56824 KB |
Output is correct |
31 |
Correct |
481 ms |
56920 KB |
Output is correct |
32 |
Correct |
464 ms |
57012 KB |
Output is correct |
33 |
Correct |
436 ms |
56916 KB |
Output is correct |
34 |
Correct |
328 ms |
56512 KB |
Output is correct |
35 |
Correct |
331 ms |
56440 KB |
Output is correct |
36 |
Correct |
380 ms |
56404 KB |
Output is correct |
37 |
Correct |
329 ms |
56496 KB |
Output is correct |
38 |
Correct |
387 ms |
56644 KB |
Output is correct |
39 |
Correct |
390 ms |
55564 KB |
Output is correct |
40 |
Correct |
361 ms |
37456 KB |
Output is correct |
41 |
Correct |
365 ms |
55024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
360 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
5 ms |
860 KB |
Output is correct |
12 |
Correct |
6 ms |
1628 KB |
Output is correct |
13 |
Correct |
6 ms |
1600 KB |
Output is correct |
14 |
Correct |
10 ms |
1628 KB |
Output is correct |
15 |
Correct |
9 ms |
1628 KB |
Output is correct |
16 |
Correct |
9 ms |
1628 KB |
Output is correct |
17 |
Correct |
8 ms |
1372 KB |
Output is correct |
18 |
Correct |
11 ms |
1628 KB |
Output is correct |
19 |
Correct |
2185 ms |
262144 KB |
Output is correct |
20 |
Correct |
2198 ms |
262144 KB |
Output is correct |
21 |
Correct |
2139 ms |
262144 KB |
Output is correct |
22 |
Correct |
2173 ms |
262144 KB |
Output is correct |
23 |
Correct |
2801 ms |
262144 KB |
Output is correct |
24 |
Correct |
186 ms |
26960 KB |
Output is correct |
25 |
Correct |
187 ms |
26848 KB |
Output is correct |
26 |
Correct |
198 ms |
26796 KB |
Output is correct |
27 |
Correct |
212 ms |
26964 KB |
Output is correct |
28 |
Correct |
203 ms |
26808 KB |
Output is correct |
29 |
Correct |
153 ms |
26836 KB |
Output is correct |
30 |
Correct |
163 ms |
26816 KB |
Output is correct |
31 |
Correct |
191 ms |
26532 KB |
Output is correct |
32 |
Correct |
122 ms |
1872 KB |
Output is correct |
33 |
Correct |
188 ms |
26632 KB |
Output is correct |
34 |
Correct |
436 ms |
56912 KB |
Output is correct |
35 |
Correct |
424 ms |
56912 KB |
Output is correct |
36 |
Correct |
434 ms |
56660 KB |
Output is correct |
37 |
Correct |
398 ms |
57024 KB |
Output is correct |
38 |
Correct |
386 ms |
56656 KB |
Output is correct |
39 |
Correct |
404 ms |
56600 KB |
Output is correct |
40 |
Correct |
378 ms |
56668 KB |
Output is correct |
41 |
Correct |
477 ms |
56936 KB |
Output is correct |
42 |
Correct |
436 ms |
56728 KB |
Output is correct |
43 |
Correct |
456 ms |
56916 KB |
Output is correct |
44 |
Correct |
477 ms |
56888 KB |
Output is correct |
45 |
Correct |
483 ms |
56824 KB |
Output is correct |
46 |
Correct |
481 ms |
56920 KB |
Output is correct |
47 |
Correct |
464 ms |
57012 KB |
Output is correct |
48 |
Correct |
436 ms |
56916 KB |
Output is correct |
49 |
Correct |
328 ms |
56512 KB |
Output is correct |
50 |
Correct |
331 ms |
56440 KB |
Output is correct |
51 |
Correct |
380 ms |
56404 KB |
Output is correct |
52 |
Correct |
329 ms |
56496 KB |
Output is correct |
53 |
Correct |
387 ms |
56644 KB |
Output is correct |
54 |
Correct |
390 ms |
55564 KB |
Output is correct |
55 |
Correct |
361 ms |
37456 KB |
Output is correct |
56 |
Correct |
365 ms |
55024 KB |
Output is correct |
57 |
Correct |
2384 ms |
262144 KB |
Output is correct |
58 |
Correct |
2395 ms |
262144 KB |
Output is correct |
59 |
Correct |
2360 ms |
262144 KB |
Output is correct |
60 |
Correct |
2340 ms |
262144 KB |
Output is correct |
61 |
Correct |
2446 ms |
262144 KB |
Output is correct |
62 |
Correct |
2375 ms |
262144 KB |
Output is correct |
63 |
Correct |
1900 ms |
262144 KB |
Output is correct |
64 |
Correct |
1964 ms |
262144 KB |
Output is correct |
65 |
Correct |
2881 ms |
262144 KB |
Output is correct |
66 |
Correct |
2784 ms |
262144 KB |
Output is correct |
67 |
Correct |
2691 ms |
262144 KB |
Output is correct |
68 |
Correct |
2721 ms |
262144 KB |
Output is correct |
69 |
Correct |
2492 ms |
262144 KB |
Output is correct |
70 |
Correct |
2782 ms |
262144 KB |
Output is correct |
71 |
Correct |
2737 ms |
262144 KB |
Output is correct |
72 |
Correct |
2739 ms |
262144 KB |
Output is correct |
73 |
Correct |
1865 ms |
262144 KB |
Output is correct |
74 |
Correct |
1770 ms |
262144 KB |
Output is correct |
75 |
Correct |
1811 ms |
262144 KB |
Output is correct |
76 |
Correct |
1896 ms |
262144 KB |
Output is correct |
77 |
Correct |
1926 ms |
262144 KB |
Output is correct |
78 |
Correct |
2203 ms |
262144 KB |
Output is correct |
79 |
Correct |
2037 ms |
158292 KB |
Output is correct |
80 |
Correct |
2186 ms |
262144 KB |
Output is correct |