#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;
const int mod = 1e9 + 7;
const int N = 2e5 + 5;
struct node{
int mx, calc;
node(){
mx = calc = 0;
}
};
node merge(node a, node b, vector<int> &arr){
node res;
res.mx = max(a.mx, b.mx);
res.calc = max(a.calc, b.calc);
if(arr[0] < a.mx){
auto x = lower_bound(arr.begin(), arr.end(), a.mx) - arr.begin();
x--;
res.calc = max(res.calc, arr[x] + a.mx);
}
return res;
}
struct SEGT{
vector<node> tree;
vector<vector<int> > list;
int S;
void init(int n){
tree.assign(4*n, node());
list.resize(4*n);
S = n;
}
void build(int ind, int l, int r, vector<int> &a){
if(l == r){
list[ind].pb(a[l]);
tree[ind].mx = a[l];
}
else{
int m = (l + r)/2;
build(ind*2, l, m, a);
build(ind*2+1, m+1, r, a);
int il = 0, ir = 0;
while(il < list[ind*2].size() && ir < list[ind*2+1].size()){
if(list[ind*2][il] <= list[ind*2+1][ir]){
list[ind].pb(list[ind*2][il]);
il++;
}
else{
list[ind].pb(list[ind*2+1][ir]);
ir++;
}
}
while(il < list[ind*2].size()){
list[ind].pb(list[ind*2][il]);
il++;
}
while(ir < list[ind*2+1].size()){
list[ind].pb(list[ind*2+1][ir]);
ir++;
}
tree[ind] = merge(tree[ind*2], tree[ind*2+1], list[ind*2+1]);
}
}
int querylist(int ind, int l, int r, int ql, int qr, int k){
if(l > r || l > qr || r < ql) return 0;
if(l >= ql && r <= qr){
if(list[ind][0] >= k) return 0;
auto x = lower_bound(list[ind].begin(), list[ind].end(), k) - list[ind].begin();
x--;
return list[ind][x];
}
else{
int m = (l + r)/2;
return max(querylist(ind*2, l, m, ql, qr, k), querylist(ind*2+1, m+1, r, ql, qr, k));
}
}
int query(int ind, int l, int r, int ql, int qr){
if(l > r || l > qr || r < ql) return 0;
if(l >= ql && r <= qr){
int gh = querylist(1, 1, S, r, qr, tree[ind].mx);
int val = tree[ind].calc;
if(gh != 0 && tree[ind].mx > gh){
val = max(val, tree[ind].mx + gh);
}
return val;
}
else{
int m = (l + r)/2;
return max(query(ind*2, l, m, ql, qr), query(ind*2+1, m+1, r, ql, qr));
}
}
};
int32_t main(){
cin.tie(0); cout.tie(0);
ios_base::sync_with_stdio(false);
int n, q;
cin>>n>>q;
vector<int> a(n+1);
for(int i = 1; i <= n; i++){
cin>>a[i];
}
SEGT seg;
seg.init(n);
seg.build(1, 1, n, a);
while(q--){
int l, r, k;
cin>>l>>r>>k;
int val = seg.query(1, 1, n, l, r);
if(val <= k) cout<<1<<endl;
else cout<<0<<endl;
}
return 0;
}
Compilation message
sortbooks.cpp: In member function 'void SEGT::build(int, int, int, std::vector<int>&)':
sortbooks.cpp:52:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | while(il < list[ind*2].size() && ir < list[ind*2+1].size()){
| ~~~^~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:52:49: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | while(il < list[ind*2].size() && ir < list[ind*2+1].size()){
| ~~~^~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:62:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | while(il < list[ind*2].size()){
| ~~~^~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:66:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | while(ir < list[ind*2+1].size()){
| ~~~^~~~~~~~~~~~~~~~~~~~~~
# |
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 |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
500 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 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 |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
500 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
14 ms |
860 KB |
Output is correct |
12 |
Correct |
14 ms |
1628 KB |
Output is correct |
13 |
Correct |
16 ms |
1628 KB |
Output is correct |
14 |
Correct |
25 ms |
1884 KB |
Output is correct |
15 |
Correct |
28 ms |
1884 KB |
Output is correct |
16 |
Correct |
20 ms |
1628 KB |
Output is correct |
17 |
Correct |
13 ms |
1368 KB |
Output is correct |
18 |
Correct |
16 ms |
1624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
421 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1025 ms |
28108 KB |
Output is correct |
2 |
Correct |
1089 ms |
28140 KB |
Output is correct |
3 |
Correct |
709 ms |
28040 KB |
Output is correct |
4 |
Correct |
623 ms |
27984 KB |
Output is correct |
5 |
Correct |
551 ms |
28032 KB |
Output is correct |
6 |
Correct |
620 ms |
28012 KB |
Output is correct |
7 |
Correct |
612 ms |
28040 KB |
Output is correct |
8 |
Correct |
538 ms |
27740 KB |
Output is correct |
9 |
Correct |
175 ms |
1616 KB |
Output is correct |
10 |
Correct |
501 ms |
27888 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 |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
500 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
14 ms |
860 KB |
Output is correct |
12 |
Correct |
14 ms |
1628 KB |
Output is correct |
13 |
Correct |
16 ms |
1628 KB |
Output is correct |
14 |
Correct |
25 ms |
1884 KB |
Output is correct |
15 |
Correct |
28 ms |
1884 KB |
Output is correct |
16 |
Correct |
20 ms |
1628 KB |
Output is correct |
17 |
Correct |
13 ms |
1368 KB |
Output is correct |
18 |
Correct |
16 ms |
1624 KB |
Output is correct |
19 |
Correct |
2702 ms |
57916 KB |
Output is correct |
20 |
Correct |
2545 ms |
60372 KB |
Output is correct |
21 |
Correct |
2704 ms |
60476 KB |
Output is correct |
22 |
Correct |
2661 ms |
60544 KB |
Output is correct |
23 |
Correct |
2689 ms |
60356 KB |
Output is correct |
24 |
Correct |
1293 ms |
60048 KB |
Output is correct |
25 |
Correct |
1291 ms |
60000 KB |
Output is correct |
26 |
Correct |
1430 ms |
60636 KB |
Output is correct |
27 |
Correct |
1456 ms |
60648 KB |
Output is correct |
28 |
Correct |
1658 ms |
60184 KB |
Output is correct |
29 |
Correct |
1461 ms |
60488 KB |
Output is correct |
30 |
Correct |
1473 ms |
60348 KB |
Output is correct |
31 |
Correct |
1513 ms |
60280 KB |
Output is correct |
32 |
Correct |
1445 ms |
60360 KB |
Output is correct |
33 |
Correct |
1461 ms |
60376 KB |
Output is correct |
34 |
Correct |
1371 ms |
60132 KB |
Output is correct |
35 |
Correct |
1348 ms |
59948 KB |
Output is correct |
36 |
Correct |
1437 ms |
59672 KB |
Output is correct |
37 |
Correct |
1370 ms |
59744 KB |
Output is correct |
38 |
Correct |
1351 ms |
60000 KB |
Output is correct |
39 |
Correct |
1379 ms |
58824 KB |
Output is correct |
40 |
Correct |
818 ms |
37836 KB |
Output is correct |
41 |
Correct |
1154 ms |
58468 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 |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
500 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
2 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
14 ms |
860 KB |
Output is correct |
12 |
Correct |
14 ms |
1628 KB |
Output is correct |
13 |
Correct |
16 ms |
1628 KB |
Output is correct |
14 |
Correct |
25 ms |
1884 KB |
Output is correct |
15 |
Correct |
28 ms |
1884 KB |
Output is correct |
16 |
Correct |
20 ms |
1628 KB |
Output is correct |
17 |
Correct |
13 ms |
1368 KB |
Output is correct |
18 |
Correct |
16 ms |
1624 KB |
Output is correct |
19 |
Runtime error |
421 ms |
262144 KB |
Execution killed with signal 9 |
20 |
Halted |
0 ms |
0 KB |
- |