結果

問題 No.59 鉄道の旅
ユーザー yuppe19 😺
提出日時 2019-09-22 10:12:18
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 23 ms / 5,000 ms
コード長 828 bytes
コンパイル時間 699 ms
コンパイル使用メモリ 68,352 KB
実行使用メモリ 7,332 KB
最終ジャッジ日時 2024-09-19 03:27:04
合計ジャッジ時間 1,535 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

template <class T>
class BIT {
  int N;
  vector<T> dat;
 public:
  explicit BIT(int n) : N(n+2), dat(N) {}
  void add(int k, T x) {
    for(int i=k+1; i<N; i+=i&-i) { dat[i] += x; }
  }
  T sum(int k) {
    T res = 0;
    for(int i=k; i; i-=i&-i) { res += dat[i]; }
    return res;
  }
};

constexpr int MAX_W = 1'000'005;

int main(void) {
  int N, K; scanf("%d%d", &N, &K);
  BIT<int> tree(MAX_W);
  int cnt;
  for(int i=0; i<N; ++i) {
    int wi; scanf("%d", &wi);
    if(wi > 0) {
      cnt = tree.sum(MAX_W) - tree.sum(wi);
      if(cnt < K) {
        tree.add(wi, 1);
      }
    } else {
      cnt = tree.sum(-wi+1) - tree.sum(-wi);
      if(cnt) {
        tree.add(-wi, -1);
      }
    }
  }
  int res = tree.sum(MAX_W);
  printf("%d\n", res);
  return 0;
}
0