結果

問題 No.59 鉄道の旅
ユーザー kyunakyuna
提出日時 2019-07-27 22:27:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,187 bytes
コンパイル時間 679 ms
コンパイル使用メモリ 71,372 KB
実行使用メモリ 7,164 KB
最終ジャッジ日時 2023-09-15 11:13:37
合計ジャッジ時間 1,686 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,888 KB
testcase_01 AC 4 ms
6,856 KB
testcase_02 AC 3 ms
6,852 KB
testcase_03 WA -
testcase_04 AC 39 ms
6,948 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 4 ms
6,964 KB
testcase_08 AC 7 ms
6,848 KB
testcase_09 AC 7 ms
6,848 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 37 ms
6,960 KB
testcase_14 AC 35 ms
6,808 KB
testcase_15 AC 4 ms
6,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename Abel> struct BIT {
    const Abel UNITY_SUM = 0;
    vector<Abel> dat;
    BIT(int n) : dat(n, UNITY_SUM) { }  // [0, n)
    inline void add(int i, Abel x) {
        while (i < dat.size()) { dat[i] += x; i |= i + 1; }
    }
    inline Abel sum(int i) {    // [0, i]
        Abel res = UNITY_SUM;
        while (i >= 0) { res += dat[i]; i = (i & (i + 1)) - 1; }
        return res;
    }
    inline Abel sum(int a, int b) { return sum(b - 1) - sum(a - 1); }   // [a, b)
    /* debug */
    Abel operator[](int i) { return sum(i, i + 1); }
    void dump() {
        for (int i = 0; i < dat.size(); ++i) cout << sum(i, i + 1) << ",";
        cout << endl;
    }
};

int main() {
    int n, k; cin >> n >> k;
    BIT<int> bit(1000001);
    int cnt = 0;
    while (n--) {
        int w; cin >> w;
        if (w > 0) {
            if (cnt - bit.sum(w) >= k) continue;
            cnt++;
            bit.add(w, 1);
        } else {
            w *= -1;
            if (bit[w] == 0) continue;
            bit.add(w, -1);
            cnt--;
        }
    }
    cout << cnt << endl;
    return 0;
}
0