結果

問題 No.59 鉄道の旅
ユーザー xuzijian629xuzijian629
提出日時 2018-11-03 00:55:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,055 bytes
コンパイル時間 2,103 ms
コンパイル使用メモリ 200,480 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-13 01:43:27
合計ジャッジ時間 5,183 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,384 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 4 ms
4,380 KB
testcase_12 AC 16 ms
4,384 KB
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;

class BIT {
    int n;
    vi data;

    i64 sum(int i) {
        i64 s = 0;
        while (i > 0) {
            s += data[i];
            i -= i & -i;
        }
        return s;
    }

public:
    BIT(int n) : n(n) {
        data = vi(n + 1);
    }

    void add(int i, i64 x) {
        i++;
        while (i <= n) {
            data[i] += x;
            i += i & -i;
        }
    }

    // [l, r)
    i64 sum(int l, int r) {
        return sum(r) - sum(l);
    }
};

int main() {
    int ans = 0;
    BIT cnt(101010);
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        int w;
        cin >> w;
        if (w < 0) {
            w = -w;
            if (cnt.sum(w, w + 1)) {
                cnt.add(w, -1);
                ans--;
            } 
        } else {
            if (cnt.sum(w, 101010) < k) {
                cnt.add(w, 1);
                ans++;
            }
        }
    }
    cout << ans << endl;
}
0