#include <bits/stdc++.h>
#define N 1000005
using namespace std;
int a[N];
int ans[N];
struct node{
int val = 0,maxi = -1;
};
struct event{
int type,l,r,val,k;
bool operator <(event &other){
if(val == other.val){
return type < other.type;
}
return val > other.val;
}
};
vector<event> events;
struct SegTree{
vector<node> t;
int n;
int idx,k;
SegTree(int sz){
n = sz;
t.assign(4*n,{0,-1});
build(1,1,n);
}
node merge(node a,node b){
node ret;
ret.val = max(ret.val,b.val);
ret.maxi = max(a.maxi,b.maxi);
return ret;
}
void build(int v,int tl,int tr){
if(tl == tr){
t[v].maxi = a[tl];
return;
}
int tm = (tl + tr)/2;
build(v*2,tl,tm);
build(v*2+1,tm+1,tr);
t[v] = merge(t[v*2],t[v*2+1]);
for(int i = tm+1;i<=tr;i++){
if(a[i] < t[v*2].maxi){
t[v].val = max(t[v].val,t[v*2].maxi + a[i]);
}
}
}
node get(int v,int tl,int tr,int l,int r){
if(tr < l || r < tl){
return {0,-1};
}
if(l <= tl && tr <= r){
return t[v];
}
int tm = (tl + tr)/2;
node a = get(v*2,tl,tm,l,r);
node b = get(v*2+1,tm+1,tr,l,r);
events.push_back({idx,max(l,tm+1),min(r,tr),k + 1 - a.maxi,a.maxi});
return merge(a,b);
}
node get(int l,int r,int id,int kk){
idx = id;
k = kk;
return get(1,1,n,l,r);
}
};
struct SegTree2{
vector<int> t;
int n;
SegTree2(int sz){
n = sz;
t.assign(4*n,1e9);
}
void upd(int v,int tl,int tr,int pos,int val){
if(tl == tr){
t[v] = min(t[v],val);
return;
}
int tm = (tl + tr)/2;
if(pos <= tm){
upd(v*2,tl,tm,pos,val);
}
else upd(v*2+1,tm+1,tr,pos,val);
t[v] = min(t[v*2],t[v*2+1]);
}
int get(int v,int tl,int tr,int l,int r){
if(tr < l || r < tl){
return 1e9;
}
if(l <= tl && tr <= r){
return t[v];
}
int tm = (tl + tr)/2;
return min(get(v*2,tl,tm,l,r),get(v*2+1,tm+1,tr,l,r));
}
void upd(int pos,int val){
upd(1,1,n,pos,val);
}
int get(int l,int r){
return get(1,1,n,l,r);
}
};
void solve(){
int n,m;
cin >> n >> m;
for(int i = 1;i<=n;i++){
cin >> a[i];
events.push_back({0,i,i,a[i]});
}
SegTree t(n);
for(int i = 1;i<=m;i++){
int l,r,k;
cin >> l >> r >> k;
ans[i] = t.get(l,r,i,k).val <= k;
}
sort(events.begin(),events.end());
SegTree2 tt(n);
for(auto u:events){
if(u.type){
ans[u.type] &= tt.get(u.l,u.r) >= u.k;
}
else tt.upd(u.l,u.val);
}
for(int i = 1;i<=m;i++){
cout << ans[i] << '\n';
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t=1;
//cin>>t;
while(t--){
solve();
}
#ifdef Local
cout<<endl<<fixed<<setprecision(2)<<1000.0 * clock() / CLOCKS_PER_SEC<< " milliseconds ";
#endif
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
547 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
782 ms |
86424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |