結果

問題 No.59 鉄道の旅
ユーザー KudeKude
提出日時 2020-09-07 06:46:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 37 ms / 5,000 ms
コード長 996 bytes
コンパイル時間 1,475 ms
コンパイル使用メモリ 165,368 KB
実行使用メモリ 7,056 KB
最終ジャッジ日時 2023-08-19 16:22:22
合計ジャッジ時間 2,579 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,932 KB
testcase_01 AC 3 ms
7,036 KB
testcase_02 AC 3 ms
7,040 KB
testcase_03 AC 3 ms
6,912 KB
testcase_04 AC 34 ms
7,056 KB
testcase_05 AC 3 ms
6,888 KB
testcase_06 AC 4 ms
6,932 KB
testcase_07 AC 3 ms
6,968 KB
testcase_08 AC 6 ms
6,960 KB
testcase_09 AC 6 ms
7,040 KB
testcase_10 AC 6 ms
6,884 KB
testcase_11 AC 4 ms
6,960 KB
testcase_12 AC 17 ms
6,848 KB
testcase_13 AC 37 ms
6,972 KB
testcase_14 AC 35 ms
7,040 KB
testcase_15 AC 3 ms
6,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
template<typename T>
struct BIT {
  int n;
  vector<T> d;
  BIT(int n=0):n(n),d(n+1) {}
  void add(int i, T x=1) {
    for (i++; i <= n; i += i&-i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i&-i) {
      x += d[i];
    }
    return x;
  }
  T sum(int l, int r) {
    return sum(r-1) - sum(l-1);
  }
  int operator[](int x) {
      return sum(x, x + 1);
  }
};
int main() {
    int n, k;
    cin >> n >> k;
    BIT<int> d(1000001);
    rep(_, n) {
        int w;
        cin >> w;
        if (w > 0) {
            if (d.sum(w, 1000001) < k) d.add(w);
        } else {
            if (d[-w] > 0) d.add(-w, -1);
        }
    }
    cout << d.sum(1000000) << endl;
}
0