Submission #896925

#TimeUsernameProblemLanguageResultExecution timeMemory
896925otariusHedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++17
100 / 100
1062 ms98676 KiB
#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <stack>
#include <iomanip>
using namespace std;

void __print(int x) { cerr << x; }

void __print(long x) { cerr << x; }

void __print(long long x) { cerr << x; }

void __print(unsigned x) { cerr << x; }

void __print(unsigned long x) { cerr << x; }

void __print(unsigned long long x) { cerr << x; }

void __print(float x) { cerr << x; }

void __print(double x) { cerr << x; }

void __print(long double x) { cerr << x; }

void __print(char x) { cerr << '\'' << x << '\''; }

void __print(const char *x) { cerr << '\"' << x << '\"'; }

void __print(const string &x) { cerr << '\"' << x << '\"'; }

void __print(bool x) { cerr << (x ? "true" : "false"); }

template<typename T, typename V>
void __print(const pair<T, V> &x) {
    cerr << '{';
    __print(x.first);
    cerr << ',';
    __print(x.second);
    cerr << '}';
}

template<typename T>
void __print(const T &x) {
    int f = 0;
    cerr << '{';
    for (auto &i: x) cerr << (f++ ? "," : ""), __print(i);
    cerr << "}";
}

void _print() { cerr << "]\n"; }

template<typename T, typename... V>
void _print(T t, V... v) {
    __print(t);
    if (sizeof...(v)) cerr << ", ";
    _print(v...);
}

#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair <int, int>
#define ull unsigned long long

// #define int long long
// #define int unsigned long long

const ll inf = 1e9 + 7;
const ll weirdMod = 998244353;

int t[4000005];
int arr[1000005], l, r, k;
vector<pair<int, pii>> q[1000005];
void update(int v, int tl, int tr, int pos, int val) {
    if (tl == tr) {
        t[v] = val;
        return;
    } int tm = (tl + tr) / 2;
    if (pos <= tm) update(2 * v, tl, tm, pos, val);
    else update(2 * v + 1, tm + 1, tr, pos, val);
    t[v] = max(t[2 * v], t[2 * v + 1]);
}
int getmax(int v, int tl, int tr, int l, int r) {
    if (l > r) return 0;
    if (tl == l && tr == r)
        return t[v];
    int tm = (tl + tr) / 2;
    return max(getmax(2 * v, tl, tm, l, min(r, tm)),
               getmax(2 * v + 1, tm + 1, tr, max(l, tm + 1), r));
}
void solve() {
    int n, m;
    cin >> n >> m;
    bool ans[m + 1] = {};
    for (int i = 1; i <= n; i++)
        cin >> arr[i];
    for (int i = 1; i <= m; i++) {
        cin >> l >> r >> k;
        q[r].pb({l, {k, i}});
    } int f[n + 1]; stack<int> st;
    for (int i = 1; i <= n; i++) {
        while (st.size() && arr[st.top()] <= arr[i]) st.pop();
        if (st.size() == 0) f[i] = 0;
        else f[i] = st.top();
        st.push(i);
    } for (int i = 1; i <= n; i++) {
        if (f[i] != 0) update(1, 1, n, f[i], arr[f[i]] + arr[i]);
        for (auto v : q[i]) {
            ans[v.sc.sc] = (getmax(1, 1, n, v.ff, i) <= v.sc.ff);
        }
    } for (int i = 1; i <= m; i++)
        cout << ans[i] << '\n';
}
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    
    int t = 1;
    // cin >> t;
    while (t--) {
        solve();
        cout << '\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...