結果

問題 No.59 鉄道の旅
ユーザー maine_honzukimaine_honzuki
提出日時 2020-05-08 23:13:12
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,031 bytes
コンパイル時間 1,446 ms
コンパイル使用メモリ 165,892 KB
実行使用メモリ 4,460 KB
最終ジャッジ日時 2023-09-17 03:28:42
合計ジャッジ時間 3,821 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

template <typename T>
struct BinaryIndexedTree {
    vector<T> data;
    int n;
    BinaryIndexedTree(int n_) : n(n_ + 1), data(n_ + 1, 0) {}
    T sum(int i) {
        T ret(0);
        for (int x = i; x > 0; x -= x & -x)
            ret += data[x];
        return ret;
    }
    void add(int i, T a) {
        for (int x = i; x <= n; x += x & -x)
            data[x] += a;
    }
};

int main() {
    int N, K, W[100010];
    cin >> N >> K;
    for (int i = 0; i < N; i++) {
        cin >> W[i];
    }

    int cnt[100020] = {};
    BinaryIndexedTree<int> bit(100020);
    int MAX = 100010;

    for (int i = 0; i < N; i++) {
        if (W[i] > 0) {
            if (bit.sum(MAX) - bit.sum(W[i] - 1) < K) {
                bit.add(W[i], 1);
                cnt[W[i]]++;
            }
        } else {
            W[i] *= -1;
            if (cnt[W[i]]) {
                cnt[W[i]]--;
                bit.add(W[i], -1);
            }
        }
    }
    cout << bit.sum(MAX) << endl;
}
0