#include <bits/stdc++.h>
using namespace std;
const int MXN = 1e6 + 5;
int n, q;
int a[MXN];
vector<int> st[MXN << 2];
int mx[MXN << 2];
void build(int l, int r, int x)
{
mx[x] = 0;
if (l == r)
{
st[x].push_back(a[l]);
return;
}
int mid = (l + r) >> 1;
build(l, mid, 2*x), build(mid + 1, r, 2*x + 1);
st[x].resize(st[2*x].size() + st[2*x + 1].size());
merge(st[2*x].begin(), st[2*x].end(), st[2*x + 1].begin(), st[2*x + 1].end(), st[x].begin());
if (st[2*x].back() > st[2*x + 1][0]) mx[x] = st[2*x].back() + *--lower_bound(st[2*x + 1].begin(), st[2*x + 1].end(), st[2*x].back());
mx[x] = max({mx[x], mx[2*x], mx[2*x + 1]});
}
vector<int> res;
void get(int l, int r, int x, int lx, int rx)
{
if (l > rx || r < lx) return;
if (l >= lx && r <= rx)
{
res.push_back(x);
return;
}
int mid = (l + r) >> 1;
get(l, mid, 2*x, lx, rx);
get(mid + 1, r, 2*x + 1, lx, rx);
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> q;
for (int i = 1; i <= n; i++) cin >> a[i];
build(1, n, 1);
while (q--)
{
res.clear();
int l, r, x;
cin >> l >> r >> x;
get(1, n, 1, l, r);
int curmx = 0, ans = 0;
for (int &i : res) ans = max(ans, mx[i]);
for (int i = 0; i + 1 < res.size(); i++)
{
int l = res[i], r = res[i + 1];
curmx = max(curmx, st[l].back());
if (curmx > st[r][0]) ans = max(ans, curmx + *--lower_bound(st[r].begin(), st[r].end(), curmx));
}
cout << (ans <= x) << '\n';
}
}
Compilation message
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:57:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | for (int i = 0; i + 1 < res.size(); i++)
| ~~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
95312 KB |
Output is correct |
2 |
Correct |
44 ms |
95436 KB |
Output is correct |
3 |
Correct |
44 ms |
95312 KB |
Output is correct |
4 |
Correct |
44 ms |
95364 KB |
Output is correct |
5 |
Correct |
48 ms |
95568 KB |
Output is correct |
6 |
Correct |
44 ms |
95312 KB |
Output is correct |
7 |
Correct |
46 ms |
95324 KB |
Output is correct |
8 |
Correct |
43 ms |
95316 KB |
Output is correct |
9 |
Correct |
43 ms |
95312 KB |
Output is correct |
10 |
Correct |
52 ms |
95444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
95312 KB |
Output is correct |
2 |
Correct |
44 ms |
95436 KB |
Output is correct |
3 |
Correct |
44 ms |
95312 KB |
Output is correct |
4 |
Correct |
44 ms |
95364 KB |
Output is correct |
5 |
Correct |
48 ms |
95568 KB |
Output is correct |
6 |
Correct |
44 ms |
95312 KB |
Output is correct |
7 |
Correct |
46 ms |
95324 KB |
Output is correct |
8 |
Correct |
43 ms |
95316 KB |
Output is correct |
9 |
Correct |
43 ms |
95312 KB |
Output is correct |
10 |
Correct |
52 ms |
95444 KB |
Output is correct |
11 |
Correct |
45 ms |
95572 KB |
Output is correct |
12 |
Correct |
44 ms |
95824 KB |
Output is correct |
13 |
Correct |
46 ms |
96060 KB |
Output is correct |
14 |
Correct |
49 ms |
96080 KB |
Output is correct |
15 |
Correct |
58 ms |
96080 KB |
Output is correct |
16 |
Correct |
47 ms |
96088 KB |
Output is correct |
17 |
Correct |
47 ms |
95944 KB |
Output is correct |
18 |
Correct |
46 ms |
95824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2267 ms |
262144 KB |
Output is correct |
2 |
Correct |
2221 ms |
262144 KB |
Output is correct |
3 |
Correct |
2288 ms |
262144 KB |
Output is correct |
4 |
Correct |
2312 ms |
262144 KB |
Output is correct |
5 |
Correct |
1493 ms |
262144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
164 ms |
108372 KB |
Output is correct |
2 |
Correct |
159 ms |
108368 KB |
Output is correct |
3 |
Correct |
133 ms |
108368 KB |
Output is correct |
4 |
Correct |
123 ms |
108568 KB |
Output is correct |
5 |
Correct |
131 ms |
108372 KB |
Output is correct |
6 |
Correct |
118 ms |
108372 KB |
Output is correct |
7 |
Correct |
111 ms |
108364 KB |
Output is correct |
8 |
Correct |
124 ms |
108476 KB |
Output is correct |
9 |
Correct |
79 ms |
95572 KB |
Output is correct |
10 |
Correct |
119 ms |
108372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
95312 KB |
Output is correct |
2 |
Correct |
44 ms |
95436 KB |
Output is correct |
3 |
Correct |
44 ms |
95312 KB |
Output is correct |
4 |
Correct |
44 ms |
95364 KB |
Output is correct |
5 |
Correct |
48 ms |
95568 KB |
Output is correct |
6 |
Correct |
44 ms |
95312 KB |
Output is correct |
7 |
Correct |
46 ms |
95324 KB |
Output is correct |
8 |
Correct |
43 ms |
95316 KB |
Output is correct |
9 |
Correct |
43 ms |
95312 KB |
Output is correct |
10 |
Correct |
52 ms |
95444 KB |
Output is correct |
11 |
Correct |
45 ms |
95572 KB |
Output is correct |
12 |
Correct |
44 ms |
95824 KB |
Output is correct |
13 |
Correct |
46 ms |
96060 KB |
Output is correct |
14 |
Correct |
49 ms |
96080 KB |
Output is correct |
15 |
Correct |
58 ms |
96080 KB |
Output is correct |
16 |
Correct |
47 ms |
96088 KB |
Output is correct |
17 |
Correct |
47 ms |
95944 KB |
Output is correct |
18 |
Correct |
46 ms |
95824 KB |
Output is correct |
19 |
Correct |
362 ms |
122524 KB |
Output is correct |
20 |
Correct |
356 ms |
122452 KB |
Output is correct |
21 |
Correct |
314 ms |
122344 KB |
Output is correct |
22 |
Correct |
308 ms |
122452 KB |
Output is correct |
23 |
Correct |
314 ms |
122448 KB |
Output is correct |
24 |
Correct |
210 ms |
122420 KB |
Output is correct |
25 |
Correct |
207 ms |
122448 KB |
Output is correct |
26 |
Correct |
246 ms |
122448 KB |
Output is correct |
27 |
Correct |
312 ms |
122492 KB |
Output is correct |
28 |
Correct |
270 ms |
122492 KB |
Output is correct |
29 |
Correct |
258 ms |
122392 KB |
Output is correct |
30 |
Correct |
253 ms |
122708 KB |
Output is correct |
31 |
Correct |
251 ms |
122448 KB |
Output is correct |
32 |
Correct |
267 ms |
122448 KB |
Output is correct |
33 |
Correct |
259 ms |
122448 KB |
Output is correct |
34 |
Correct |
202 ms |
122448 KB |
Output is correct |
35 |
Correct |
202 ms |
122448 KB |
Output is correct |
36 |
Correct |
197 ms |
122324 KB |
Output is correct |
37 |
Correct |
210 ms |
122448 KB |
Output is correct |
38 |
Correct |
201 ms |
122452 KB |
Output is correct |
39 |
Correct |
234 ms |
122452 KB |
Output is correct |
40 |
Correct |
161 ms |
112648 KB |
Output is correct |
41 |
Correct |
229 ms |
122428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
95312 KB |
Output is correct |
2 |
Correct |
44 ms |
95436 KB |
Output is correct |
3 |
Correct |
44 ms |
95312 KB |
Output is correct |
4 |
Correct |
44 ms |
95364 KB |
Output is correct |
5 |
Correct |
48 ms |
95568 KB |
Output is correct |
6 |
Correct |
44 ms |
95312 KB |
Output is correct |
7 |
Correct |
46 ms |
95324 KB |
Output is correct |
8 |
Correct |
43 ms |
95316 KB |
Output is correct |
9 |
Correct |
43 ms |
95312 KB |
Output is correct |
10 |
Correct |
52 ms |
95444 KB |
Output is correct |
11 |
Correct |
45 ms |
95572 KB |
Output is correct |
12 |
Correct |
44 ms |
95824 KB |
Output is correct |
13 |
Correct |
46 ms |
96060 KB |
Output is correct |
14 |
Correct |
49 ms |
96080 KB |
Output is correct |
15 |
Correct |
58 ms |
96080 KB |
Output is correct |
16 |
Correct |
47 ms |
96088 KB |
Output is correct |
17 |
Correct |
47 ms |
95944 KB |
Output is correct |
18 |
Correct |
46 ms |
95824 KB |
Output is correct |
19 |
Correct |
2267 ms |
262144 KB |
Output is correct |
20 |
Correct |
2221 ms |
262144 KB |
Output is correct |
21 |
Correct |
2288 ms |
262144 KB |
Output is correct |
22 |
Correct |
2312 ms |
262144 KB |
Output is correct |
23 |
Correct |
1493 ms |
262144 KB |
Output is correct |
24 |
Correct |
164 ms |
108372 KB |
Output is correct |
25 |
Correct |
159 ms |
108368 KB |
Output is correct |
26 |
Correct |
133 ms |
108368 KB |
Output is correct |
27 |
Correct |
123 ms |
108568 KB |
Output is correct |
28 |
Correct |
131 ms |
108372 KB |
Output is correct |
29 |
Correct |
118 ms |
108372 KB |
Output is correct |
30 |
Correct |
111 ms |
108364 KB |
Output is correct |
31 |
Correct |
124 ms |
108476 KB |
Output is correct |
32 |
Correct |
79 ms |
95572 KB |
Output is correct |
33 |
Correct |
119 ms |
108372 KB |
Output is correct |
34 |
Correct |
362 ms |
122524 KB |
Output is correct |
35 |
Correct |
356 ms |
122452 KB |
Output is correct |
36 |
Correct |
314 ms |
122344 KB |
Output is correct |
37 |
Correct |
308 ms |
122452 KB |
Output is correct |
38 |
Correct |
314 ms |
122448 KB |
Output is correct |
39 |
Correct |
210 ms |
122420 KB |
Output is correct |
40 |
Correct |
207 ms |
122448 KB |
Output is correct |
41 |
Correct |
246 ms |
122448 KB |
Output is correct |
42 |
Correct |
312 ms |
122492 KB |
Output is correct |
43 |
Correct |
270 ms |
122492 KB |
Output is correct |
44 |
Correct |
258 ms |
122392 KB |
Output is correct |
45 |
Correct |
253 ms |
122708 KB |
Output is correct |
46 |
Correct |
251 ms |
122448 KB |
Output is correct |
47 |
Correct |
267 ms |
122448 KB |
Output is correct |
48 |
Correct |
259 ms |
122448 KB |
Output is correct |
49 |
Correct |
202 ms |
122448 KB |
Output is correct |
50 |
Correct |
202 ms |
122448 KB |
Output is correct |
51 |
Correct |
197 ms |
122324 KB |
Output is correct |
52 |
Correct |
210 ms |
122448 KB |
Output is correct |
53 |
Correct |
201 ms |
122452 KB |
Output is correct |
54 |
Correct |
234 ms |
122452 KB |
Output is correct |
55 |
Correct |
161 ms |
112648 KB |
Output is correct |
56 |
Correct |
229 ms |
122428 KB |
Output is correct |
57 |
Correct |
2114 ms |
262144 KB |
Output is correct |
58 |
Correct |
2128 ms |
262144 KB |
Output is correct |
59 |
Correct |
2154 ms |
262144 KB |
Output is correct |
60 |
Correct |
2124 ms |
262144 KB |
Output is correct |
61 |
Correct |
2118 ms |
262144 KB |
Output is correct |
62 |
Correct |
2146 ms |
262144 KB |
Output is correct |
63 |
Correct |
994 ms |
262144 KB |
Output is correct |
64 |
Correct |
946 ms |
262144 KB |
Output is correct |
65 |
Correct |
1448 ms |
262144 KB |
Output is correct |
66 |
Correct |
1404 ms |
262144 KB |
Output is correct |
67 |
Correct |
1390 ms |
262144 KB |
Output is correct |
68 |
Correct |
1379 ms |
262144 KB |
Output is correct |
69 |
Correct |
1384 ms |
262144 KB |
Output is correct |
70 |
Correct |
1417 ms |
262144 KB |
Output is correct |
71 |
Correct |
1500 ms |
262144 KB |
Output is correct |
72 |
Correct |
1424 ms |
262144 KB |
Output is correct |
73 |
Correct |
933 ms |
262144 KB |
Output is correct |
74 |
Correct |
986 ms |
262144 KB |
Output is correct |
75 |
Correct |
943 ms |
262144 KB |
Output is correct |
76 |
Correct |
926 ms |
262144 KB |
Output is correct |
77 |
Correct |
991 ms |
262144 KB |
Output is correct |
78 |
Correct |
1461 ms |
262144 KB |
Output is correct |
79 |
Correct |
848 ms |
191812 KB |
Output is correct |
80 |
Correct |
1329 ms |
262144 KB |
Output is correct |