結果

問題 No.59 鉄道の旅
ユーザー togatogatogatoga
提出日時 2015-08-03 12:02:01
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 35 ms / 5,000 ms
コード長 1,999 bytes
コンパイル時間 766 ms
コンパイル使用メモリ 90,660 KB
実行使用メモリ 10,996 KB
最終ジャッジ日時 2023-08-26 06:33:09
合計ジャッジ時間 1,731 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
10,780 KB
testcase_01 AC 3 ms
10,704 KB
testcase_02 AC 4 ms
10,744 KB
testcase_03 AC 3 ms
10,700 KB
testcase_04 AC 35 ms
10,644 KB
testcase_05 AC 4 ms
10,704 KB
testcase_06 AC 4 ms
10,996 KB
testcase_07 AC 4 ms
10,700 KB
testcase_08 AC 7 ms
10,804 KB
testcase_09 AC 7 ms
10,728 KB
testcase_10 AC 8 ms
10,916 KB
testcase_11 AC 5 ms
10,744 KB
testcase_12 AC 17 ms
10,808 KB
testcase_13 AC 35 ms
10,744 KB
testcase_14 AC 34 ms
10,700 KB
testcase_15 AC 4 ms
10,800 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <algorithm>
#include <functional>
#include <limits.h>
#include <bitset>

#include <tuple>
#include <unordered_map>

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
// 1-indexed
///////////////////////////////////////////////////////////////////
template <typename Weight> class BIT {
private:
  int N; //要素数
  vector<Weight> data;

public:
  BIT() {}
  BIT(int N) : N(N) { data.resize(N + 1); }
  void init(int N) {
    this->N = N;
    data.resize(N + 1);
  }
  Weight sum(int index) { // O(logN) sum [1, index]
    Weight res = 0;
    while (index > 0) {
      res += data[index];
      index -= index & -index;
    }
    return res;
  }
  Weight sum(int left, int right) { // sum[left, right]
    return sum(right) - sum(left - 1);
  }
  void add(int index, Weight x) {
    while (index <= N) {
      data[index] += x;
      index += index & -index;
    }
  }
  Weight operator[](int index) { return sum(index) - sum(index - 1); }
};
///////////////////////////////////////////////////////
int N, K;
const int MAX_W = 1000010;

int main() {
  cin >> N >> K;
  BIT<ll> bit;
  bit.init(MAX_W);

  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    if (x >= 0) {
      int res = bit.sum(x, MAX_W);
      if (res >= K) {
        continue;
      }
      bit.add(x, 1);
    } else {
      x = -x;
      int res = bit[x];
      if (res == 0)
        continue;
      bit.add(x, -1);
    }
  }
  cout << bit.sum(MAX_W) << endl;
  return 0;
}
0