제출 #1336554

#제출 시각아이디문제언어결과실행 시간메모리
1336554justin271828Hedgehog Daniyar and Algorithms (IZhO19_sortbooks)C++20
0 / 100
1336 ms116780 KiB
#include <bits/stdc++.h>
using namespace std;

struct node {
    int s, e, m, val;
    node *l, *r;
    node (int S, int E) :
        s(S), e(E), m((S+E)/2), val(0) {
            if (s != e) {
                l = new node(s, m);
                r = new node(m+1, e);
            }
        }
    void up(int x, int k) {
        if (s == e) {
            val = k;
            return;
        }
        else if (x > m) r->up(x, k);
        else l->up(x, k);
        val = max(l->val, r->val);
    }
    int q(int x, int y) {
        if (x <= s && e <= y) return val;
        else if (x > m) return r->q(x, y);
        else if (y <= m) return l->q(x, y);
        else return max(l->val, r->val);
    }
}*tree;

#define ii pair<int, int>
#define tup pair<ii, ii>
#define f first
#define s second

bool comp(tup a, tup b) {
    if (a.f.s == b.f.s) return a.s.s < b.s.s;
    else return a.f.s < b.f.s;
}

int main() {
    int N, M;
    cin >> N >> M;
    int w[N];
    for (int i = 0; i < N; i++) cin >> w[i];
    tup q[M];
    for (int i = 0; i < M; i++) {
        cin >> q[i].f.f >> q[i].f.s >> q[i].s.f;
        q[i].f.f--;
        q[i].f.s--;
        q[i].s.s = i;
    }
    sort(q, q+M, comp);
    int index = 0;
    stack<ii> st;
    tree = new node(0, N-1);
    bool ans[M];
    for (int i = 0; i < N; i++) {
        if (index == M) break;
        while (!st.empty() && st.top().f <= w[i]) st.pop();
        if (!st.empty()) tree->up(st.top().s, st.top().f+w[i]);
        st.push({w[i], i});
        while (index < M && q[index].f.s == i) {
            tup p = q[index];
            ans[p.s.s] = (tree->q(p.f.f, p.f.s) <= p.s.f);
            index++;
        }
    }
    for (bool b: ans) cout << b << "\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...