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;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
typedef pair<int,int> ii;
#define REP(i, a, b) for(int i = a; i < b; i++)
struct query{int x, y, k, idx;};
bool comp(query a, query b) {return a.y<b.y;}
struct node{
int s,e,m,v;
node *l,*r;
node(int _s, int _e) {
s=_s;e=_e;m=(s+e)>>1;v=-1210201069;
if(s!=e){
l=new node(s,m);
r=new node(m+1,e);
}
}
void up(int x, int u) {
if(s==e) {v=max(v,u);return;}
if(x<=m) l->up(x,u);
else r->up(x,u);
v=max(l->v,r->v);
}
int qry(int x, int y) {
if(s==x&&e==y) return v;
if(y<=m) return l->qry(x,y);
if(x>m) return r->qry(x,y);
return max(l->qry(x,m),r->qry(m+1,y));
}
}*root;
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int N, M; cin >> N >> M;
root=new node(0,N-1);
int W[N];REP(i,0,N)cin>>W[i];
query q[M];
REP(i,0,M){
cin>>q[i].x>>q[i].y>>q[i].k;
q[i].x--;q[i].y--;q[i].idx=i;
}
sort(q,q+M,comp);
vector<ii> mono_stack;
bool ans[M];
REP(i,0,M) {
REP(j,(i?(q[i-1].y+1):0),q[i].y+1) {
auto it=lower_bound(mono_stack.begin(),mono_stack.end(),mp(W[j],1e9),greater<ii>());
if(it!=mono_stack.begin()) {
it--; root->up((*it).se,(*it).fi+W[j]);
}
while(mono_stack.size()&&mono_stack.back().fi<=W[j]) mono_stack.pop_back();
mono_stack.pb(mp(W[j],j));
}
ans[q[i].idx]=root->qry(q[i].x,N-1)<=q[i].k;
}
REP(i,0,M) cout << (ans[i]?1:0) << "\n";
}
# | 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... |