답안 #1087794

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1087794 2024-09-13T08:45:10 Z PanosPask Hedgehog Daniyar and Algorithms (IZhO19_sortbooks) C++14
0 / 100
408 ms 73888 KB
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair

using namespace std;

typedef pair<int, int> pi;

const int INF = 1e9 + 1;

struct SegTree {
    int size;
    vector<int> tree;

    void init(int N) {
        size = 1;
        while (size < N) {
            size *= 2;
        }

        tree.assign(2 * size, 0);
    }

    void set(int i, int v, int x, int lx, int rx) {
        if (lx == rx - 1) {
            tree[x] = v;
            return;
        }

        int mid = (lx + rx) / 2;
        if (i < mid) {
            set(i, v, 2 * x + 1, lx, mid);
        }
        else {
            set(i, v, 2 * x + 2, mid, rx);
        }

        tree[x] = max(tree[2 * x + 1], tree[2 * x + 2]);
    }
    void set(int i, int v) {
        set(i, v, 0, 0, size);
    }

    int calc(int l, int r, int x, int lx, int rx) {
        if (lx >= r || rx >= l) {
            return 0;
        }
        else if (l <= lx && rx <= r) {
            return tree[x];
        }

        int mid = (lx + rx) / 2;

        return max(calc(l, r, 2 * x + 1, lx, mid), calc(l, r, 2 * x + 2, mid, rx));
    }
    int calc(int l, int r) {
        return calc(l, r, 0, 0, size);
    }
};

struct Query {
    int l, r, k;

    int id;
};

int N, M;
vector<int> w;
vector<Query> queries;
vector<int> ans;
SegTree st;

int invcomp(int a, int b)
{
    return a > b;
}

// Get last place in local maximum array that is BEFORE r
int getlast(int r, vector<int>& s)
{
    int pos = upper_bound(s.begin(), s.end(), r, invcomp) - s.begin();

    return s[pos];
}

int main(void)
{
    scanf("%d %d", &N, &M);

    w.resize(N);
    st.init(N + 1);
    queries.resize(M);
    ans.resize(M);

    for (int i = 0; i < N; i++) {
        scanf("%d", &w[i]);
    }
    w.pb(INF);

    for (int i = 0; i < M; i++) {
        scanf("%d %d %d", &queries[i].l, &queries[i].r, &queries[i].k);
        queries[i].l--;
        queries[i].id = i;
    }

    vector<int> s;
    s.pb(N);
    int p = 0;
    for (int i = N - 1; i >= 0; i--) {
        while (w[s.front()] <= w[i]) {
            st.set(s.front(), w[s.front()]);
        }

        int res = st.calc(i, s.front());
        st.set(i, res + w[i]);
        s.pb(i);

        while (p < M && queries[p].l == i) {
            Query cur = queries[p];

            int pos = getlast(cur.r, s);
            int res = st.calc(cur.l, pos);
            if (pos + 1 != cur.r) {
                res = max(res, st.calc(pos + 1, cur.r) + w[pos]);
            }

            ans[cur.id] = res <= cur.k;

            p++;
        }
    }

    for (int i = 0; i < M; i++) {
        printf("%d\n", ans[i]);
    }

    return 0;
}

Compilation message

sortbooks.cpp: In function 'int main()':
sortbooks.cpp:88:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |     scanf("%d %d", &N, &M);
      |     ~~~~~^~~~~~~~~~~~~~~~~
sortbooks.cpp:96:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |         scanf("%d", &w[i]);
      |         ~~~~~^~~~~~~~~~~~~
sortbooks.cpp:101:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |         scanf("%d %d %d", &queries[i].l, &queries[i].r, &queries[i].k);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 344 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 344 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 408 ms 73888 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 37 ms 6856 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 344 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 1 ms 344 KB Output isn't correct
4 Halted 0 ms 0 KB -