#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 |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 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 |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 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 |
4 ms |
976 KB |
Output is correct |
13 |
Correct |
5 ms |
976 KB |
Output is correct |
14 |
Correct |
6 ms |
1232 KB |
Output is correct |
15 |
Correct |
6 ms |
1232 KB |
Output is correct |
16 |
Correct |
4 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 |
1584 ms |
168276 KB |
Output is correct |
2 |
Correct |
1496 ms |
168292 KB |
Output is correct |
3 |
Correct |
1508 ms |
168456 KB |
Output is correct |
4 |
Correct |
1547 ms |
168452 KB |
Output is correct |
5 |
Correct |
1512 ms |
168380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
139 ms |
12452 KB |
Output is correct |
2 |
Correct |
133 ms |
12580 KB |
Output is correct |
3 |
Correct |
146 ms |
12376 KB |
Output is correct |
4 |
Correct |
156 ms |
12912 KB |
Output is correct |
5 |
Correct |
144 ms |
12984 KB |
Output is correct |
6 |
Correct |
137 ms |
12936 KB |
Output is correct |
7 |
Correct |
128 ms |
12892 KB |
Output is correct |
8 |
Correct |
110 ms |
12960 KB |
Output is correct |
9 |
Correct |
36 ms |
1012 KB |
Output is correct |
10 |
Correct |
107 ms |
12940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 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 |
4 ms |
976 KB |
Output is correct |
13 |
Correct |
5 ms |
976 KB |
Output is correct |
14 |
Correct |
6 ms |
1232 KB |
Output is correct |
15 |
Correct |
6 ms |
1232 KB |
Output is correct |
16 |
Correct |
4 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 |
396 ms |
25400 KB |
Output is correct |
20 |
Correct |
342 ms |
25232 KB |
Output is correct |
21 |
Correct |
281 ms |
25252 KB |
Output is correct |
22 |
Correct |
288 ms |
25704 KB |
Output is correct |
23 |
Correct |
273 ms |
25732 KB |
Output is correct |
24 |
Correct |
324 ms |
25672 KB |
Output is correct |
25 |
Correct |
313 ms |
25768 KB |
Output is correct |
26 |
Correct |
387 ms |
25748 KB |
Output is correct |
27 |
Correct |
385 ms |
25872 KB |
Output is correct |
28 |
Correct |
421 ms |
25744 KB |
Output is correct |
29 |
Correct |
398 ms |
25732 KB |
Output is correct |
30 |
Correct |
422 ms |
25740 KB |
Output is correct |
31 |
Correct |
393 ms |
25712 KB |
Output is correct |
32 |
Correct |
409 ms |
25744 KB |
Output is correct |
33 |
Correct |
396 ms |
25744 KB |
Output is correct |
34 |
Correct |
322 ms |
25744 KB |
Output is correct |
35 |
Correct |
316 ms |
25740 KB |
Output is correct |
36 |
Correct |
317 ms |
25744 KB |
Output is correct |
37 |
Correct |
315 ms |
25744 KB |
Output is correct |
38 |
Correct |
332 ms |
25740 KB |
Output is correct |
39 |
Correct |
304 ms |
25720 KB |
Output is correct |
40 |
Correct |
219 ms |
22236 KB |
Output is correct |
41 |
Correct |
270 ms |
25748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
0 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 |
4 ms |
976 KB |
Output is correct |
13 |
Correct |
5 ms |
976 KB |
Output is correct |
14 |
Correct |
6 ms |
1232 KB |
Output is correct |
15 |
Correct |
6 ms |
1232 KB |
Output is correct |
16 |
Correct |
4 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 |
1584 ms |
168276 KB |
Output is correct |
20 |
Correct |
1496 ms |
168292 KB |
Output is correct |
21 |
Correct |
1508 ms |
168456 KB |
Output is correct |
22 |
Correct |
1547 ms |
168452 KB |
Output is correct |
23 |
Correct |
1512 ms |
168380 KB |
Output is correct |
24 |
Correct |
139 ms |
12452 KB |
Output is correct |
25 |
Correct |
133 ms |
12580 KB |
Output is correct |
26 |
Correct |
146 ms |
12376 KB |
Output is correct |
27 |
Correct |
156 ms |
12912 KB |
Output is correct |
28 |
Correct |
144 ms |
12984 KB |
Output is correct |
29 |
Correct |
137 ms |
12936 KB |
Output is correct |
30 |
Correct |
128 ms |
12892 KB |
Output is correct |
31 |
Correct |
110 ms |
12960 KB |
Output is correct |
32 |
Correct |
36 ms |
1012 KB |
Output is correct |
33 |
Correct |
107 ms |
12940 KB |
Output is correct |
34 |
Correct |
396 ms |
25400 KB |
Output is correct |
35 |
Correct |
342 ms |
25232 KB |
Output is correct |
36 |
Correct |
281 ms |
25252 KB |
Output is correct |
37 |
Correct |
288 ms |
25704 KB |
Output is correct |
38 |
Correct |
273 ms |
25732 KB |
Output is correct |
39 |
Correct |
324 ms |
25672 KB |
Output is correct |
40 |
Correct |
313 ms |
25768 KB |
Output is correct |
41 |
Correct |
387 ms |
25748 KB |
Output is correct |
42 |
Correct |
385 ms |
25872 KB |
Output is correct |
43 |
Correct |
421 ms |
25744 KB |
Output is correct |
44 |
Correct |
398 ms |
25732 KB |
Output is correct |
45 |
Correct |
422 ms |
25740 KB |
Output is correct |
46 |
Correct |
393 ms |
25712 KB |
Output is correct |
47 |
Correct |
409 ms |
25744 KB |
Output is correct |
48 |
Correct |
396 ms |
25744 KB |
Output is correct |
49 |
Correct |
322 ms |
25744 KB |
Output is correct |
50 |
Correct |
316 ms |
25740 KB |
Output is correct |
51 |
Correct |
317 ms |
25744 KB |
Output is correct |
52 |
Correct |
315 ms |
25744 KB |
Output is correct |
53 |
Correct |
332 ms |
25740 KB |
Output is correct |
54 |
Correct |
304 ms |
25720 KB |
Output is correct |
55 |
Correct |
219 ms |
22236 KB |
Output is correct |
56 |
Correct |
270 ms |
25748 KB |
Output is correct |
57 |
Incorrect |
1562 ms |
178080 KB |
Output isn't correct |
58 |
Halted |
0 ms |
0 KB |
- |