This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
const ll Size=1e6+1;
class node{
public:
ll ans,largest;
};
ll w[Size];
node seg_tree[4*Size];
void build(ll l,ll r,ll i){
if(l==r){
seg_tree[i].ans=0;
seg_tree[i].largest=w[l];
}else{
ll mid=(l+r)/2;
build(l,mid,i*2);
build(mid+1,r,i*2+1);
ll max_v=seg_tree[i*2].largest,largest_swap=0;
for(ll j=mid+1;j<=r;j++){
if(w[j]<max_v and largest_swap<w[j]){
largest_swap=w[j];
}
}
seg_tree[i].largest=max(seg_tree[i*2].largest,seg_tree[i*2+1].largest);
if(!largest_swap){
seg_tree[i].ans=max(seg_tree[i*2].ans,seg_tree[i*2+1].ans);
}else{
seg_tree[i].ans=max(max(seg_tree[i*2].ans,seg_tree[i*2+1].ans),max_v+largest_swap);
}
}
}
node query(ll l,ll r,ll i,ll tl,ll tr){
if(l>tr or r<tl){
node a;
a.ans=-1;
return a;
}
if(l==r){
return seg_tree[i];
}else{
ll mid=(l+r)/2;
node n1=query(l,mid,i*2,tl,min(tr,mid));
node n2=query(mid+1,r,i*2+1,max(tl,mid+1),tr);
node res;
if(n1.ans==-1)return n2;
if(n2.ans==-1)return n1;
ll max_v=n1.largest,largest_swap=0;
for(ll j=mid+1;j<=r;j++){
if(w[j]<max_v and largest_swap<w[j]){
largest_swap=w[j];
}
}
res.largest=max(n1.largest,n2.largest);
if(!largest_swap){
res.ans=max(n1.ans,n2.ans);
}else{
res.ans=max(max(n1.ans,n2.ans),max_v+largest_swap);
}
return res;
}
}
void solve(){
ll n,m;
cin>>n>>m;
for(ll i=0;i<n;i++){
cin>>w[i];
}
build(0,n-1,1);
while(m--){
ll l,r,k;
cin>>l>>r>>k;
l--; r--;
node res=query(0,n-1,1,l,r);
cout<<(res.ans<=k)<<"\n";
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll t=1;
while(t--){
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |