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 FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define cbr cerr<<"hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define lbd(x,y) (lower_bound(all(x),y)-x.begin())
#define ubd(x,y) (upper_bound(all(x),y)-x.begin())
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
inline long long rand(long long x, long long y) { return rng() % (y+1-x) + x; } //inclusivesss
string inline to_string(char c) {string s(1,c);return s;} template<typename T> inline T gcd(T a,T b){ return a==0?llabs(b):gcd(b%a,a); }
using ll=long long;
using ld=long double;
#define FOR(i,s,e) for(ll i=s;i<=ll(e);++i)
#define DEC(i,s,e) for(ll i=s;i>=ll(e);--i)
using pi=pair<ll,ll>; using spi=pair<ll,pi>; using dpi=pair<pi,pi>;
#define LLINF ((long long)1e18)
#define INF int(1e9+1e6)
#define MAXN (10006)
struct node {
int s,e,m;
ll v,add;
node*l,*r;
node(int S,int E){
s=S,e=E,m=(s+e)>>1;
v=0, add=-LLINF;
if(s^e)l=new node(s,m),r=new node(m+1,e);
}
void update(int x,int y,ll nval) {
if(s==x&&e==y) {
add = max(add, nval);
return;
}
if(x>m) r->update(x,y,nval);
else if(y<=m) l->update(x,y,nval);
else l->update(x,m,nval),r->update(m+1,y,nval);
v = max(l->value(), r->value());
}
ll rmq(int x,int y) {
if(x>y) return 0;
value();
if(s==x&&e==y) return v;
if(x>m) return r->rmq(x,y);
else if(y<=m) return l->rmq(x,y);
else return max(l->rmq(x,m), r->rmq(m+1,y));
}
void set(int x,ll nval) {
value();
if(s==e) {
v = nval;
return;
}
if(x>m) r->set(x,nval);
else l->set(x,nval);
v=max(l->value(),r->value());
}
ll value() {
v = max(v, add);
if(s^e) {
l->add=max(l->add, add);
r->add=max(r->add, add);
}
add=-LLINF;
return v;
}
} *seg, *ans;
ll n,m,A[MAXN];
bool can[MAXN];
vector<ll> v;
vector<tuple<ll,ll,ll>> Q[MAXN];
int main() {
FAST
cin>>n>>m;
FOR(i,1,n)cin>>A[i];
seg=new node(1, n), ans = new node(1, n);
FOR(i,1,m) {
ll l,r,k;
cin>>l>>r>>k;
Q[r].pb({l, k, i});
}
auto add=[&](ll x){
while(v.size() && A[v.back()] <= A[x]) {
ans->set(v.back(), A[v.back()]+seg->rmq(siz(v),siz(v)));
seg->set(siz(v), 0);
v.pop_back();
}
v.eb(x);
};
FOR(r,1,n) {
add(r);
for(auto q:Q[r]) {
ll l=get<0>(q);
ll ind = lbd(v, l) + 1;
ll mx = max(ans->rmq(l, r), seg->rmq(ind, siz(v)));
if(mx <= get<1>(q)) can[get<2>(q)]=1;
}
}
FOR(i,1,m) cout<<can[i]<<'\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... |