#include <bits/stdc++.h>
using namespace std;
int w[1000005];
int ans[1000005];
int rt[4000005];
int lb[4000005];
int rb[4000005];
void build(int c, int l, int r) {
lb[c] = l;
rb[c] = r;
rt[c] = 1e9;
int k = (l + r) / 2;
if (l != r) {
build(2 * c, l, k);
build(2 * c + 1, k + 1, r);
}
}
void update(int c, int l, int r, int x) {
if (lb[c] == l && rb[c] == r) {
rt[c] = min(rt[c], x);
return;
}
int k = (lb[c] + rb[c]) / 2;
if (l <= k && k < r) {
update(2 * c, l, k, x);
update(2 * c + 1, k + 1, r, x);
} else if (r <= k) {
update(2 * c, l, r, x);
} else {
update(2 * c + 1, l, r, x);
}
}
int query(int c, int i, int mx = 1e9) {
mx = min(mx, rt[c]);
if (lb[c] == rb[c])
return mx;
int k = (lb[c] + rb[c]) / 2;
if (i <= k)
return query(2 * c, i, mx);
else
return query(2 * c + 1, i, mx);
}
void maintain(int i, int j) {
// add (i, j) to some range data structure...
update(1, 1, i, j);
}
bool check(int i, int j) {
// check whether there exists a pair (i', j') such that i <= i' < j' <= j in
// the data structure
return query(1, i) <= j;
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
stack<int> stk;
vector<pair<int, int>> interesting;
for (int i = 1; i <= n; i++) {
scanf("%d", w + i);
while (!stk.empty() && w[stk.top()] <= w[i])
stk.pop();
if (!stk.empty())
interesting.push_back({stk.top(), i});
stk.push(i);
}
while (!stk.empty())
stk.pop();
for (int i = n; i > 0; i--) {
while (!stk.empty() && w[stk.top()] >= w[i])
stk.pop();
if (!stk.empty())
interesting.push_back({i, stk.top()});
stk.push(i);
}
sort(interesting.begin(), interesting.end(),
[](pair<int, int> x, pair<int, int> y) {
return w[x.first] + w[x.second] > w[y.first] + w[y.second];
});
vector<tuple<int, int, int, int>> queries;
for (int i = 0; i < m; i++) {
int l, r, k;
scanf("%d%d%d", &l, &r, &k);
queries.emplace_back(l, r, k, i);
}
sort(queries.begin(), queries.end(),
[](tuple<int, int, int, int> x, tuple<int, int, int, int> y) {
return get<2>(x) > get<2>(y);
});
build(1, 1, n);
int lptr = 0;
for (auto q : queries) {
int l, r, k, i;
tie(l, r, k, i) = q;
while (lptr < interesting.size() &&
w[interesting[lptr].first] + w[interesting[lptr].second] > k) {
maintain(interesting[lptr].first, interesting[lptr].second);
lptr++;
}
ans[i] = check(l, r) ? 0 : 1;
}
for (int i = 0; i < m; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
Compilation message
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:101:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
101 | while (lptr < interesting.size() &&
| ~~~~~^~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:62:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
sortbooks.cpp:66:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | scanf("%d", w + i);
| ~~~~~^~~~~~~~~~~~~
sortbooks.cpp:89:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
89 | scanf("%d%d%d", &l, &r, &k);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
4 ms |
724 KB |
Output is correct |
13 |
Correct |
5 ms |
724 KB |
Output is correct |
14 |
Correct |
6 ms |
852 KB |
Output is correct |
15 |
Correct |
5 ms |
868 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
3 ms |
724 KB |
Output is correct |
18 |
Correct |
3 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2278 ms |
66176 KB |
Output is correct |
2 |
Correct |
2304 ms |
66336 KB |
Output is correct |
3 |
Correct |
2285 ms |
66168 KB |
Output is correct |
4 |
Correct |
2351 ms |
66300 KB |
Output is correct |
5 |
Correct |
1148 ms |
50724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
7608 KB |
Output is correct |
2 |
Correct |
124 ms |
7924 KB |
Output is correct |
3 |
Correct |
57 ms |
6444 KB |
Output is correct |
4 |
Correct |
58 ms |
6396 KB |
Output is correct |
5 |
Correct |
59 ms |
6392 KB |
Output is correct |
6 |
Correct |
50 ms |
6504 KB |
Output is correct |
7 |
Correct |
56 ms |
6480 KB |
Output is correct |
8 |
Correct |
68 ms |
7116 KB |
Output is correct |
9 |
Correct |
38 ms |
3008 KB |
Output is correct |
10 |
Correct |
66 ms |
7016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
4 ms |
724 KB |
Output is correct |
13 |
Correct |
5 ms |
724 KB |
Output is correct |
14 |
Correct |
6 ms |
852 KB |
Output is correct |
15 |
Correct |
5 ms |
868 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
3 ms |
724 KB |
Output is correct |
18 |
Correct |
3 ms |
724 KB |
Output is correct |
19 |
Correct |
321 ms |
14976 KB |
Output is correct |
20 |
Correct |
318 ms |
15332 KB |
Output is correct |
21 |
Correct |
294 ms |
15148 KB |
Output is correct |
22 |
Correct |
298 ms |
15216 KB |
Output is correct |
23 |
Correct |
290 ms |
15196 KB |
Output is correct |
24 |
Correct |
143 ms |
12748 KB |
Output is correct |
25 |
Correct |
145 ms |
12092 KB |
Output is correct |
26 |
Correct |
139 ms |
12024 KB |
Output is correct |
27 |
Correct |
139 ms |
12852 KB |
Output is correct |
28 |
Correct |
150 ms |
12788 KB |
Output is correct |
29 |
Correct |
156 ms |
12076 KB |
Output is correct |
30 |
Correct |
159 ms |
12048 KB |
Output is correct |
31 |
Correct |
165 ms |
12080 KB |
Output is correct |
32 |
Correct |
162 ms |
12080 KB |
Output is correct |
33 |
Correct |
164 ms |
12340 KB |
Output is correct |
34 |
Correct |
116 ms |
12564 KB |
Output is correct |
35 |
Correct |
186 ms |
13780 KB |
Output is correct |
36 |
Correct |
114 ms |
14120 KB |
Output is correct |
37 |
Correct |
116 ms |
14516 KB |
Output is correct |
38 |
Correct |
128 ms |
14808 KB |
Output is correct |
39 |
Correct |
188 ms |
16232 KB |
Output is correct |
40 |
Correct |
133 ms |
12672 KB |
Output is correct |
41 |
Correct |
149 ms |
16552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
4 ms |
724 KB |
Output is correct |
13 |
Correct |
5 ms |
724 KB |
Output is correct |
14 |
Correct |
6 ms |
852 KB |
Output is correct |
15 |
Correct |
5 ms |
868 KB |
Output is correct |
16 |
Correct |
3 ms |
724 KB |
Output is correct |
17 |
Correct |
3 ms |
724 KB |
Output is correct |
18 |
Correct |
3 ms |
724 KB |
Output is correct |
19 |
Correct |
2278 ms |
66176 KB |
Output is correct |
20 |
Correct |
2304 ms |
66336 KB |
Output is correct |
21 |
Correct |
2285 ms |
66168 KB |
Output is correct |
22 |
Correct |
2351 ms |
66300 KB |
Output is correct |
23 |
Correct |
1148 ms |
50724 KB |
Output is correct |
24 |
Correct |
134 ms |
7608 KB |
Output is correct |
25 |
Correct |
124 ms |
7924 KB |
Output is correct |
26 |
Correct |
57 ms |
6444 KB |
Output is correct |
27 |
Correct |
58 ms |
6396 KB |
Output is correct |
28 |
Correct |
59 ms |
6392 KB |
Output is correct |
29 |
Correct |
50 ms |
6504 KB |
Output is correct |
30 |
Correct |
56 ms |
6480 KB |
Output is correct |
31 |
Correct |
68 ms |
7116 KB |
Output is correct |
32 |
Correct |
38 ms |
3008 KB |
Output is correct |
33 |
Correct |
66 ms |
7016 KB |
Output is correct |
34 |
Correct |
321 ms |
14976 KB |
Output is correct |
35 |
Correct |
318 ms |
15332 KB |
Output is correct |
36 |
Correct |
294 ms |
15148 KB |
Output is correct |
37 |
Correct |
298 ms |
15216 KB |
Output is correct |
38 |
Correct |
290 ms |
15196 KB |
Output is correct |
39 |
Correct |
143 ms |
12748 KB |
Output is correct |
40 |
Correct |
145 ms |
12092 KB |
Output is correct |
41 |
Correct |
139 ms |
12024 KB |
Output is correct |
42 |
Correct |
139 ms |
12852 KB |
Output is correct |
43 |
Correct |
150 ms |
12788 KB |
Output is correct |
44 |
Correct |
156 ms |
12076 KB |
Output is correct |
45 |
Correct |
159 ms |
12048 KB |
Output is correct |
46 |
Correct |
165 ms |
12080 KB |
Output is correct |
47 |
Correct |
162 ms |
12080 KB |
Output is correct |
48 |
Correct |
164 ms |
12340 KB |
Output is correct |
49 |
Correct |
116 ms |
12564 KB |
Output is correct |
50 |
Correct |
186 ms |
13780 KB |
Output is correct |
51 |
Correct |
114 ms |
14120 KB |
Output is correct |
52 |
Correct |
116 ms |
14516 KB |
Output is correct |
53 |
Correct |
128 ms |
14808 KB |
Output is correct |
54 |
Correct |
188 ms |
16232 KB |
Output is correct |
55 |
Correct |
133 ms |
12672 KB |
Output is correct |
56 |
Correct |
149 ms |
16552 KB |
Output is correct |
57 |
Correct |
2657 ms |
66352 KB |
Output is correct |
58 |
Correct |
2627 ms |
66408 KB |
Output is correct |
59 |
Correct |
2510 ms |
66324 KB |
Output is correct |
60 |
Correct |
2568 ms |
66348 KB |
Output is correct |
61 |
Correct |
2494 ms |
66436 KB |
Output is correct |
62 |
Correct |
2473 ms |
66496 KB |
Output is correct |
63 |
Correct |
634 ms |
50580 KB |
Output is correct |
64 |
Correct |
626 ms |
50596 KB |
Output is correct |
65 |
Correct |
1006 ms |
50704 KB |
Output is correct |
66 |
Correct |
976 ms |
50728 KB |
Output is correct |
67 |
Correct |
1024 ms |
50700 KB |
Output is correct |
68 |
Correct |
1181 ms |
50700 KB |
Output is correct |
69 |
Correct |
1192 ms |
50740 KB |
Output is correct |
70 |
Correct |
1146 ms |
50804 KB |
Output is correct |
71 |
Correct |
1132 ms |
50668 KB |
Output is correct |
72 |
Correct |
1150 ms |
50708 KB |
Output is correct |
73 |
Correct |
566 ms |
50584 KB |
Output is correct |
74 |
Correct |
588 ms |
50656 KB |
Output is correct |
75 |
Correct |
565 ms |
50556 KB |
Output is correct |
76 |
Correct |
592 ms |
50564 KB |
Output is correct |
77 |
Correct |
582 ms |
50664 KB |
Output is correct |
78 |
Correct |
1170 ms |
55944 KB |
Output is correct |
79 |
Correct |
743 ms |
39536 KB |
Output is correct |
80 |
Correct |
969 ms |
58044 KB |
Output is correct |