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 all(x) begin(x),end(x)
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#define debug(a) cerr << "(" << #a << ": " << a << ")\n";
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int mxN = 1e5+1, oo = 1e9;
struct segtree {
struct node {
int mx=0, lazy=0;
node() {}
node(int val) {
mx=val;
}
};
vector<node> seg;
int n,ptwo;
segtree(int nn) {
n=nn,ptwo=1;
while(ptwo<n) ptwo*=2;
seg.resize(2*ptwo);
}
void updateNode(int i, int val) {
auto& v = seg[i];
v.mx+=val,v.lazy+=val;
}
void pull(int i) {
auto& v = seg[i];
v.mx = max(seg[i*2].mx,seg[i*2+1].mx);
}
void add(int i, int l, int r, int ql, int qr, int val) {
if(qr<l or r<ql) return;
if(ql<=l and r<=qr) {
updateNode(i,val);
return;
}
int mid = (l+r)/2;
push(i);
add(i*2,l,mid,ql,qr,val),add(i*2+1,mid+1,r,ql,qr,val);
pull(i);
}
void push(int i) {
updateNode(i*2,seg[i].lazy);
updateNode(i*2+1,seg[i].lazy);
seg[i].lazy=0;
}
ll get(int i, int l, int r, int ql, int qr) {
if(qr<l or r<ql) return 0;
if(ql<=l and r<=qr) {
return seg[i].mx;
}
int mid = (l+r)/2;
push(i);
auto sum = max(get(i*2,l,mid,ql,qr),get(i*2+1,mid+1,r,ql,qr));
pull(i);
return sum;
}
node& operator[](int i) {return seg[i+ptwo];}
void build() {
for(int i=ptwo-1;i>=1;--i) {
pull(i);
}
}
};
int main() {
int n,m; cin >> n >> m;
struct query {
int l,k,id;
};
vector<vector<query>> queries(n);
vi w(n); for(auto& i : w) cin >> i;
segtree seg(n);
for(int i=0;i<m;++i) {
int l,r,k; cin >> l >> r >> k,--l,--r;
queries[r].push_back({l,k,i});
}
vi prv(n,-1), from(n,0),ans(m,-1);
int active=-1;
for(int i=0;i<n;++i) {
prv[i]=i-1;
while(prv[i]!=-1 and w[prv[i]]<=w[i]) prv[i] = prv[prv[i]];
while(active<prv[i]) {
++active;
seg.add(1,0,seg.ptwo-1,active,active,w[active]);
}
if(prv[i]!=-1) {
int& before = from[prv[i]];
if(w[i]>before) {
seg.add(1,0,seg.ptwo-1,prv[prv[i]]+1, prv[i], w[i]-before);
before=w[i];
}
}
for(auto& q : queries[i]) {
ans[q.id]=seg.get(1,0,seg.ptwo-1,q.l, i)<=q.k;
}
}
for(auto i : ans) {
cout << 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... |