結果

問題 No.59 鉄道の旅
ユーザー simansiman
提出日時 2022-03-17 00:20:52
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 81 ms / 5,000 ms
コード長 1,097 bytes
コンパイル時間 4,826 ms
コンパイル使用メモリ 133,856 KB
実行使用メモリ 15,624 KB
最終ジャッジ日時 2023-10-25 07:59:34
合計ジャッジ時間 5,594 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
11,660 KB
testcase_01 AC 9 ms
11,660 KB
testcase_02 AC 9 ms
11,660 KB
testcase_03 AC 9 ms
11,660 KB
testcase_04 AC 81 ms
15,096 KB
testcase_05 AC 9 ms
11,660 KB
testcase_06 AC 9 ms
11,660 KB
testcase_07 AC 9 ms
11,660 KB
testcase_08 AC 14 ms
11,660 KB
testcase_09 AC 15 ms
11,660 KB
testcase_10 AC 14 ms
11,660 KB
testcase_11 AC 10 ms
11,660 KB
testcase_12 AC 23 ms
11,660 KB
testcase_13 AC 69 ms
14,832 KB
testcase_14 AC 76 ms
15,624 KB
testcase_15 AC 9 ms
11,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <map>
#include <queue>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

class BinaryIndexTree {
public:
  vector <ll> bit;
  int N;

  BinaryIndexTree(int n) {
    N = n;

    for (int i = 0; i <= N; ++i) {
      bit.push_back(0);
    }
  }

  ll sum(int i) {
    ll ret = 0;

    while (i > 0) {
      ret += bit[i];
      i -= i & -i;
    }

    return ret;
  }

  void add(int i, ll x) {
    while (i <= N) {
      bit[i] += x;
      i += i & -i;
    }
  }
};

int main() {
  int N, K;
  cin >> N >> K;

  int L = 1000000;
  BinaryIndexTree bit(1000010);

  map<int, int> counter;
  int w;
  for (int i = 0; i < N; ++i) {
    cin >> w;

    if (w >= 0) {
      int sum = bit.sum(L) - bit.sum(w - 1);
      if (sum >= K) continue;

      counter[w]++;
      bit.add(w, 1);
    } else {
      if (counter[abs(w)] == 0) continue;
      counter[abs(w)]--;
      bit.add(abs(w), -1);
    }
  }

  cout << bit.sum(L) << endl;

  return 0;
}
0