#include <iostream>
#include<vector>
#include<algorithm>
#pragma GCC optimize("O2")
#pragma GCC optimize("Os")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O1")
#pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const int N = 1e6;
int n, m, w[N + 2], cnt, lg2[N + 2];
struct nodA {
int inv, st, dr;
bool sorted;
} aint[4 * N + 2];
vector<int>all;
int ans = 0, mx = 0;
void build (int nod, int st, int dr) {
if (st == dr) {
aint[nod].inv = 0;
aint[nod].st = all.size();
all.push_back(w[st]);
aint[nod].dr = all.size();
aint[nod].sorted = 1;
return;
}
int mid = (st + dr) / 2;
build(2 * nod, st, mid);
build(2 * nod + 1, mid + 1, dr);
aint[nod].inv = max(aint[2 * nod].inv, aint[2 * nod + 1].inv);
auto it = lower_bound(all.begin() + aint[2 * nod + 1].st, all.begin() + aint[2 * nod + 1].dr, all[aint[2 * nod].dr - 1])-all.begin();
if (it != aint[2 * nod + 1].st) {
it--;
aint[nod].inv = max(aint[nod].inv, all[aint[2 * nod].dr - 1] + all[it]);
}
aint[nod].st = all.size();
int i = aint[2* nod].st, j = aint[2 * nod + 1].st;
while (i < aint[2 * nod].dr&& j < aint[2 * nod + 1].dr) {
if (all[i] == all[j]) {
int x = all[i];
all.push_back(all[i]);
all.push_back(all[i]);
i++;
j++;
}
else if (all[i]< all[j]) {
all.push_back(all[i]);
i++;
}
else if (all[i] > all[j]) {
all.push_back(all[j]);
j++;
}
}
while (i < aint[2 * nod].dr)
all.push_back(all[i++]);
while (j < aint[2 * nod + 1].dr)
all.push_back(all[j++]);
aint[nod].dr = all.size();
aint[nod].sorted = (aint[2 * nod].sorted & aint[2 * nod + 1].sorted & w[mid] <= w[mid + 1]);
}
int lastdr;
bool ok;
void query (int nod, int st, int dr, int a, int b) {
if (a <= st && dr <= b) {
if (mx == -1) {
ans = aint[nod].inv;
mx = max(mx, all[aint[nod].dr - 1]);
ok &= aint[nod].sorted;
lastdr = dr;
}
else {
if (n <= 200000) {
ans = max(ans, aint[nod].inv);
int poz = aint[nod].st - 1;
int l = lg2[dr - st + 1];
for (int pas = (1 << (l + 1)); pas; (pas >>= 1))
if (poz + pas < aint[nod].dr && all[poz + pas] < mx)
poz += pas;
if (poz != aint[nod].st - 1) {
ans = max(ans, mx + all[poz]);
}
}
ok &= aint[nod].sorted;
ok &= (w[lastdr] <= w[lastdr + 1]);
mx = max(mx, all[aint[nod].dr - 1]);
lastdr = dr;
}
return;
}
int mid = (st + dr) / 2;
if (a <= mid)
query(2 * nod, st, mid, a, b);
if (b > mid)
query(2 * nod + 1, mid + 1, dr, a, b);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 2; i <= n; i++)
lg2[i] = lg2[i / 2] + 1;
for (int i = 1; i <= n; i++)
cin >> w[i];
build(1, 1, n);
for (int i = 1; i <= m; i++) {
int l, r, k;
cin >> l >> r >> k;
ans = mx = lastdr = -1;
ok = 1;
query(1, 1, n, l, r);
if (n > 200000) {
cout << ok << "\n";
}
else {
cout << (ans <= k) << "\n";
}
}
return 0;
}
Compilation message
sortbooks.cpp: In function 'void build(int, int, int)':
sortbooks.cpp:46:11: warning: unused variable 'x' [-Wunused-variable]
46 | int x = all[i];
| ^
sortbooks.cpp:66:80: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
66 | aint[nod].sorted = (aint[2 * nod].sorted & aint[2 * nod + 1].sorted & w[mid] <= w[mid + 1]);
| ~~~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
588 KB |
Output is correct |
12 |
Correct |
6 ms |
976 KB |
Output is correct |
13 |
Correct |
4 ms |
976 KB |
Output is correct |
14 |
Correct |
6 ms |
1232 KB |
Output is correct |
15 |
Correct |
5 ms |
1232 KB |
Output is correct |
16 |
Correct |
6 ms |
1232 KB |
Output is correct |
17 |
Correct |
3 ms |
848 KB |
Output is correct |
18 |
Correct |
4 ms |
1232 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1578 ms |
168332 KB |
Output is correct |
2 |
Correct |
1638 ms |
168364 KB |
Output is correct |
3 |
Correct |
1579 ms |
168360 KB |
Output is correct |
4 |
Correct |
1595 ms |
168352 KB |
Output is correct |
5 |
Correct |
1460 ms |
168348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
139 ms |
12464 KB |
Output is correct |
2 |
Correct |
119 ms |
12432 KB |
Output is correct |
3 |
Correct |
152 ms |
12456 KB |
Output is correct |
4 |
Correct |
147 ms |
12452 KB |
Output is correct |
5 |
Correct |
134 ms |
12440 KB |
Output is correct |
6 |
Correct |
131 ms |
12584 KB |
Output is correct |
7 |
Correct |
129 ms |
12464 KB |
Output is correct |
8 |
Correct |
103 ms |
12432 KB |
Output is correct |
9 |
Correct |
36 ms |
552 KB |
Output is correct |
10 |
Correct |
105 ms |
12428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
588 KB |
Output is correct |
12 |
Correct |
6 ms |
976 KB |
Output is correct |
13 |
Correct |
4 ms |
976 KB |
Output is correct |
14 |
Correct |
6 ms |
1232 KB |
Output is correct |
15 |
Correct |
5 ms |
1232 KB |
Output is correct |
16 |
Correct |
6 ms |
1232 KB |
Output is correct |
17 |
Correct |
3 ms |
848 KB |
Output is correct |
18 |
Correct |
4 ms |
1232 KB |
Output is correct |
19 |
Correct |
344 ms |
25252 KB |
Output is correct |
20 |
Correct |
353 ms |
25184 KB |
Output is correct |
21 |
Correct |
280 ms |
25336 KB |
Output is correct |
22 |
Correct |
280 ms |
25220 KB |
Output is correct |
23 |
Correct |
270 ms |
25256 KB |
Output is correct |
24 |
Correct |
309 ms |
25192 KB |
Output is correct |
25 |
Correct |
307 ms |
25232 KB |
Output is correct |
26 |
Correct |
375 ms |
25276 KB |
Output is correct |
27 |
Correct |
362 ms |
25196 KB |
Output is correct |
28 |
Correct |
400 ms |
25208 KB |
Output is correct |
29 |
Correct |
393 ms |
25248 KB |
Output is correct |
30 |
Correct |
405 ms |
25224 KB |
Output is correct |
31 |
Correct |
386 ms |
25232 KB |
Output is correct |
32 |
Correct |
418 ms |
25252 KB |
Output is correct |
33 |
Correct |
423 ms |
25144 KB |
Output is correct |
34 |
Correct |
312 ms |
25140 KB |
Output is correct |
35 |
Correct |
320 ms |
25192 KB |
Output is correct |
36 |
Correct |
319 ms |
25228 KB |
Output is correct |
37 |
Correct |
310 ms |
25188 KB |
Output is correct |
38 |
Correct |
323 ms |
25308 KB |
Output is correct |
39 |
Correct |
315 ms |
25196 KB |
Output is correct |
40 |
Correct |
218 ms |
21948 KB |
Output is correct |
41 |
Correct |
267 ms |
25176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
588 KB |
Output is correct |
12 |
Correct |
6 ms |
976 KB |
Output is correct |
13 |
Correct |
4 ms |
976 KB |
Output is correct |
14 |
Correct |
6 ms |
1232 KB |
Output is correct |
15 |
Correct |
5 ms |
1232 KB |
Output is correct |
16 |
Correct |
6 ms |
1232 KB |
Output is correct |
17 |
Correct |
3 ms |
848 KB |
Output is correct |
18 |
Correct |
4 ms |
1232 KB |
Output is correct |
19 |
Correct |
1578 ms |
168332 KB |
Output is correct |
20 |
Correct |
1638 ms |
168364 KB |
Output is correct |
21 |
Correct |
1579 ms |
168360 KB |
Output is correct |
22 |
Correct |
1595 ms |
168352 KB |
Output is correct |
23 |
Correct |
1460 ms |
168348 KB |
Output is correct |
24 |
Correct |
139 ms |
12464 KB |
Output is correct |
25 |
Correct |
119 ms |
12432 KB |
Output is correct |
26 |
Correct |
152 ms |
12456 KB |
Output is correct |
27 |
Correct |
147 ms |
12452 KB |
Output is correct |
28 |
Correct |
134 ms |
12440 KB |
Output is correct |
29 |
Correct |
131 ms |
12584 KB |
Output is correct |
30 |
Correct |
129 ms |
12464 KB |
Output is correct |
31 |
Correct |
103 ms |
12432 KB |
Output is correct |
32 |
Correct |
36 ms |
552 KB |
Output is correct |
33 |
Correct |
105 ms |
12428 KB |
Output is correct |
34 |
Correct |
344 ms |
25252 KB |
Output is correct |
35 |
Correct |
353 ms |
25184 KB |
Output is correct |
36 |
Correct |
280 ms |
25336 KB |
Output is correct |
37 |
Correct |
280 ms |
25220 KB |
Output is correct |
38 |
Correct |
270 ms |
25256 KB |
Output is correct |
39 |
Correct |
309 ms |
25192 KB |
Output is correct |
40 |
Correct |
307 ms |
25232 KB |
Output is correct |
41 |
Correct |
375 ms |
25276 KB |
Output is correct |
42 |
Correct |
362 ms |
25196 KB |
Output is correct |
43 |
Correct |
400 ms |
25208 KB |
Output is correct |
44 |
Correct |
393 ms |
25248 KB |
Output is correct |
45 |
Correct |
405 ms |
25224 KB |
Output is correct |
46 |
Correct |
386 ms |
25232 KB |
Output is correct |
47 |
Correct |
418 ms |
25252 KB |
Output is correct |
48 |
Correct |
423 ms |
25144 KB |
Output is correct |
49 |
Correct |
312 ms |
25140 KB |
Output is correct |
50 |
Correct |
320 ms |
25192 KB |
Output is correct |
51 |
Correct |
319 ms |
25228 KB |
Output is correct |
52 |
Correct |
310 ms |
25188 KB |
Output is correct |
53 |
Correct |
323 ms |
25308 KB |
Output is correct |
54 |
Correct |
315 ms |
25196 KB |
Output is correct |
55 |
Correct |
218 ms |
21948 KB |
Output is correct |
56 |
Correct |
267 ms |
25176 KB |
Output is correct |
57 |
Incorrect |
1513 ms |
168384 KB |
Output isn't correct |
58 |
Halted |
0 ms |
0 KB |
- |