結果

問題 No.59 鉄道の旅
ユーザー kyunakyuna
提出日時 2019-07-27 22:35:17
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 38 ms / 5,000 ms
コード長 1,101 bytes
コンパイル時間 571 ms
コンパイル使用メモリ 70,576 KB
実行使用メモリ 7,088 KB
最終ジャッジ日時 2023-09-15 11:13:53
合計ジャッジ時間 1,547 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,848 KB
testcase_01 AC 3 ms
6,844 KB
testcase_02 AC 3 ms
6,936 KB
testcase_03 AC 3 ms
6,848 KB
testcase_04 AC 38 ms
6,800 KB
testcase_05 AC 3 ms
7,020 KB
testcase_06 AC 3 ms
6,964 KB
testcase_07 AC 3 ms
7,016 KB
testcase_08 AC 6 ms
7,024 KB
testcase_09 AC 7 ms
6,868 KB
testcase_10 AC 7 ms
6,848 KB
testcase_11 AC 5 ms
6,844 KB
testcase_12 AC 18 ms
6,848 KB
testcase_13 AC 37 ms
7,088 KB
testcase_14 AC 35 ms
6,904 KB
testcase_15 AC 4 ms
7,000 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 + 1, UNITY_SUM) { }  // [1, n]
    inline void add(int i, Abel x) {
        while (i < dat.size()) { dat[i] += x; i += i & -i; }
    }
    inline Abel sum(int i) {    // [1, i]
        Abel res = UNITY_SUM;
        while (i > 0) { res += dat[i]; i -= i & -i; }
        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 = 1; i < dat.size(); ++i) cout << sum(i, i + 1) << ",";
        cout << endl;
    }
};

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