Submission #152628

#TimeUsernameProblemLanguageResultExecution timeMemory
152628andrewHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++17
100 / 100
1744 ms151064 KiB

		
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
 
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
 
#define fi first
#define se second
#define p_b push_back
#define pll pair<ll,ll>
#define pii pair<int,int>
#define m_p make_pair
#define all(x) x.begin(),x.end()
#define sset ordered_set
#define sqr(x) (x)*(x)
#define pw(x) (1ll << x)
 
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
const ll MAXN = 1123456;
const ll N = 2e5;
mt19937_64 rnd(chrono::system_clock::now().time_since_epoch().count());
 
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
template <typename T> void vout(T s){cout << s << endl;exit(0);}
 
ll ans[MAXN];
 
vector <ll> v[MAXN];
 
struct qry{
    ll l, r, idx, k;
};
 
bool cmp(qry a, qry b){
    if(a.l > b.l)return 1;
    if(a.l < b.l)return 0;
    return (a.idx < b.idx);
}
 
ll t[4 * MAXN];
 
void modify(ll v, ll tl, ll tr, ll pos, ll val){
    if(tl == tr)t[v] = val; else{
        ll tm = (tl + tr) >> 1;
        if(pos <= tm)modify(v << 1, tl, tm, pos, val);
        else modify((v << 1) + 1, tm + 1, tr, pos, val);
        t[v] = max(t[v << 1], t[(v << 1) + 1]);
    }
}
 
ll get(ll v, ll tl, ll tr, ll l, ll r){
    if(l > r)return 0;
    if(tl == l && tr == r)return t[v];
    ll tm = (tl + tr) >> 1;
    return max(get(v << 1, tl, tm, l, min(r, tm)), get((v << 1) + 1, tm + 1, tr, max(l, tm + 1), r));
}
 
int main(){
    ios_base :: sync_with_stdio(0);
    cin.tie(0);
 
    ll n, m;
    cin >> n >> m;
 
    vector <ll> w(n + 1), le(n + 1);
 
    for(int i = 1; i <= n; i++)cin >> w[i];
 
    w[0] = 1e18;
 
    vector <ll> st;
    st.p_b(0);
 
    for(int i = 1; i <= n; i++){
        while(w[st.back()] <= w[i])st.pop_back();
        le[i] = st.back();
        v[le[i]].p_b(i);
        st.p_b(i);
    }
 
    vector <qry> c(m);
 
    ll uk = n;
 
    for(int i = 0; i < m; i++){
        cin >> c[i].l >> c[i].r >> c[i].k;
        c[i].idx = i;
    }
 
    sort(all(c), cmp);
 
    for(auto kek : c){
        while(kek.l < uk){
            for(auto j : v[uk - 1]){
                modify(1, 1, n, j, w[uk - 1] + w[j]);
            }
            uk--;
        }
        ans[kek.idx] = (get(1, 1, n, kek.l, kek.r) <= kek.k);
    }
 
    for(int i = 0; i < m; i++)cout << ans[i] << "\n";
 
    return 0;
}
#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...