Submission #396802

#TimeUsernameProblemLanguageResultExecution timeMemory
396802SavicSHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++14
100 / 100
2534 ms104924 KiB
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>

#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)

using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 1000005;
const int inf = 1e9 + 5;

template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k)  print the k-th smallest number in os(0-based)

int n, q;
int niz[maxn];

vector<array<int,3>> upit[maxn];

int res[maxn];

ll bor[4 * maxn];
ll lenj[4 * maxn];
void propagate(int v, int tl, int tr) {
    if(lenj[v]) {
        bor[v] += lenj[v];
        if(tl != tr) {
            lenj[v * 2] += lenj[v];
            lenj[v * 2 + 1] += lenj[v];
        }
        lenj[v] = 0;
    }
}

void lazyupd(int v, int tl, int tr, int l, int r, ll val) {
    propagate(v, tl, tr);
    if(tl > tr || l > tr || tl > r)return;
    if(tl >= l && tr <= r) {
        bor[v] += val;
        if(tl != tr) {
            lenj[v * 2] += val;
            lenj[v * 2 + 1] += val;
        }
        return;
    }
    int mid = (tl + tr) / 2;
    lazyupd(v * 2, tl, mid, l, r, val);
    lazyupd(v * 2 + 1, mid + 1, tr, l, r, val);
    bor[v] = max(bor[v * 2], bor[v * 2 + 1]);
}

ll kveri(int v, int tl, int tr, int l, int r) {
    propagate(v, tl, tr);
    if(tl > r || l > tr)return 0;
    if(tl >= l && tr <= r)return bor[v];
    int mid = (tl + tr) / 2;
    return max(kveri(v * 2, tl, mid, l, r), kveri(v * 2 + 1, mid + 1, tr, l, r));
}

int main() 
{
    ios::sync_with_stdio(false);
    cout.tie(nullptr);
    cin.tie(nullptr);
    cin >> n >> q;
    ff(i,1,n)cin >> niz[i];
    ff(i,1,q) {
        int L, R, K;
        cin >> L >> R >> K;
        upit[L].pb({R, K, i});
    }

    stack<int> stek;
    fb(i,n,1) {

        while(!stek.empty() && niz[stek.top()] < niz[i]) {
            int vrh = stek.top();
            stek.pop();

            lazyupd(1,1,n,vrh + 1, (sz(stek) == 0 ? n : stek.top() - 1), niz[i] - niz[vrh]);
            lazyupd(1,1,n,vrh,vrh,niz[i] + niz[vrh]);

        }
        stek.push(i);

        for(auto c : upit[i]) {
            int R = c[0];
            int K = c[1];
            int id = c[2];
            res[id] = (kveri(1,1,n,i,R) <= K);
        }


    }

    ff(i,1,q)cout << res[i] << '\n';

    return 0;
}
/**

5 2
3 5 1 8 2
1 3 6
2 5 3

// probati bojenje sahovski ili slicno

**/


#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...