#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
struct segment_tree{
vector<int> st;
segment_tree(int n) : st(n << 2) {}
void update(int id, int l, int r, int pos, int val){
if(l == r) {
st[id] = max(st[id], val);
return;
}
int mid = l + r >> 1;
if(pos <= mid) update(id << 1, l, mid, pos, val);
else update(id << 1 | 1, mid + 1, r, pos, val);
st[id] = max(st[id << 1], st[id << 1 | 1]);
}
int query(int id, int l, int r, int u, int v){
if(v < l || u > r) return 0;
if(u <= l && r <= v) return st[id];
int mid = l + r >> 1;
return max(query(id << 1, l, mid, u, v), query(id << 1 | 1, mid + 1, r, u, v));
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int N, M;
cin >> N >> M;
vector<int> a(N);
for(int i = 0; i < N; ++i){
cin >> a[i];
}
vector<vector<tuple<int, int, int>>> sweep(N);
for(int i = 0; i < M; ++i){
int l, r, k;
cin >> l >> r >> k;
--l, --r;
sweep[r].emplace_back(l, k, i);
}
vector<bool> answer(M, false);
segment_tree tree(N);
stack<int> st;
for(int i = 0; i < N; ++i){
while(!st.empty() && a[st.top()] <= a[i]) st.pop();
if(!st.empty()) tree.update(1, 0, N - 1, st.top(), a[st.top()] + a[i]);
for(auto [j, k, id] : sweep[i]){
answer[id] = (tree.query(1, 0, N - 1, j, i) <= k);
}
st.push(i);
}
for(auto it : answer) cout << it << '\n';
return 0;
}
Compilation message
sortbooks.cpp: In member function 'void segment_tree::update(int, int, int, int, int)':
sortbooks.cpp:16:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
16 | int mid = l + r >> 1;
| ~~^~~
sortbooks.cpp: In member function 'int segment_tree::query(int, int, int, int, int)':
sortbooks.cpp:25:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
25 | int mid = l + r >> 1;
| ~~^~~
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:58:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
58 | for(auto [j, k, id] : sweep[i]){
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
3 ms |
736 KB |
Output is correct |
13 |
Correct |
3 ms |
760 KB |
Output is correct |
14 |
Correct |
3 ms |
780 KB |
Output is correct |
15 |
Correct |
3 ms |
592 KB |
Output is correct |
16 |
Correct |
3 ms |
760 KB |
Output is correct |
17 |
Correct |
3 ms |
592 KB |
Output is correct |
18 |
Correct |
3 ms |
848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
936 ms |
67144 KB |
Output is correct |
2 |
Correct |
866 ms |
75848 KB |
Output is correct |
3 |
Correct |
828 ms |
67144 KB |
Output is correct |
4 |
Correct |
1029 ms |
84200 KB |
Output is correct |
5 |
Correct |
740 ms |
67176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
6984 KB |
Output is correct |
2 |
Correct |
55 ms |
8020 KB |
Output is correct |
3 |
Correct |
50 ms |
8264 KB |
Output is correct |
4 |
Correct |
52 ms |
6860 KB |
Output is correct |
5 |
Correct |
57 ms |
9052 KB |
Output is correct |
6 |
Correct |
45 ms |
8284 KB |
Output is correct |
7 |
Correct |
46 ms |
8324 KB |
Output is correct |
8 |
Correct |
46 ms |
6720 KB |
Output is correct |
9 |
Correct |
24 ms |
2056 KB |
Output is correct |
10 |
Correct |
52 ms |
8580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
3 ms |
736 KB |
Output is correct |
13 |
Correct |
3 ms |
760 KB |
Output is correct |
14 |
Correct |
3 ms |
780 KB |
Output is correct |
15 |
Correct |
3 ms |
592 KB |
Output is correct |
16 |
Correct |
3 ms |
760 KB |
Output is correct |
17 |
Correct |
3 ms |
592 KB |
Output is correct |
18 |
Correct |
3 ms |
848 KB |
Output is correct |
19 |
Correct |
151 ms |
17232 KB |
Output is correct |
20 |
Correct |
145 ms |
17224 KB |
Output is correct |
21 |
Correct |
117 ms |
13128 KB |
Output is correct |
22 |
Correct |
134 ms |
13056 KB |
Output is correct |
23 |
Correct |
153 ms |
19272 KB |
Output is correct |
24 |
Correct |
99 ms |
18200 KB |
Output is correct |
25 |
Correct |
116 ms |
18864 KB |
Output is correct |
26 |
Correct |
126 ms |
19784 KB |
Output is correct |
27 |
Correct |
127 ms |
19272 KB |
Output is correct |
28 |
Correct |
109 ms |
19348 KB |
Output is correct |
29 |
Correct |
116 ms |
19832 KB |
Output is correct |
30 |
Correct |
118 ms |
13640 KB |
Output is correct |
31 |
Correct |
123 ms |
19024 KB |
Output is correct |
32 |
Correct |
123 ms |
19784 KB |
Output is correct |
33 |
Correct |
132 ms |
20256 KB |
Output is correct |
34 |
Correct |
93 ms |
12872 KB |
Output is correct |
35 |
Correct |
109 ms |
18916 KB |
Output is correct |
36 |
Correct |
99 ms |
18776 KB |
Output is correct |
37 |
Correct |
103 ms |
18632 KB |
Output is correct |
38 |
Correct |
95 ms |
18864 KB |
Output is correct |
39 |
Correct |
113 ms |
18436 KB |
Output is correct |
40 |
Correct |
78 ms |
9820 KB |
Output is correct |
41 |
Correct |
101 ms |
18172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
3 ms |
736 KB |
Output is correct |
13 |
Correct |
3 ms |
760 KB |
Output is correct |
14 |
Correct |
3 ms |
780 KB |
Output is correct |
15 |
Correct |
3 ms |
592 KB |
Output is correct |
16 |
Correct |
3 ms |
760 KB |
Output is correct |
17 |
Correct |
3 ms |
592 KB |
Output is correct |
18 |
Correct |
3 ms |
848 KB |
Output is correct |
19 |
Correct |
936 ms |
67144 KB |
Output is correct |
20 |
Correct |
866 ms |
75848 KB |
Output is correct |
21 |
Correct |
828 ms |
67144 KB |
Output is correct |
22 |
Correct |
1029 ms |
84200 KB |
Output is correct |
23 |
Correct |
740 ms |
67176 KB |
Output is correct |
24 |
Correct |
55 ms |
6984 KB |
Output is correct |
25 |
Correct |
55 ms |
8020 KB |
Output is correct |
26 |
Correct |
50 ms |
8264 KB |
Output is correct |
27 |
Correct |
52 ms |
6860 KB |
Output is correct |
28 |
Correct |
57 ms |
9052 KB |
Output is correct |
29 |
Correct |
45 ms |
8284 KB |
Output is correct |
30 |
Correct |
46 ms |
8324 KB |
Output is correct |
31 |
Correct |
46 ms |
6720 KB |
Output is correct |
32 |
Correct |
24 ms |
2056 KB |
Output is correct |
33 |
Correct |
52 ms |
8580 KB |
Output is correct |
34 |
Correct |
151 ms |
17232 KB |
Output is correct |
35 |
Correct |
145 ms |
17224 KB |
Output is correct |
36 |
Correct |
117 ms |
13128 KB |
Output is correct |
37 |
Correct |
134 ms |
13056 KB |
Output is correct |
38 |
Correct |
153 ms |
19272 KB |
Output is correct |
39 |
Correct |
99 ms |
18200 KB |
Output is correct |
40 |
Correct |
116 ms |
18864 KB |
Output is correct |
41 |
Correct |
126 ms |
19784 KB |
Output is correct |
42 |
Correct |
127 ms |
19272 KB |
Output is correct |
43 |
Correct |
109 ms |
19348 KB |
Output is correct |
44 |
Correct |
116 ms |
19832 KB |
Output is correct |
45 |
Correct |
118 ms |
13640 KB |
Output is correct |
46 |
Correct |
123 ms |
19024 KB |
Output is correct |
47 |
Correct |
123 ms |
19784 KB |
Output is correct |
48 |
Correct |
132 ms |
20256 KB |
Output is correct |
49 |
Correct |
93 ms |
12872 KB |
Output is correct |
50 |
Correct |
109 ms |
18916 KB |
Output is correct |
51 |
Correct |
99 ms |
18776 KB |
Output is correct |
52 |
Correct |
103 ms |
18632 KB |
Output is correct |
53 |
Correct |
95 ms |
18864 KB |
Output is correct |
54 |
Correct |
113 ms |
18436 KB |
Output is correct |
55 |
Correct |
78 ms |
9820 KB |
Output is correct |
56 |
Correct |
101 ms |
18172 KB |
Output is correct |
57 |
Correct |
885 ms |
99400 KB |
Output is correct |
58 |
Correct |
934 ms |
83716 KB |
Output is correct |
59 |
Correct |
764 ms |
82108 KB |
Output is correct |
60 |
Correct |
783 ms |
82124 KB |
Output is correct |
61 |
Correct |
784 ms |
65272 KB |
Output is correct |
62 |
Correct |
767 ms |
97248 KB |
Output is correct |
63 |
Correct |
501 ms |
94024 KB |
Output is correct |
64 |
Correct |
548 ms |
88576 KB |
Output is correct |
65 |
Correct |
759 ms |
94972 KB |
Output is correct |
66 |
Correct |
688 ms |
72716 KB |
Output is correct |
67 |
Correct |
699 ms |
96876 KB |
Output is correct |
68 |
Correct |
869 ms |
98528 KB |
Output is correct |
69 |
Correct |
726 ms |
83640 KB |
Output is correct |
70 |
Correct |
802 ms |
83328 KB |
Output is correct |
71 |
Correct |
747 ms |
83328 KB |
Output is correct |
72 |
Correct |
806 ms |
83428 KB |
Output is correct |
73 |
Correct |
513 ms |
76632 KB |
Output is correct |
74 |
Correct |
544 ms |
89344 KB |
Output is correct |
75 |
Correct |
507 ms |
76616 KB |
Output is correct |
76 |
Correct |
490 ms |
88392 KB |
Output is correct |
77 |
Correct |
526 ms |
88068 KB |
Output is correct |
78 |
Correct |
664 ms |
78700 KB |
Output is correct |
79 |
Correct |
513 ms |
53996 KB |
Output is correct |
80 |
Correct |
714 ms |
81636 KB |
Output is correct |