結果

問題 No.59 鉄道の旅
ユーザー ninoinuininoinui
提出日時 2020-09-06 03:02:15
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 42 ms / 5,000 ms
コード長 1,013 bytes
コンパイル時間 2,274 ms
コンパイル使用メモリ 200,424 KB
実行使用メモリ 11,064 KB
最終ジャッジ日時 2023-08-19 13:25:10
合計ジャッジ時間 3,487 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
10,684 KB
testcase_01 AC 6 ms
10,776 KB
testcase_02 AC 4 ms
10,680 KB
testcase_03 AC 5 ms
10,876 KB
testcase_04 AC 42 ms
10,800 KB
testcase_05 AC 5 ms
11,064 KB
testcase_06 AC 4 ms
10,680 KB
testcase_07 AC 6 ms
10,864 KB
testcase_08 AC 9 ms
10,740 KB
testcase_09 AC 9 ms
10,732 KB
testcase_10 AC 9 ms
10,740 KB
testcase_11 AC 6 ms
10,728 KB
testcase_12 AC 19 ms
10,816 KB
testcase_13 AC 37 ms
10,796 KB
testcase_14 AC 36 ms
10,724 KB
testcase_15 AC 6 ms
10,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename T> class FenwickTree {
  int N, M;
  vector<T> B;
public:
  FenwickTree(int n) : N(n + 1), M(1), B(N, 0) {
    while (M * 2 <= N) M *= 2;
  }
  FenwickTree(const vector<T> &V) : N((int) V.size() + 1), M(1), B(N, 0) {
    for (int i = 0; i < N - 1; i++) add(i, V.at(i));
    while (M * 2 <= N) M *= 2;
  }
  void add(int i, T x) {
    i++;
    while (i < N) B.at(i) += x, i += i & -i;
  }
  void add(int l, int r, T x) {
    add(l, x);
    add(r + 1 , -x);
  }
  T sum(int i) {
    i++;
    T ret = 0;
    while (i > 0) ret += B.at(i), i -= i & -i;
    return ret;
  }
  T sum(int l, int r) {
    return sum(r) - sum(l - 1);
  }
};

int main() {
  int N, K;
  cin >> N >> K;
  FenwickTree<long> FT(1e6+1);
  for (int i = 0; i < N; i++) {
    int W;
    cin >> W;
    if (W > 0) {
      if (FT.sum(W, 1e6) >= K) continue;
      FT.add(W, 1);
    } else {
      W = -W;
      if (FT.sum(W, W)) FT.add(W, -1);
    }
  }
  cout << FT.sum(1e6) << "\n";
}
0