제출 #802475

#제출 시각아이디문제언어결과실행 시간메모리
802475Ahmed57Hedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++17
100 / 100
1604 ms67364 KiB
#include<bits/stdc++.h>

using namespace std;
struct query{
    int id,l,r,mood;
};
bool comp(query a, query b){
    return a.l>b.l;
}
//BIT Fenwick tree
int n ;
long long bit[1000001]={0};
void add(int e,long long v){
    while(e<=n){
        bit[e] = max(bit[e],v);
        e+=e&-e;
    }
}
long long sum(int e){
    long long res = 0;
    while(e>=1){
        res=max(res,bit[e]);
        e -= e&-e;
    }
    return res;
}
//end BIT
int main(){
	int q;
	cin>>n>>q;
	int arr[n+1];
    for(int i = 1;i<=n;i++)cin>>arr[i];
    vector<query> v;
    for(int i = 1;i<=q;i++){
        query tmp;
        cin>>tmp.l>>tmp.r>>tmp.mood;
        tmp.id = i;
        v.push_back(tmp);
    }
    sort(v.begin(),v.end(),comp);
    int now = n;
    stack<int> st;
    int ans[q+1];
    for(auto qu:v){
        while(now>=qu.l){
            while(st.size()&&arr[now]>arr[st.top()]){
                add(st.top(),arr[st.top()]+arr[now]);
                st.pop();
            }
            while(st.size()&&arr[now]==arr[st.top()])st.pop();
            st.push(now);
            now--;
        }
        ans[qu.id] = (sum(qu.r)<=qu.mood);
    }
    for(int i = 1;i<=q;i++){
        cout<<ans[i]<<"\n";
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...