Submission #1336243

#TimeUsernameProblemLanguageResultExecution timeMemory
1336243yhkhooHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++17
100 / 100
765 ms33852 KiB
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1000005;

struct Query{
    int l, r, k, i;
} q[MAXN];
bool operator< (Query &a, Query &b){
    return a.r < b.r;
}
int n, m, w[MAXN], d[MAXN];
bitset<MAXN> ans;

#define lsb(x) ((x) & (-(x)))
struct Fenwick{ // 0-index suffix max
    int t[MAXN];

    Fenwick(){
        memset(t, -1, sizeof(t));
    }
    
    void update(int X, int V){
        X = n-X;
        for(; X <= n; X += lsb(X)){
            t[X] = max(t[X], V);
        }
    }

    int query(int X){
        X = n-X;
        int ret = 0;
        for(; X; X -= lsb(X)){
            ret = max(ret, t[X]);
        }
        return ret;
    }

    int bs(int V){ // find last > V
        int pos = 0;
        for(int k=19; k>=0; k--){
            int npos = pos + (1<<k);
            if(npos <= n){
                if(t[npos] <= V){
                    pos = npos;
                }
            }
        }
        return n - (pos+1);
    }
} fw1, fw2;

signed main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m;
    for(int i=0; i<n; i++){
        cin >> w[i];
        d[i] = w[i];
    }
    sort(d, d+n);
    for(int i=0; i<n; i++){
        w[i] = lower_bound(d, d+n, w[i]) - d;
    }
    for(int i=0; i<m; i++){
        int l, r, k;
        cin >> l >> r >> k;
        l--; r--;
        q[i] = {l, r, k, i};
    }
    sort(q, q+m);
    auto qp = q;
    for(int i=0; i<n; i++){
        int evil = fw1.bs(w[i]);
        if(evil != -1){
            int tw = d[w[i]] + d[w[evil]];
            fw2.update(evil, tw);
        }
        fw1.update(i, w[i]);
        while(qp != q+m && qp->r == i){
            ans[qp->i] = fw2.query(qp->l) <= qp->k;
            qp++;
        }
    }
    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...