#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=1e6+5;
const int inf = 2e9 + 10;
int n, m;
int a[mxn] = {};
struct segtree {
int tree[4*mxn];
void update(int node,int b,int e, int p, int v) {
if(b > p || e < p) return;
if(b == e) {
tree[node] = v;
return;
}
int mid = b + e >> 1;
int left = node << 1;
int right = left | 1;
update(left, b, mid, p, v);
update(right, mid + 1, e, p, v);
tree[node] = max(tree[left], tree[right]);
}
int query(int node,int b,int e,int l,int r) {
if(e<l || b>r) return 0;
if(b >= l && e <= r) return tree[node];
int mid=b+e>>1;
int left=node<<1;
int right=left|1;
return max(query(left, b, mid, l, r), query(right, mid + 1, e, l, r));
}
} st;
int main() {
cin >> n >> m;
vector<pii> b;
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
b.emplace_back(a[i], i);
}
a[0] = inf;
stack<int> St;
St.push(0);
int nxt[n + 5];
vector<int> con[n + 5];
for(int i = 1; i <= n; i++) {
while(a[St.top()] <= a[i]) St.pop();
con[St.top()].push_back(i);
if(St.top() != 0) st.update(1, 1, n, i, a[St.top()] + a[i]);
St.push(i);
}
vector< tuple<int,int, int> > query[n + 1];
for(int i = 1; i <= m; i++) {
int l, r, k;
scanf("%d%d%d", &l, &r, &k);
query[l].emplace_back(r, i, k);
}
bool res[m + 1];
for(int l = 1; l <= n; l++) {
for(auto [r, i, k] : query[l]) {
int v = st.query(1, 1, n, l, r);
res[i] = (v <= k);
}
for(int x : con[l]) {
st.update(1, 1, n, x, 0);
}
}
for(int i = 1; i <= m; i++) {
printf("%d\n", res[i]);
}
}
Compilation message
sortbooks.cpp: In member function 'void segtree::update(int, int, int, int, int)':
sortbooks.cpp:19:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
19 | int mid = b + e >> 1;
| ~~^~~
sortbooks.cpp: In member function 'int segtree::query(int, int, int, int, int)':
sortbooks.cpp:29:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
29 | int mid=b+e>>1;
| ~^~
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:49:9: warning: unused variable 'nxt' [-Wunused-variable]
49 | int nxt[n + 5];
| ^~~
sortbooks.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
sortbooks.cpp:66:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
66 | scanf("%d%d%d", &l, &r, &k);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 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 |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 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 |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
5 ms |
716 KB |
Output is correct |
13 |
Correct |
5 ms |
716 KB |
Output is correct |
14 |
Correct |
8 ms |
844 KB |
Output is correct |
15 |
Correct |
6 ms |
844 KB |
Output is correct |
16 |
Correct |
7 ms |
716 KB |
Output is correct |
17 |
Correct |
6 ms |
588 KB |
Output is correct |
18 |
Correct |
6 ms |
740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1801 ms |
110588 KB |
Output is correct |
2 |
Correct |
1741 ms |
108516 KB |
Output is correct |
3 |
Correct |
1760 ms |
108432 KB |
Output is correct |
4 |
Correct |
1759 ms |
108424 KB |
Output is correct |
5 |
Correct |
1440 ms |
87928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
134 ms |
12468 KB |
Output is correct |
2 |
Correct |
115 ms |
11164 KB |
Output is correct |
3 |
Correct |
92 ms |
8952 KB |
Output is correct |
4 |
Correct |
99 ms |
8972 KB |
Output is correct |
5 |
Correct |
139 ms |
9136 KB |
Output is correct |
6 |
Correct |
72 ms |
8628 KB |
Output is correct |
7 |
Correct |
75 ms |
8720 KB |
Output is correct |
8 |
Correct |
95 ms |
9780 KB |
Output is correct |
9 |
Correct |
58 ms |
2104 KB |
Output is correct |
10 |
Correct |
94 ms |
9696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 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 |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
5 ms |
716 KB |
Output is correct |
13 |
Correct |
5 ms |
716 KB |
Output is correct |
14 |
Correct |
8 ms |
844 KB |
Output is correct |
15 |
Correct |
6 ms |
844 KB |
Output is correct |
16 |
Correct |
7 ms |
716 KB |
Output is correct |
17 |
Correct |
6 ms |
588 KB |
Output is correct |
18 |
Correct |
6 ms |
740 KB |
Output is correct |
19 |
Correct |
310 ms |
22484 KB |
Output is correct |
20 |
Correct |
294 ms |
22332 KB |
Output is correct |
21 |
Correct |
251 ms |
21876 KB |
Output is correct |
22 |
Correct |
298 ms |
21944 KB |
Output is correct |
23 |
Correct |
272 ms |
21884 KB |
Output is correct |
24 |
Correct |
186 ms |
17292 KB |
Output is correct |
25 |
Correct |
170 ms |
17392 KB |
Output is correct |
26 |
Correct |
209 ms |
17428 KB |
Output is correct |
27 |
Correct |
204 ms |
17424 KB |
Output is correct |
28 |
Correct |
217 ms |
17496 KB |
Output is correct |
29 |
Correct |
225 ms |
17828 KB |
Output is correct |
30 |
Correct |
242 ms |
17948 KB |
Output is correct |
31 |
Correct |
251 ms |
17864 KB |
Output is correct |
32 |
Correct |
268 ms |
17916 KB |
Output is correct |
33 |
Correct |
218 ms |
17856 KB |
Output is correct |
34 |
Correct |
155 ms |
16816 KB |
Output is correct |
35 |
Correct |
165 ms |
16724 KB |
Output is correct |
36 |
Correct |
155 ms |
16820 KB |
Output is correct |
37 |
Correct |
159 ms |
16716 KB |
Output is correct |
38 |
Correct |
164 ms |
16816 KB |
Output is correct |
39 |
Correct |
254 ms |
18668 KB |
Output is correct |
40 |
Correct |
171 ms |
12716 KB |
Output is correct |
41 |
Correct |
238 ms |
19168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
2 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 |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
3 ms |
460 KB |
Output is correct |
12 |
Correct |
5 ms |
716 KB |
Output is correct |
13 |
Correct |
5 ms |
716 KB |
Output is correct |
14 |
Correct |
8 ms |
844 KB |
Output is correct |
15 |
Correct |
6 ms |
844 KB |
Output is correct |
16 |
Correct |
7 ms |
716 KB |
Output is correct |
17 |
Correct |
6 ms |
588 KB |
Output is correct |
18 |
Correct |
6 ms |
740 KB |
Output is correct |
19 |
Correct |
1801 ms |
110588 KB |
Output is correct |
20 |
Correct |
1741 ms |
108516 KB |
Output is correct |
21 |
Correct |
1760 ms |
108432 KB |
Output is correct |
22 |
Correct |
1759 ms |
108424 KB |
Output is correct |
23 |
Correct |
1440 ms |
87928 KB |
Output is correct |
24 |
Correct |
134 ms |
12468 KB |
Output is correct |
25 |
Correct |
115 ms |
11164 KB |
Output is correct |
26 |
Correct |
92 ms |
8952 KB |
Output is correct |
27 |
Correct |
99 ms |
8972 KB |
Output is correct |
28 |
Correct |
139 ms |
9136 KB |
Output is correct |
29 |
Correct |
72 ms |
8628 KB |
Output is correct |
30 |
Correct |
75 ms |
8720 KB |
Output is correct |
31 |
Correct |
95 ms |
9780 KB |
Output is correct |
32 |
Correct |
58 ms |
2104 KB |
Output is correct |
33 |
Correct |
94 ms |
9696 KB |
Output is correct |
34 |
Correct |
310 ms |
22484 KB |
Output is correct |
35 |
Correct |
294 ms |
22332 KB |
Output is correct |
36 |
Correct |
251 ms |
21876 KB |
Output is correct |
37 |
Correct |
298 ms |
21944 KB |
Output is correct |
38 |
Correct |
272 ms |
21884 KB |
Output is correct |
39 |
Correct |
186 ms |
17292 KB |
Output is correct |
40 |
Correct |
170 ms |
17392 KB |
Output is correct |
41 |
Correct |
209 ms |
17428 KB |
Output is correct |
42 |
Correct |
204 ms |
17424 KB |
Output is correct |
43 |
Correct |
217 ms |
17496 KB |
Output is correct |
44 |
Correct |
225 ms |
17828 KB |
Output is correct |
45 |
Correct |
242 ms |
17948 KB |
Output is correct |
46 |
Correct |
251 ms |
17864 KB |
Output is correct |
47 |
Correct |
268 ms |
17916 KB |
Output is correct |
48 |
Correct |
218 ms |
17856 KB |
Output is correct |
49 |
Correct |
155 ms |
16816 KB |
Output is correct |
50 |
Correct |
165 ms |
16724 KB |
Output is correct |
51 |
Correct |
155 ms |
16820 KB |
Output is correct |
52 |
Correct |
159 ms |
16716 KB |
Output is correct |
53 |
Correct |
164 ms |
16816 KB |
Output is correct |
54 |
Correct |
254 ms |
18668 KB |
Output is correct |
55 |
Correct |
171 ms |
12716 KB |
Output is correct |
56 |
Correct |
238 ms |
19168 KB |
Output is correct |
57 |
Correct |
1882 ms |
108808 KB |
Output is correct |
58 |
Correct |
1730 ms |
108896 KB |
Output is correct |
59 |
Correct |
1604 ms |
106948 KB |
Output is correct |
60 |
Correct |
1601 ms |
106848 KB |
Output is correct |
61 |
Correct |
1604 ms |
107024 KB |
Output is correct |
62 |
Correct |
1622 ms |
106796 KB |
Output is correct |
63 |
Correct |
858 ms |
82692 KB |
Output is correct |
64 |
Correct |
839 ms |
82612 KB |
Output is correct |
65 |
Correct |
1251 ms |
86324 KB |
Output is correct |
66 |
Correct |
1298 ms |
86432 KB |
Output is correct |
67 |
Correct |
1294 ms |
86432 KB |
Output is correct |
68 |
Correct |
1482 ms |
88472 KB |
Output is correct |
69 |
Correct |
1406 ms |
88516 KB |
Output is correct |
70 |
Correct |
1343 ms |
87968 KB |
Output is correct |
71 |
Correct |
1347 ms |
87836 KB |
Output is correct |
72 |
Correct |
1362 ms |
87964 KB |
Output is correct |
73 |
Correct |
785 ms |
81676 KB |
Output is correct |
74 |
Correct |
771 ms |
81660 KB |
Output is correct |
75 |
Correct |
802 ms |
81676 KB |
Output is correct |
76 |
Correct |
793 ms |
81560 KB |
Output is correct |
77 |
Correct |
798 ms |
81948 KB |
Output is correct |
78 |
Correct |
1220 ms |
91248 KB |
Output is correct |
79 |
Correct |
860 ms |
53844 KB |
Output is correct |
80 |
Correct |
1480 ms |
90748 KB |
Output is correct |