#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
vector <int> g[maxn];
stack <int> st;
int a[maxn], t[4 * maxn];
int ans[maxn], res[maxn], l[maxn], r[maxn], k[maxn];
void upd(int v, int tl, int tr, int pos, int val) {
if (tl == tr) {
t[v] = max(val + a[pos], t[v]);
return;
}
int mid = tl + tr >> 1;
if (pos <= mid) upd(v + v, tl, mid, pos, val);
else upd(v + v + 1, mid + 1, tr, pos, val);
t[v] = max(t[v + v], t[v + v + 1]);
}
int get(int v, int tl, int tr, int l, int r) {
if (l > tr || r < tl) return 0;
if (l <= tl && tr <= r) return t[v];
int mid = tl + tr >> 1;
return max(get(v + v, tl, mid, l, r), get(v + v + 1, mid + 1, tr, l, r));
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
while (!st.empty() && a[st.top()] <= a[i]) {
st.pop();
}
if (st.empty()) ans[i] = -1;
else ans[i] = st.top();
st.push(i);
}
for (int i = 1; i <= m; i++) {
cin >> l[i] >> r[i] >> k[i];
g[r[i]].pb(i);
}
for (int i = 1; i <= n; i++) {
if (ans[i] != -1) upd(1, 1, n, ans[i], a[i]);
for (auto to : g[i]) res[to] = (get(1, 1, n, l[to], r[to]) > k[to] ? 0 : 1);
}
for (int i = 1; i <= m; i++) {
cout << res[i] << '\n';
}
}
Compilation message
sortbooks.cpp: In function 'void upd(long long int, long long int, long long int, long long int, long long int)':
sortbooks.cpp:22:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
22 | int mid = tl + tr >> 1;
| ~~~^~~~
sortbooks.cpp: In function 'long long int get(long long int, long long int, long long int, long long int, long long int)':
sortbooks.cpp:31:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
31 | int mid = tl + tr >> 1;
| ~~~^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23952 KB |
Output is correct |
4 |
Correct |
11 ms |
23896 KB |
Output is correct |
5 |
Correct |
12 ms |
23900 KB |
Output is correct |
6 |
Correct |
13 ms |
24248 KB |
Output is correct |
7 |
Correct |
12 ms |
23900 KB |
Output is correct |
8 |
Correct |
12 ms |
24048 KB |
Output is correct |
9 |
Correct |
12 ms |
23900 KB |
Output is correct |
10 |
Correct |
12 ms |
23900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23952 KB |
Output is correct |
4 |
Correct |
11 ms |
23896 KB |
Output is correct |
5 |
Correct |
12 ms |
23900 KB |
Output is correct |
6 |
Correct |
13 ms |
24248 KB |
Output is correct |
7 |
Correct |
12 ms |
23900 KB |
Output is correct |
8 |
Correct |
12 ms |
24048 KB |
Output is correct |
9 |
Correct |
12 ms |
23900 KB |
Output is correct |
10 |
Correct |
12 ms |
23900 KB |
Output is correct |
11 |
Correct |
14 ms |
24156 KB |
Output is correct |
12 |
Correct |
14 ms |
24664 KB |
Output is correct |
13 |
Correct |
14 ms |
24444 KB |
Output is correct |
14 |
Correct |
15 ms |
24408 KB |
Output is correct |
15 |
Correct |
14 ms |
24408 KB |
Output is correct |
16 |
Correct |
15 ms |
24300 KB |
Output is correct |
17 |
Correct |
13 ms |
24412 KB |
Output is correct |
18 |
Correct |
17 ms |
24396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1314 ms |
140944 KB |
Output is correct |
2 |
Correct |
1371 ms |
141828 KB |
Output is correct |
3 |
Correct |
1314 ms |
141556 KB |
Output is correct |
4 |
Correct |
1348 ms |
141700 KB |
Output is correct |
5 |
Correct |
1061 ms |
125508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
82 ms |
34796 KB |
Output is correct |
2 |
Correct |
80 ms |
34368 KB |
Output is correct |
3 |
Correct |
74 ms |
32640 KB |
Output is correct |
4 |
Correct |
75 ms |
32636 KB |
Output is correct |
5 |
Correct |
75 ms |
32612 KB |
Output is correct |
6 |
Correct |
61 ms |
31700 KB |
Output is correct |
7 |
Correct |
66 ms |
31824 KB |
Output is correct |
8 |
Correct |
65 ms |
32192 KB |
Output is correct |
9 |
Correct |
39 ms |
29432 KB |
Output is correct |
10 |
Correct |
65 ms |
32204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23952 KB |
Output is correct |
4 |
Correct |
11 ms |
23896 KB |
Output is correct |
5 |
Correct |
12 ms |
23900 KB |
Output is correct |
6 |
Correct |
13 ms |
24248 KB |
Output is correct |
7 |
Correct |
12 ms |
23900 KB |
Output is correct |
8 |
Correct |
12 ms |
24048 KB |
Output is correct |
9 |
Correct |
12 ms |
23900 KB |
Output is correct |
10 |
Correct |
12 ms |
23900 KB |
Output is correct |
11 |
Correct |
14 ms |
24156 KB |
Output is correct |
12 |
Correct |
14 ms |
24664 KB |
Output is correct |
13 |
Correct |
14 ms |
24444 KB |
Output is correct |
14 |
Correct |
15 ms |
24408 KB |
Output is correct |
15 |
Correct |
14 ms |
24408 KB |
Output is correct |
16 |
Correct |
15 ms |
24300 KB |
Output is correct |
17 |
Correct |
13 ms |
24412 KB |
Output is correct |
18 |
Correct |
17 ms |
24396 KB |
Output is correct |
19 |
Correct |
219 ms |
48484 KB |
Output is correct |
20 |
Correct |
184 ms |
48208 KB |
Output is correct |
21 |
Correct |
159 ms |
46940 KB |
Output is correct |
22 |
Correct |
164 ms |
46976 KB |
Output is correct |
23 |
Correct |
158 ms |
46932 KB |
Output is correct |
24 |
Correct |
140 ms |
42492 KB |
Output is correct |
25 |
Correct |
134 ms |
42832 KB |
Output is correct |
26 |
Correct |
146 ms |
43140 KB |
Output is correct |
27 |
Correct |
148 ms |
43152 KB |
Output is correct |
28 |
Correct |
170 ms |
43344 KB |
Output is correct |
29 |
Correct |
156 ms |
43988 KB |
Output is correct |
30 |
Correct |
165 ms |
44116 KB |
Output is correct |
31 |
Correct |
167 ms |
44116 KB |
Output is correct |
32 |
Correct |
156 ms |
44176 KB |
Output is correct |
33 |
Correct |
167 ms |
44068 KB |
Output is correct |
34 |
Correct |
131 ms |
42272 KB |
Output is correct |
35 |
Correct |
122 ms |
42156 KB |
Output is correct |
36 |
Correct |
130 ms |
42068 KB |
Output is correct |
37 |
Correct |
123 ms |
42096 KB |
Output is correct |
38 |
Correct |
122 ms |
42068 KB |
Output is correct |
39 |
Correct |
164 ms |
43596 KB |
Output is correct |
40 |
Correct |
111 ms |
40140 KB |
Output is correct |
41 |
Correct |
138 ms |
41836 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
23900 KB |
Output is correct |
2 |
Correct |
10 ms |
23900 KB |
Output is correct |
3 |
Correct |
11 ms |
23952 KB |
Output is correct |
4 |
Correct |
11 ms |
23896 KB |
Output is correct |
5 |
Correct |
12 ms |
23900 KB |
Output is correct |
6 |
Correct |
13 ms |
24248 KB |
Output is correct |
7 |
Correct |
12 ms |
23900 KB |
Output is correct |
8 |
Correct |
12 ms |
24048 KB |
Output is correct |
9 |
Correct |
12 ms |
23900 KB |
Output is correct |
10 |
Correct |
12 ms |
23900 KB |
Output is correct |
11 |
Correct |
14 ms |
24156 KB |
Output is correct |
12 |
Correct |
14 ms |
24664 KB |
Output is correct |
13 |
Correct |
14 ms |
24444 KB |
Output is correct |
14 |
Correct |
15 ms |
24408 KB |
Output is correct |
15 |
Correct |
14 ms |
24408 KB |
Output is correct |
16 |
Correct |
15 ms |
24300 KB |
Output is correct |
17 |
Correct |
13 ms |
24412 KB |
Output is correct |
18 |
Correct |
17 ms |
24396 KB |
Output is correct |
19 |
Correct |
1314 ms |
140944 KB |
Output is correct |
20 |
Correct |
1371 ms |
141828 KB |
Output is correct |
21 |
Correct |
1314 ms |
141556 KB |
Output is correct |
22 |
Correct |
1348 ms |
141700 KB |
Output is correct |
23 |
Correct |
1061 ms |
125508 KB |
Output is correct |
24 |
Correct |
82 ms |
34796 KB |
Output is correct |
25 |
Correct |
80 ms |
34368 KB |
Output is correct |
26 |
Correct |
74 ms |
32640 KB |
Output is correct |
27 |
Correct |
75 ms |
32636 KB |
Output is correct |
28 |
Correct |
75 ms |
32612 KB |
Output is correct |
29 |
Correct |
61 ms |
31700 KB |
Output is correct |
30 |
Correct |
66 ms |
31824 KB |
Output is correct |
31 |
Correct |
65 ms |
32192 KB |
Output is correct |
32 |
Correct |
39 ms |
29432 KB |
Output is correct |
33 |
Correct |
65 ms |
32204 KB |
Output is correct |
34 |
Correct |
219 ms |
48484 KB |
Output is correct |
35 |
Correct |
184 ms |
48208 KB |
Output is correct |
36 |
Correct |
159 ms |
46940 KB |
Output is correct |
37 |
Correct |
164 ms |
46976 KB |
Output is correct |
38 |
Correct |
158 ms |
46932 KB |
Output is correct |
39 |
Correct |
140 ms |
42492 KB |
Output is correct |
40 |
Correct |
134 ms |
42832 KB |
Output is correct |
41 |
Correct |
146 ms |
43140 KB |
Output is correct |
42 |
Correct |
148 ms |
43152 KB |
Output is correct |
43 |
Correct |
170 ms |
43344 KB |
Output is correct |
44 |
Correct |
156 ms |
43988 KB |
Output is correct |
45 |
Correct |
165 ms |
44116 KB |
Output is correct |
46 |
Correct |
167 ms |
44116 KB |
Output is correct |
47 |
Correct |
156 ms |
44176 KB |
Output is correct |
48 |
Correct |
167 ms |
44068 KB |
Output is correct |
49 |
Correct |
131 ms |
42272 KB |
Output is correct |
50 |
Correct |
122 ms |
42156 KB |
Output is correct |
51 |
Correct |
130 ms |
42068 KB |
Output is correct |
52 |
Correct |
123 ms |
42096 KB |
Output is correct |
53 |
Correct |
122 ms |
42068 KB |
Output is correct |
54 |
Correct |
164 ms |
43596 KB |
Output is correct |
55 |
Correct |
111 ms |
40140 KB |
Output is correct |
56 |
Correct |
138 ms |
41836 KB |
Output is correct |
57 |
Correct |
1281 ms |
141232 KB |
Output is correct |
58 |
Correct |
1285 ms |
141444 KB |
Output is correct |
59 |
Correct |
1205 ms |
138724 KB |
Output is correct |
60 |
Correct |
1223 ms |
138808 KB |
Output is correct |
61 |
Correct |
1212 ms |
138576 KB |
Output is correct |
62 |
Correct |
1215 ms |
138808 KB |
Output is correct |
63 |
Correct |
745 ms |
116804 KB |
Output is correct |
64 |
Correct |
744 ms |
116004 KB |
Output is correct |
65 |
Correct |
950 ms |
121960 KB |
Output is correct |
66 |
Correct |
950 ms |
121964 KB |
Output is correct |
67 |
Correct |
970 ms |
122128 KB |
Output is correct |
68 |
Correct |
1031 ms |
125108 KB |
Output is correct |
69 |
Correct |
1045 ms |
124764 KB |
Output is correct |
70 |
Correct |
1034 ms |
124276 KB |
Output is correct |
71 |
Correct |
1016 ms |
124488 KB |
Output is correct |
72 |
Correct |
1018 ms |
124300 KB |
Output is correct |
73 |
Correct |
725 ms |
113388 KB |
Output is correct |
74 |
Correct |
699 ms |
114496 KB |
Output is correct |
75 |
Correct |
696 ms |
113436 KB |
Output is correct |
76 |
Correct |
686 ms |
113408 KB |
Output is correct |
77 |
Correct |
708 ms |
113256 KB |
Output is correct |
78 |
Correct |
915 ms |
122392 KB |
Output is correct |
79 |
Correct |
633 ms |
102840 KB |
Output is correct |
80 |
Correct |
887 ms |
114948 KB |
Output is correct |