結果

問題 No.59 鉄道の旅
ユーザー yuppe19 😺yuppe19 😺
提出日時 2019-09-22 10:12:18
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 5,000 ms
コード長 828 bytes
コンパイル時間 762 ms
コンパイル使用メモリ 68,584 KB
実行使用メモリ 7,200 KB
最終ジャッジ日時 2023-10-19 07:07:17
合計ジャッジ時間 1,796 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,200 KB
testcase_01 AC 4 ms
7,200 KB
testcase_02 AC 4 ms
7,200 KB
testcase_03 AC 4 ms
7,200 KB
testcase_04 AC 23 ms
7,200 KB
testcase_05 AC 4 ms
7,200 KB
testcase_06 AC 4 ms
7,200 KB
testcase_07 AC 4 ms
7,200 KB
testcase_08 AC 6 ms
7,200 KB
testcase_09 AC 6 ms
7,200 KB
testcase_10 AC 6 ms
7,200 KB
testcase_11 AC 5 ms
7,200 KB
testcase_12 AC 14 ms
7,200 KB
testcase_13 AC 19 ms
7,200 KB
testcase_14 AC 19 ms
7,200 KB
testcase_15 AC 4 ms
7,200 KB
権限があれば一括ダウンロードができます

ソースコード

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