結果
| 問題 | No.59 鉄道の旅 | 
| コンテスト | |
| ユーザー |  kyo1 | 
| 提出日時 | 2020-06-08 17:26:08 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 74 ms / 5,000 ms | 
| コード長 | 825 bytes | 
| コンパイル時間 | 2,440 ms | 
| コンパイル使用メモリ | 192,540 KB | 
| 最終ジャッジ日時 | 2025-01-11 00:12:42 | 
| ジャッジサーバーID (参考情報) | judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 4 | 
| other | AC * 12 | 
ソースコード
#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;
}
            
            
            
        