結果

問題 No.868 ハイパー部分和問題
ユーザー lam6er
提出日時 2025-04-15 22:20:29
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,990 bytes
コンパイル時間 1,921 ms
コンパイル使用メモリ 198,796 KB
実行使用メモリ 63,388 KB
最終ジャッジ日時 2025-04-15 22:22:23
合計ジャッジ時間 35,939 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 2
other AC * 29 TLE * 1 -- * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int MAX_K = 15000;

class SegmentTree {
    struct Node {
        bitset<MAX_K + 1> bs;
    };
    int n, K;
    vector<Node> tree;

    void merge(bitset<MAX_K + 1>& res, const bitset<MAX_K + 1>& L, const bitset<MAX_K + 1>& R) {
        res = L | R;
        if (res[K]) return;
        for (int t = 0; t <= K; ++t) {
            if (R[t]) {
                auto shifted = L << t;
                res |= shifted;
                if (res[K]) break;
            }
        }
    }

    void update(int pos, const bitset<MAX_K + 1>& val) {
        pos += n;
        tree[pos].bs = val;
        for (pos >>= 1; pos >= 1; pos >>= 1) {
            int left = 2 * pos;
            int right = 2 * pos + 1;
            merge(tree[pos].bs, tree[left].bs, tree[right].bs);
        }
    }

public:
    SegmentTree(const vector<int>& A, int K_val) : K(K_val) {
        int size = A.size();
        n = 1;
        while (n < size) n <<= 1;
        tree.resize(2 * n);

        for (int i = 0; i < size; ++i) {
            int a = A[i];
            bitset<MAX_K + 1> bs;
            bs.set(0);
            if (a <= K) bs.set(a);
            tree[n + i].bs = bs;
        }

        for (int i = n - 1; i >= 1; --i) {
            merge(tree[i].bs, tree[2 * i].bs, tree[2 * i + 1].bs);
        }
    }

    void updateElement(int index, int value) {
        bitset<MAX_K + 1> bs;
        bs.set(0);
        if (value <= K) bs.set(value);
        update(index, bs);
    }

    bool query() {
        return tree[1].bs[K];
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int N, K;
    cin >> N >> K;
    vector<int> A(N);
    for (int i = 0; i < N; ++i) {
        cin >> A[i];
    }

    SegmentTree st(A, K);

    int Q;
    cin >> Q;
    while (Q--) {
        int x, v;
        cin >> x >> v;
        st.updateElement(x - 1, v); // Convert to 0-based index
        cout << (st.query() ? 1 : 0) << '\n';
    }

    return 0;
}
0