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;
struct query{
int l,r,id,val;
};
int n,m,a[100005],ans[100005],cnt,pt,seg[400005];
stack <int> s;
pair <int,int> inv[100005];
query q[100005];
bool cmp(query a1,query a2){
return a1.r < a2.r;
}
bool invcmp(pair<int,int> a1,pair<int,int> a2){
return a1.second < a2.second;
}
void update(int id,int l,int r,int pos,int v){
if (l == r)
seg[id]=max(seg[id],v);
else
{
int mid=(l+r)/2;
if (pos <= mid)update(id*2,l,mid,pos,v);
else update(id*2+1,mid+1,r,pos,v);
seg[id]=max(seg[id*2],seg[id*2+1]);
}
}
int getans(int id,int l,int r,int x,int y){
if (x <= l && r <= y)
return seg[id];
if (x > r || y < l)
return -1;
int mid=(l+r)/2;
return max(getans(id*2,l,mid,x,y),getans(id*2+1,mid+1,r,x,y));
}
int main(){
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i=1;i<=n;i++){
cin >> a[i];
while (!s.empty() && a[i] >= s.top()) s.pop();
if (!s.empty()) inv[++cnt]=make_pair(s.top(),i);
s.push(i);
}
for (int i=1;i<=m;i++){
cin >> q[i].l >> q[i].r >> q[i].val;
q[i].id=i;
}
sort(inv+1,inv+1+cnt,invcmp); sort(q+1,q+1+m,cmp);
for (int i=1;i<=m;i++){
while (pt < cnt && inv[pt+1].second <= q[i].r) {
pt++;
update(1,1,n,inv[pt].first,a[inv[pt].first]+a[inv[pt].second]);
}
if (getans(1,1,n,q[i].l,q[i].r) <= q[i].val) ans[q[i].id]=1;
}
for (int i=1;i<=m;i++){
cout << ans[i] << "\n";
}
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... |