結果

問題 No.59 鉄道の旅
ユーザー kimiyukikimiyuki
提出日時 2016-11-22 20:58:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,317 bytes
コンパイル時間 609 ms
コンパイル使用メモリ 73,648 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-26 07:36:55
合計ジャッジ時間 3,268 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 3 ms
4,384 KB
testcase_12 AC 17 ms
4,376 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <functional>
using namespace std;

template <typename T>
struct binary_indexed_tree { // on monoid
    vector<T> a;
    T unit;
    function<T (T,T)> append; // associative
    template <typename F>
    binary_indexed_tree(size_t n, T a_unit, F a_append) : a(n, a_unit), unit(a_unit), append(a_append) {}
    void point_append(size_t i, T w) { // a[i] += w
        for (size_t j = i+1; j <= a.size(); j += j & -j) a[j-1] = append(a[j-1], w);
    }
    int initial_range_concat(size_t i) { // sum [0, i)
        T acc = unit;
        for (size_t j = i; 0 < j; j -= j & -j) acc = append(acc, a[j-1]);
        return acc;
    }
    T point_get(size_t i) {
        return initial_range_concat(i+1) - initial_range_concat(i);
    }
};

const int w_max = 100000;
int main() {
    int n, k; cin >> n >> k;
    binary_indexed_tree<int> bit(w_max+1, int(), plus<int>());
    while (n --) {
        int w; cin >> w;
        int i = w_max - abs(w);
        if (w > 0) {
            if (bit.initial_range_concat(i+1) < k) {
                bit.point_append(i, 1);
            }
        } else if (w < 0) {
            if (bit.point_get(i)) {
                bit.point_append(i, -1);
            }
        }
    }
    cout << bit.initial_range_concat(w_max) << endl;
    return 0;
}
0