#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
const int M = 4e6 + 7;
const long long inf = 1e18 + 7;
// bool debug_mode = 0;
struct MergeSortTree{
vector < int > tree[M];
void build(vector < int > &v , int ind=1 , int l=1 , int r=N){
for(int i = l-1;i<r;i++){
tree[ind].push_back(v[i]);
}
sort(tree[ind].begin() , tree[ind].end());
if(l != r){
int mid = (l+r) >> 1;
build(v , ind*2 , l , mid);
build(v , ind*2+1 , mid+1 , r);
}
}
inline int query(int ind , int val){
auto it = lower_bound(tree[ind].begin() , tree[ind].end() , val);
int res = -1;
if(it != tree[ind].begin()){
res = *(--it);
}
// if(debug_mode){
// cout << "v : ";for(auto itr : tree[ind])cout << itr << " ";cout << endl;
// cout << "val : " << val << endl;
// cout << "res : " << res << endl;
// }
return res;
}
} mst;
struct SegmentTree{
struct Node{
int mx , indx;
long long ans;
Node():mx(0) , ans(-inf) , indx(0){}
} tree[M];
Node merge(Node &a , Node &b){
Node c;
c.mx = max(a.mx , b.mx);
int tmp = mst.query(b.indx , a.mx);
if(tmp != -1){
c.ans = max({a.ans , b.ans , (long long)a.mx + (long long)tmp});
}
else {
c.ans = max(a.ans , b.ans);
}
return c;
}
void build(vector < int > &v , int ind=1 , int l=1 , int r=N){
for(int i = l-1;i<r;i++){
tree[ind].mx = max(tree[ind].mx , v[i]);
}
if(l != r){
int mid = (l+r) >> 1;
build(v , ind*2 , l , mid);
build(v , ind*2+1 , mid+1 , r);
tree[ind] = merge(tree[ind*2] , tree[ind*2+1]);
}
tree[ind].indx = ind;
// cout << l << " " << r << " => " << tree[ind].mx << " " << tree[ind].ans << endl;
}
vector < Node > vec;
void dfs(int ql , int qr , int ind=1 , int l=1 , int r=N){
if(l >= ql and r <= qr){
// cout << l << " " << r << " : " << tree[ind].mx << " " << tree[ind].ans << endl;
vec.push_back(tree[ind]);
}
else if(l > qr or r < ql){
return;
}
else{
int mid = (l+r) >> 1;
dfs(ql , qr , ind*2 , l , mid);
dfs(ql , qr , ind*2+1 , mid+1 , r);
}
}
long long query(int l , int r){
Node ret;
// debug_mode = 1;
for(auto itr : vec){
ret = merge(ret , itr);
// cout << ret.mx << " " << ret.ans << endl;
}
// debug_mode = 0;
vec.clear();
// cout << "query : " << ret.ans << endl;
return ret.ans;
}
} segt;
void solve(){
int n,m;
cin >> n >> m;
vector < int > arr(n);
for(int i = 0;i<n;i++){
cin >> arr[i];
}
mst.build(arr,1,1,arr.size());
segt.build(arr,1,1,arr.size());
// cout << "---" << endl;
for(int qt = 0;qt<m;qt++){
int l,r,k;
cin >> l >> r >> k;
segt.dfs(l,r,1,1,arr.size());
cout << (segt.query(l,r) <= k) << '\n';
// cout << "---" << endl;
}
}
signed main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int testcase = 1;//cin >> testcase;
while(testcase--)solve();
}
Compilation message
sortbooks.cpp: In constructor 'SegmentTree::Node::Node()':
sortbooks.cpp:37:19: warning: 'SegmentTree::Node::ans' will be initialized after [-Wreorder]
37 | long long ans;
| ^~~
sortbooks.cpp:36:18: warning: 'int SegmentTree::Node::indx' [-Wreorder]
36 | int mx , indx;
| ^~~~
sortbooks.cpp:38:9: warning: when initialized here [-Wreorder]
38 | Node():mx(0) , ans(-inf) , indx(0){}
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
156752 KB |
Output is correct |
2 |
Correct |
33 ms |
156752 KB |
Output is correct |
3 |
Correct |
31 ms |
156756 KB |
Output is correct |
4 |
Correct |
31 ms |
156752 KB |
Output is correct |
5 |
Correct |
30 ms |
157020 KB |
Output is correct |
6 |
Correct |
30 ms |
157016 KB |
Output is correct |
7 |
Correct |
31 ms |
157020 KB |
Output is correct |
8 |
Correct |
34 ms |
156968 KB |
Output is correct |
9 |
Correct |
30 ms |
156980 KB |
Output is correct |
10 |
Correct |
31 ms |
157020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
156752 KB |
Output is correct |
2 |
Correct |
33 ms |
156752 KB |
Output is correct |
3 |
Correct |
31 ms |
156756 KB |
Output is correct |
4 |
Correct |
31 ms |
156752 KB |
Output is correct |
5 |
Correct |
30 ms |
157020 KB |
Output is correct |
6 |
Correct |
30 ms |
157016 KB |
Output is correct |
7 |
Correct |
31 ms |
157020 KB |
Output is correct |
8 |
Correct |
34 ms |
156968 KB |
Output is correct |
9 |
Correct |
30 ms |
156980 KB |
Output is correct |
10 |
Correct |
31 ms |
157020 KB |
Output is correct |
11 |
Correct |
33 ms |
157020 KB |
Output is correct |
12 |
Correct |
35 ms |
157520 KB |
Output is correct |
13 |
Correct |
36 ms |
157524 KB |
Output is correct |
14 |
Correct |
37 ms |
157524 KB |
Output is correct |
15 |
Correct |
39 ms |
157520 KB |
Output is correct |
16 |
Correct |
35 ms |
157648 KB |
Output is correct |
17 |
Correct |
33 ms |
157276 KB |
Output is correct |
18 |
Correct |
35 ms |
157448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
867 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
244 ms |
170488 KB |
Output is correct |
2 |
Correct |
228 ms |
170776 KB |
Output is correct |
3 |
Correct |
177 ms |
170456 KB |
Output is correct |
4 |
Correct |
209 ms |
170444 KB |
Output is correct |
5 |
Correct |
178 ms |
170996 KB |
Output is correct |
6 |
Correct |
147 ms |
170540 KB |
Output is correct |
7 |
Correct |
141 ms |
170448 KB |
Output is correct |
8 |
Correct |
145 ms |
170628 KB |
Output is correct |
9 |
Correct |
62 ms |
157152 KB |
Output is correct |
10 |
Correct |
154 ms |
170540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
156752 KB |
Output is correct |
2 |
Correct |
33 ms |
156752 KB |
Output is correct |
3 |
Correct |
31 ms |
156756 KB |
Output is correct |
4 |
Correct |
31 ms |
156752 KB |
Output is correct |
5 |
Correct |
30 ms |
157020 KB |
Output is correct |
6 |
Correct |
30 ms |
157016 KB |
Output is correct |
7 |
Correct |
31 ms |
157020 KB |
Output is correct |
8 |
Correct |
34 ms |
156968 KB |
Output is correct |
9 |
Correct |
30 ms |
156980 KB |
Output is correct |
10 |
Correct |
31 ms |
157020 KB |
Output is correct |
11 |
Correct |
33 ms |
157020 KB |
Output is correct |
12 |
Correct |
35 ms |
157520 KB |
Output is correct |
13 |
Correct |
36 ms |
157524 KB |
Output is correct |
14 |
Correct |
37 ms |
157524 KB |
Output is correct |
15 |
Correct |
39 ms |
157520 KB |
Output is correct |
16 |
Correct |
35 ms |
157648 KB |
Output is correct |
17 |
Correct |
33 ms |
157276 KB |
Output is correct |
18 |
Correct |
35 ms |
157448 KB |
Output is correct |
19 |
Correct |
544 ms |
185260 KB |
Output is correct |
20 |
Correct |
530 ms |
185040 KB |
Output is correct |
21 |
Correct |
439 ms |
185204 KB |
Output is correct |
22 |
Correct |
468 ms |
185564 KB |
Output is correct |
23 |
Correct |
465 ms |
185036 KB |
Output is correct |
24 |
Correct |
306 ms |
185092 KB |
Output is correct |
25 |
Correct |
291 ms |
185036 KB |
Output is correct |
26 |
Correct |
376 ms |
185292 KB |
Output is correct |
27 |
Correct |
376 ms |
185116 KB |
Output is correct |
28 |
Correct |
387 ms |
185548 KB |
Output is correct |
29 |
Correct |
411 ms |
185040 KB |
Output is correct |
30 |
Correct |
381 ms |
185040 KB |
Output is correct |
31 |
Correct |
445 ms |
185164 KB |
Output is correct |
32 |
Correct |
434 ms |
185228 KB |
Output is correct |
33 |
Correct |
435 ms |
185552 KB |
Output is correct |
34 |
Correct |
296 ms |
185036 KB |
Output is correct |
35 |
Correct |
298 ms |
185320 KB |
Output is correct |
36 |
Correct |
309 ms |
185036 KB |
Output is correct |
37 |
Correct |
311 ms |
185072 KB |
Output is correct |
38 |
Correct |
324 ms |
185228 KB |
Output is correct |
39 |
Correct |
471 ms |
185216 KB |
Output is correct |
40 |
Correct |
250 ms |
173268 KB |
Output is correct |
41 |
Correct |
376 ms |
185184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
156752 KB |
Output is correct |
2 |
Correct |
33 ms |
156752 KB |
Output is correct |
3 |
Correct |
31 ms |
156756 KB |
Output is correct |
4 |
Correct |
31 ms |
156752 KB |
Output is correct |
5 |
Correct |
30 ms |
157020 KB |
Output is correct |
6 |
Correct |
30 ms |
157016 KB |
Output is correct |
7 |
Correct |
31 ms |
157020 KB |
Output is correct |
8 |
Correct |
34 ms |
156968 KB |
Output is correct |
9 |
Correct |
30 ms |
156980 KB |
Output is correct |
10 |
Correct |
31 ms |
157020 KB |
Output is correct |
11 |
Correct |
33 ms |
157020 KB |
Output is correct |
12 |
Correct |
35 ms |
157520 KB |
Output is correct |
13 |
Correct |
36 ms |
157524 KB |
Output is correct |
14 |
Correct |
37 ms |
157524 KB |
Output is correct |
15 |
Correct |
39 ms |
157520 KB |
Output is correct |
16 |
Correct |
35 ms |
157648 KB |
Output is correct |
17 |
Correct |
33 ms |
157276 KB |
Output is correct |
18 |
Correct |
35 ms |
157448 KB |
Output is correct |
19 |
Runtime error |
867 ms |
262144 KB |
Execution killed with signal 9 |
20 |
Halted |
0 ms |
0 KB |
- |