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;
const int N=1e6+5;
int n,m, tree[4*N], a[N];
struct book{
int x, mood, ind;
};
struct books {
int x, y;
int sum;
bool operator<(books const& other) {
return y < other.y;
}
};
int ans[N];
vector<book> q[N];
vector<books> v;
stack<int> st;
void update(int i, int x, int y, int k, int val){
if(y<k || x>k) return;
if(k>=x && k<=y) {
tree[i]=max(tree[i], val);
if(x==y) return;
}
int mid=(x+y)/2;
update(2*i+1, x, mid, k, val);
update(2*i+2, mid+1, y, k,val);
}
int getmax(int i, int x, int y, int l , int r){
if(r<x || l>y) return 0;
if(x>=l && y<=r) return tree[i];
int mid=(x+y)/2;
if(x!=y){
return max(getmax(2*i+1,x, mid , l, r), getmax(2*i+2, mid+1, y, l, r));
}
return 0;
}
main(){
cin>>n>>m;
for(int i=0; i<n; i++){
cin>>a[i];
}
for(int i=0; i<m; i++){
int l, r, k;
cin>>l>>r>>k;
l--; r--;
q[r].push_back({l,k,i});
}
for(int i=n-1; i>=0; i--){
while(st.size()!=0 && a[st.top()]<a[i]){
v.push_back({i, st.top(), a[i]+a[st.top()] });
st.pop();
}
st.push(i);
}
v.push_back({n+1, n+1, n+1});
sort(v.begin(), v.end());
for(int j=0; j<min(n,v[0].y); j++){
for(auto k: q[j]){
int mx=getmax(0,0,n-1,k.x, j);
//cout<<i<<" "<<k.x<<" "<<k.mood<<" "<<k.ind<<" "<<mx<<endl;
if(mx<=k.mood) ans[k.ind]=1;
else ans[k.ind]=0;
}
}
for(int i=0; i<v.size()-1; i++){
update(0,0,n-1,v[i].x, v[i].sum);
for(int j=v[i].y; j<min(n,v[i+1].y); j++){
for(auto k: q[j]){
int mx=getmax(0,0,n-1,k.x, j);
//cout<<i<<" "<<k.x<<" "<<k.mood<<" "<<k.ind<<" "<<mx<<endl;
if(mx<=k.mood) ans[k.ind]=1;
else ans[k.ind]=0;
}
}
}
for(int i=0; i<m; i++){
cout<<ans[i]<<endl;
}
}
Compilation message (stderr)
sortbooks.cpp:39:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
39 | main(){
| ^
sortbooks.cpp: In function 'int main()':
sortbooks.cpp:73:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<books>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i=0; i<v.size()-1; i++){
| ~^~~~~~~~~~~
# | 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... |