結果

問題 No.59 鉄道の旅
ユーザー kyo1kyo1
提出日時 2020-06-08 17:26:08
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 18 ms / 5,000 ms
コード長 825 bytes
コンパイル時間 2,626 ms
コンパイル使用メモリ 198,808 KB
実行使用メモリ 10,892 KB
最終ジャッジ日時 2023-08-26 21:35:50
合計ジャッジ時間 2,712 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
10,688 KB
testcase_01 AC 4 ms
10,760 KB
testcase_02 AC 3 ms
10,664 KB
testcase_03 AC 3 ms
10,704 KB
testcase_04 AC 18 ms
10,736 KB
testcase_05 AC 3 ms
10,756 KB
testcase_06 AC 3 ms
10,784 KB
testcase_07 AC 3 ms
10,800 KB
testcase_08 AC 5 ms
10,760 KB
testcase_09 AC 6 ms
10,796 KB
testcase_10 AC 5 ms
10,784 KB
testcase_11 AC 4 ms
10,892 KB
testcase_12 AC 10 ms
10,744 KB
testcase_13 AC 16 ms
10,736 KB
testcase_14 AC 16 ms
10,660 KB
testcase_15 AC 4 ms
10,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

template <typename T>
class FenwickTree {
 private:
  std::vector<T> ft;

 public:
  FenwickTree(int sz) : ft(sz + 1, 0) {}

  void add(int idx, T x) {
    for (int i = idx + 1; i < static_cast<int>(ft.size()); i += i & -i) {
      ft[i] += x;
    }
  }

  T operator()(int idx) const {
    T res = 0;
    for (int i = idx + 1; i > 0; i -= i & -i) {
      res += ft[i];
    }
    return res;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  FenwickTree<int> ft(2000000);
  for (int i = 0; i < N; i++) {
    int w;
    cin >> w;
    if (w > 0) {
      if (ft(2000000) - ft(w - 1) < K) ft.add(w, 1);
    } else {
      w *= -1;
      if (ft(w) - ft(w - 1) > 0) ft.add(w, -1);
    }
  }
  cout << ft(2000000) << '\n';
  return 0;
}
0