#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]);
| ~~~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
3 ms |
620 KB |
Output is correct |
12 |
Correct |
5 ms |
1008 KB |
Output is correct |
13 |
Correct |
6 ms |
1008 KB |
Output is correct |
14 |
Correct |
23 ms |
1264 KB |
Output is correct |
15 |
Correct |
7 ms |
1264 KB |
Output is correct |
16 |
Correct |
6 ms |
1264 KB |
Output is correct |
17 |
Correct |
5 ms |
880 KB |
Output is correct |
18 |
Correct |
5 ms |
1264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2485 ms |
168404 KB |
Output is correct |
2 |
Correct |
2506 ms |
168596 KB |
Output is correct |
3 |
Correct |
2443 ms |
168572 KB |
Output is correct |
4 |
Correct |
2468 ms |
168532 KB |
Output is correct |
5 |
Correct |
2324 ms |
168568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
254 ms |
12520 KB |
Output is correct |
2 |
Correct |
207 ms |
12624 KB |
Output is correct |
3 |
Correct |
286 ms |
12540 KB |
Output is correct |
4 |
Correct |
290 ms |
13008 KB |
Output is correct |
5 |
Correct |
293 ms |
13904 KB |
Output is correct |
6 |
Correct |
234 ms |
13680 KB |
Output is correct |
7 |
Correct |
228 ms |
13168 KB |
Output is correct |
8 |
Correct |
195 ms |
13572 KB |
Output is correct |
9 |
Correct |
60 ms |
1664 KB |
Output is correct |
10 |
Correct |
271 ms |
13132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
3 ms |
620 KB |
Output is correct |
12 |
Correct |
5 ms |
1008 KB |
Output is correct |
13 |
Correct |
6 ms |
1008 KB |
Output is correct |
14 |
Correct |
23 ms |
1264 KB |
Output is correct |
15 |
Correct |
7 ms |
1264 KB |
Output is correct |
16 |
Correct |
6 ms |
1264 KB |
Output is correct |
17 |
Correct |
5 ms |
880 KB |
Output is correct |
18 |
Correct |
5 ms |
1264 KB |
Output is correct |
19 |
Correct |
620 ms |
25276 KB |
Output is correct |
20 |
Correct |
572 ms |
26340 KB |
Output is correct |
21 |
Correct |
470 ms |
25256 KB |
Output is correct |
22 |
Correct |
578 ms |
25736 KB |
Output is correct |
23 |
Correct |
459 ms |
25984 KB |
Output is correct |
24 |
Correct |
518 ms |
25940 KB |
Output is correct |
25 |
Correct |
569 ms |
25400 KB |
Output is correct |
26 |
Correct |
674 ms |
25276 KB |
Output is correct |
27 |
Correct |
694 ms |
25364 KB |
Output is correct |
28 |
Correct |
740 ms |
25372 KB |
Output is correct |
29 |
Correct |
782 ms |
25356 KB |
Output is correct |
30 |
Correct |
788 ms |
25252 KB |
Output is correct |
31 |
Correct |
811 ms |
25592 KB |
Output is correct |
32 |
Correct |
801 ms |
26220 KB |
Output is correct |
33 |
Correct |
730 ms |
25276 KB |
Output is correct |
34 |
Correct |
555 ms |
26328 KB |
Output is correct |
35 |
Correct |
556 ms |
26300 KB |
Output is correct |
36 |
Correct |
577 ms |
25360 KB |
Output is correct |
37 |
Correct |
532 ms |
25276 KB |
Output is correct |
38 |
Correct |
507 ms |
25300 KB |
Output is correct |
39 |
Correct |
575 ms |
25340 KB |
Output is correct |
40 |
Correct |
409 ms |
22140 KB |
Output is correct |
41 |
Correct |
517 ms |
25916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
2 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
3 ms |
620 KB |
Output is correct |
12 |
Correct |
5 ms |
1008 KB |
Output is correct |
13 |
Correct |
6 ms |
1008 KB |
Output is correct |
14 |
Correct |
23 ms |
1264 KB |
Output is correct |
15 |
Correct |
7 ms |
1264 KB |
Output is correct |
16 |
Correct |
6 ms |
1264 KB |
Output is correct |
17 |
Correct |
5 ms |
880 KB |
Output is correct |
18 |
Correct |
5 ms |
1264 KB |
Output is correct |
19 |
Correct |
2485 ms |
168404 KB |
Output is correct |
20 |
Correct |
2506 ms |
168596 KB |
Output is correct |
21 |
Correct |
2443 ms |
168572 KB |
Output is correct |
22 |
Correct |
2468 ms |
168532 KB |
Output is correct |
23 |
Correct |
2324 ms |
168568 KB |
Output is correct |
24 |
Correct |
254 ms |
12520 KB |
Output is correct |
25 |
Correct |
207 ms |
12624 KB |
Output is correct |
26 |
Correct |
286 ms |
12540 KB |
Output is correct |
27 |
Correct |
290 ms |
13008 KB |
Output is correct |
28 |
Correct |
293 ms |
13904 KB |
Output is correct |
29 |
Correct |
234 ms |
13680 KB |
Output is correct |
30 |
Correct |
228 ms |
13168 KB |
Output is correct |
31 |
Correct |
195 ms |
13572 KB |
Output is correct |
32 |
Correct |
60 ms |
1664 KB |
Output is correct |
33 |
Correct |
271 ms |
13132 KB |
Output is correct |
34 |
Correct |
620 ms |
25276 KB |
Output is correct |
35 |
Correct |
572 ms |
26340 KB |
Output is correct |
36 |
Correct |
470 ms |
25256 KB |
Output is correct |
37 |
Correct |
578 ms |
25736 KB |
Output is correct |
38 |
Correct |
459 ms |
25984 KB |
Output is correct |
39 |
Correct |
518 ms |
25940 KB |
Output is correct |
40 |
Correct |
569 ms |
25400 KB |
Output is correct |
41 |
Correct |
674 ms |
25276 KB |
Output is correct |
42 |
Correct |
694 ms |
25364 KB |
Output is correct |
43 |
Correct |
740 ms |
25372 KB |
Output is correct |
44 |
Correct |
782 ms |
25356 KB |
Output is correct |
45 |
Correct |
788 ms |
25252 KB |
Output is correct |
46 |
Correct |
811 ms |
25592 KB |
Output is correct |
47 |
Correct |
801 ms |
26220 KB |
Output is correct |
48 |
Correct |
730 ms |
25276 KB |
Output is correct |
49 |
Correct |
555 ms |
26328 KB |
Output is correct |
50 |
Correct |
556 ms |
26300 KB |
Output is correct |
51 |
Correct |
577 ms |
25360 KB |
Output is correct |
52 |
Correct |
532 ms |
25276 KB |
Output is correct |
53 |
Correct |
507 ms |
25300 KB |
Output is correct |
54 |
Correct |
575 ms |
25340 KB |
Output is correct |
55 |
Correct |
409 ms |
22140 KB |
Output is correct |
56 |
Correct |
517 ms |
25916 KB |
Output is correct |
57 |
Incorrect |
2341 ms |
169520 KB |
Output isn't correct |
58 |
Halted |
0 ms |
0 KB |
- |