#include <bits/stdc++.h>
using namespace std;
#define getar(ar,n) for(int i=0;i<n;++i) cin>>ar[i]
#define show(n) cout<<n<<'\n'
#define all(v) v.begin(), v.end()
#define br cout<<"\n"
#define pb push_back
#define nl '\n'
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define ret return
#define ld long double
#define sza(x) ((int)x.size())
const int mxN = 1e6 + 10;
struct Node{
vector<int> vec;
int mx=0,bad_mx=0;
};
vector<int> ar;
vector<Node> seg;
int mx=0;
void build(int node,int start,int end){
if(start==end){
seg[node].vec={ar[start]};
seg[node].mx=ar[start];
seg[node].bad_mx=0;
return;
}
int mid=start+(end-start)/2;
build(node*2+1,start,mid);
build(node*2+2,mid+1,end);
seg[node].vec.assign(seg[node*2+1].vec.size()+seg[node*2+2].vec.size(),0);
merge(all(seg[node*2+1].vec),all(seg[node*2+2].vec),seg[node].vec.begin());
seg[node].mx=max(seg[node*2+1].mx,seg[node*2+2].mx);
int mxx=0;
for(int i=start;i<=end;++i){
if(mxx>ar[i]){
seg[node].bad_mx=max(seg[node].bad_mx,mxx+ar[i]);
}else{
mxx=ar[i];
}
}
}
bool qry(int nd,int start,int end,int l,int r,int k){
if(start>r||end<l) return 1;
if(start>=l&&end<=r){
bool f=(max(seg[nd].bad_mx,(mx>seg[nd].vec[0]?mx+*--lower_bound(all(seg[nd].vec),mx):0))<=k);
mx=max(mx,seg[nd].mx);
return f;
}
int mid=start+(end-start)/2;
return ((!qry(nd*2+1,start,mid,l,r,k)||!qry(nd*2+2,mid+1,end,l,r,k))?0:1);
}
void solve(){
int n,l,r,w,q;cin>>n>>q;
ar.resize(n+1);
for(int i=0;i<n;++i){
cin>>ar[i];
}
seg.resize((n+10)*4);
build(0,0,n-1);
while(q--){
cin>>l>>r>>w;
mx=0;
cout<<(qry(0,0,n-1,l-1,r-1,w))<<nl;
}
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
solve();
return 0;
}
Compilation message
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:82:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | freopen("input.in","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
sortbooks.cpp:83:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen("output.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
604 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2 ms |
600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |