#include <bits/stdc++.h>
#define noSuccess t--
#define int long long
#define pb push_back
#define F first
#define S second
#define Sattar_is_the_best ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define all(s) s.begin(),s.end()
#define yes "YES\n"
#define no "NO\n"
#define sz size
using namespace std;
const int maxn = 2e6;
const int mod = 1e9 + 7;
int n,q,a[maxn],p[maxn],t[maxn];
int l[maxn],r[maxn],k[maxn];
vector<pair<int,int>>Q[maxn];
vector<int>add[maxn];
void upd(int v,int tl,int tr,int pos,int val){
if(tl==tr){
t[v]=val;
return;
}
int mid = tl + tr >> 1;
if(pos<=mid) upd(v+v,tl,mid,pos,val);
else upd(v+v+1,mid+1,tr,pos,val);
t[v]=max(t[v+v],t[v+v+1]);
}
int get(int v,int tl,int tr,int l,int r){
if(tl>r || tr<l) return 0;
if(l<=tl && tr<=r) return t[v];
int mid = tl + tr >> 1;
return max(get(v+v,tl,mid,l,r),get(v+v+1,mid+1,tr,l,r));
}
int ans[maxn];
void tryAgain(){
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
}
for(int i=1;i<=q;i++){
cin>>l[i]>>r[i]>>k[i];
Q[l[i]].pb({r[i],i});
}
stack<int>st;
for(int i=1;i<=n;i++){
while(!st.empty() && a[st.top()]<=a[i]){
st.pop();
}
if(st.empty()) p[i]=0;
else p[i]=st.top();
// cout<<i<<' '<<p[i]<<'\n';
st.push(i);
add[p[i]].pb(i);
}
for(int L=n;L>=1;L--){
for(auto to : add[L]){
// cout<<L<<' '<<to<<' '<<a[L]<<' '<<a[to]<<' '<<a[L]+a[to]<<'\n';
upd(1,1,n,to,a[L]+a[to]);
// cout<<get(1,1,n,L,n)<<'\n';
}
for(auto to : Q[L]){
// cout<<to.S<<'\n';
// cout<<L<<' '<<to.F<<' '<<get(1,1,n,L,to.F)<<' '<<k[to.S]<<'\n';
ans[to.S]=(get(1,1,n,L,to.F)<=k[to.S] ? 1 : 0);
}
}
for(int i=1;i<=q;i++){
cout<<ans[i]<<'\n';
}
// cout<<get(1,1,n,1,n);
}
signed main(){
Sattar_is_the_best
// freopen("length.in", "r", stdin);
// freopen("length.out", "w", stdout);
int t = 1;
// cin >> t;
while(noSuccess){
tryAgain();
}
}
# | 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... |