#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;
typedef pair<int, int> pi;
const int INF = 1e9 + 1;
struct SegTree {
int size;
vector<int> tree;
void init(int N) {
size = 1;
while (size < N) {
size *= 2;
}
tree.assign(2 * size, -INF);
}
void set(int i, int v, int x, int lx, int rx) {
if (lx == rx - 1) {
tree[x] = v;
return;
}
int mid = (lx + rx) / 2;
if (i < mid) {
set(i, v, 2 * x + 1, lx, mid);
}
else {
set(i, v, 2 * x + 2, mid, rx);
}
tree[x] = max(tree[2 * x + 1], tree[2 * x + 2]);
}
void set(int i, int v) {
set(i, v, 0, 0, size);
}
int calc(int l, int r, int x, int lx, int rx) {
if (lx >= r || l >= rx) {
return -INF;
}
else if (l <= lx && rx <= r) {
return tree[x];
}
int mid = (lx + rx) / 2;
return max(calc(l, r, 2 * x + 1, lx, mid), calc(l, r, 2 * x + 2, mid, rx));
}
int calc(int l, int r) {
return calc(l, r, 0, 0, size);
}
};
struct Query {
int l, r, k;
int id;
};
bool querysort(Query a, Query b)
{
return a.l > b.l;
}
int N, M;
vector<int> w;
vector<Query> queries;
vector<int> ans;
SegTree st;
SegTree vals;
int invcomp(int a, int b)
{
return a > b;
}
// Get last place in local maximum array that is BEFORE r
int getlast(int r, vector<int>& s)
{
int pos = upper_bound(s.begin(), s.end(), r, invcomp) - s.begin();
return s[pos];
}
int main(void)
{
scanf("%d %d", &N, &M);
w.resize(N);
st.init(N + 1);
vals.init(N + 1);
queries.resize(M);
ans.resize(M);
for (int i = 0; i < N; i++) {
scanf("%d", &w[i]);
}
w.pb(INF);
for (int i = 0; i < M; i++) {
scanf("%d %d %d", &queries[i].l, &queries[i].r, &queries[i].k);
queries[i].l--;
queries[i].id = i;
}
sort(queries.begin(), queries.end(), querysort);
vector<int> s;
s.pb(N);
int p = 0;
for (int i = N - 1; i >= 0; i--) {
while (w[s.back()] < w[i]) {
vals.set(s.back(), -INF);
s.pop_back();
}
st.set(i, w[i]);
int curopt = st.calc(i + 1, s.back());
if (curopt >= 0) {
curopt += w[i];
}
vals.set(i, curopt);
s.pb(i);
while (p < M && queries[p].l == i) {
Query cur = queries[p];
int pos = getlast(cur.r, s);
int res = vals.calc(cur.l, pos);
if (pos + 1 != cur.r) {
res = max(res, st.calc(pos + 1, cur.r) + w[pos]);
}
ans[cur.id] = res <= cur.k;
p++;
}
}
for (int i = 0; i < M; i++) {
printf("%d\n", ans[i]);
}
return 0;
}
Compilation message
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:94:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
94 | scanf("%d %d", &N, &M);
| ~~~~~^~~~~~~~~~~~~~~~~
sortbooks.cpp:103:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
103 | scanf("%d", &w[i]);
| ~~~~~^~~~~~~~~~~~~
sortbooks.cpp:108:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | scanf("%d %d %d", &queries[i].l, &queries[i].r, &queries[i].k);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
4 ms |
600 KB |
Output is correct |
13 |
Correct |
4 ms |
604 KB |
Output is correct |
14 |
Correct |
5 ms |
844 KB |
Output is correct |
15 |
Correct |
5 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
704 KB |
Output is correct |
17 |
Correct |
5 ms |
604 KB |
Output is correct |
18 |
Correct |
5 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1060 ms |
44172 KB |
Output is correct |
2 |
Correct |
1054 ms |
44116 KB |
Output is correct |
3 |
Correct |
1054 ms |
44112 KB |
Output is correct |
4 |
Correct |
1059 ms |
44112 KB |
Output is correct |
5 |
Correct |
1085 ms |
50284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
5208 KB |
Output is correct |
2 |
Correct |
108 ms |
6776 KB |
Output is correct |
3 |
Correct |
83 ms |
7820 KB |
Output is correct |
4 |
Correct |
84 ms |
7912 KB |
Output is correct |
5 |
Correct |
85 ms |
7880 KB |
Output is correct |
6 |
Correct |
72 ms |
7580 KB |
Output is correct |
7 |
Correct |
81 ms |
7636 KB |
Output is correct |
8 |
Correct |
87 ms |
7116 KB |
Output is correct |
9 |
Correct |
33 ms |
3920 KB |
Output is correct |
10 |
Correct |
83 ms |
7132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
4 ms |
600 KB |
Output is correct |
13 |
Correct |
4 ms |
604 KB |
Output is correct |
14 |
Correct |
5 ms |
844 KB |
Output is correct |
15 |
Correct |
5 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
704 KB |
Output is correct |
17 |
Correct |
5 ms |
604 KB |
Output is correct |
18 |
Correct |
5 ms |
860 KB |
Output is correct |
19 |
Correct |
224 ms |
16132 KB |
Output is correct |
20 |
Correct |
221 ms |
16192 KB |
Output is correct |
21 |
Correct |
193 ms |
15932 KB |
Output is correct |
22 |
Correct |
189 ms |
15916 KB |
Output is correct |
23 |
Correct |
196 ms |
15920 KB |
Output is correct |
24 |
Correct |
159 ms |
17540 KB |
Output is correct |
25 |
Correct |
157 ms |
17540 KB |
Output is correct |
26 |
Correct |
184 ms |
17928 KB |
Output is correct |
27 |
Correct |
209 ms |
17900 KB |
Output is correct |
28 |
Correct |
180 ms |
17728 KB |
Output is correct |
29 |
Correct |
202 ms |
17924 KB |
Output is correct |
30 |
Correct |
242 ms |
17904 KB |
Output is correct |
31 |
Correct |
241 ms |
17800 KB |
Output is correct |
32 |
Correct |
197 ms |
17932 KB |
Output is correct |
33 |
Correct |
250 ms |
17800 KB |
Output is correct |
34 |
Correct |
147 ms |
17468 KB |
Output is correct |
35 |
Correct |
147 ms |
17472 KB |
Output is correct |
36 |
Correct |
153 ms |
17212 KB |
Output is correct |
37 |
Correct |
169 ms |
17220 KB |
Output is correct |
38 |
Correct |
157 ms |
17416 KB |
Output is correct |
39 |
Correct |
177 ms |
16200 KB |
Output is correct |
40 |
Correct |
141 ms |
14712 KB |
Output is correct |
41 |
Correct |
173 ms |
15168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
4 ms |
600 KB |
Output is correct |
13 |
Correct |
4 ms |
604 KB |
Output is correct |
14 |
Correct |
5 ms |
844 KB |
Output is correct |
15 |
Correct |
5 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
704 KB |
Output is correct |
17 |
Correct |
5 ms |
604 KB |
Output is correct |
18 |
Correct |
5 ms |
860 KB |
Output is correct |
19 |
Correct |
1060 ms |
44172 KB |
Output is correct |
20 |
Correct |
1054 ms |
44116 KB |
Output is correct |
21 |
Correct |
1054 ms |
44112 KB |
Output is correct |
22 |
Correct |
1059 ms |
44112 KB |
Output is correct |
23 |
Correct |
1085 ms |
50284 KB |
Output is correct |
24 |
Correct |
95 ms |
5208 KB |
Output is correct |
25 |
Correct |
108 ms |
6776 KB |
Output is correct |
26 |
Correct |
83 ms |
7820 KB |
Output is correct |
27 |
Correct |
84 ms |
7912 KB |
Output is correct |
28 |
Correct |
85 ms |
7880 KB |
Output is correct |
29 |
Correct |
72 ms |
7580 KB |
Output is correct |
30 |
Correct |
81 ms |
7636 KB |
Output is correct |
31 |
Correct |
87 ms |
7116 KB |
Output is correct |
32 |
Correct |
33 ms |
3920 KB |
Output is correct |
33 |
Correct |
83 ms |
7132 KB |
Output is correct |
34 |
Correct |
224 ms |
16132 KB |
Output is correct |
35 |
Correct |
221 ms |
16192 KB |
Output is correct |
36 |
Correct |
193 ms |
15932 KB |
Output is correct |
37 |
Correct |
189 ms |
15916 KB |
Output is correct |
38 |
Correct |
196 ms |
15920 KB |
Output is correct |
39 |
Correct |
159 ms |
17540 KB |
Output is correct |
40 |
Correct |
157 ms |
17540 KB |
Output is correct |
41 |
Correct |
184 ms |
17928 KB |
Output is correct |
42 |
Correct |
209 ms |
17900 KB |
Output is correct |
43 |
Correct |
180 ms |
17728 KB |
Output is correct |
44 |
Correct |
202 ms |
17924 KB |
Output is correct |
45 |
Correct |
242 ms |
17904 KB |
Output is correct |
46 |
Correct |
241 ms |
17800 KB |
Output is correct |
47 |
Correct |
197 ms |
17932 KB |
Output is correct |
48 |
Correct |
250 ms |
17800 KB |
Output is correct |
49 |
Correct |
147 ms |
17468 KB |
Output is correct |
50 |
Correct |
147 ms |
17472 KB |
Output is correct |
51 |
Correct |
153 ms |
17212 KB |
Output is correct |
52 |
Correct |
169 ms |
17220 KB |
Output is correct |
53 |
Correct |
157 ms |
17416 KB |
Output is correct |
54 |
Correct |
177 ms |
16200 KB |
Output is correct |
55 |
Correct |
141 ms |
14712 KB |
Output is correct |
56 |
Correct |
173 ms |
15168 KB |
Output is correct |
57 |
Correct |
1069 ms |
75700 KB |
Output is correct |
58 |
Correct |
1059 ms |
75788 KB |
Output is correct |
59 |
Correct |
1012 ms |
75564 KB |
Output is correct |
60 |
Correct |
1047 ms |
75532 KB |
Output is correct |
61 |
Correct |
1051 ms |
75564 KB |
Output is correct |
62 |
Correct |
1070 ms |
75564 KB |
Output is correct |
63 |
Correct |
833 ms |
81720 KB |
Output is correct |
64 |
Correct |
820 ms |
81728 KB |
Output is correct |
65 |
Correct |
941 ms |
83636 KB |
Output is correct |
66 |
Correct |
991 ms |
83740 KB |
Output is correct |
67 |
Correct |
1027 ms |
83584 KB |
Output is correct |
68 |
Correct |
1078 ms |
83716 KB |
Output is correct |
69 |
Correct |
1055 ms |
83688 KB |
Output is correct |
70 |
Correct |
1038 ms |
83724 KB |
Output is correct |
71 |
Correct |
1083 ms |
83840 KB |
Output is correct |
72 |
Correct |
1064 ms |
83772 KB |
Output is correct |
73 |
Correct |
809 ms |
80180 KB |
Output is correct |
74 |
Correct |
851 ms |
81156 KB |
Output is correct |
75 |
Correct |
822 ms |
80392 KB |
Output is correct |
76 |
Correct |
754 ms |
80396 KB |
Output is correct |
77 |
Correct |
751 ms |
80324 KB |
Output is correct |
78 |
Correct |
930 ms |
76300 KB |
Output is correct |
79 |
Correct |
712 ms |
66632 KB |
Output is correct |
80 |
Correct |
1018 ms |
70924 KB |
Output is correct |