結果

問題 No.59 鉄道の旅
ユーザー iqueue02iqueue02
提出日時 2019-12-23 20:40:46
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 134 ms / 5,000 ms
コード長 1,318 bytes
コンパイル時間 1,979 ms
コンパイル使用メモリ 178,232 KB
実行使用メモリ 26,068 KB
最終ジャッジ日時 2023-10-19 03:23:38
合計ジャッジ時間 3,483 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
19,732 KB
testcase_01 AC 8 ms
19,732 KB
testcase_02 AC 8 ms
19,732 KB
testcase_03 AC 8 ms
19,732 KB
testcase_04 AC 134 ms
25,012 KB
testcase_05 AC 8 ms
19,732 KB
testcase_06 AC 8 ms
19,732 KB
testcase_07 AC 8 ms
19,732 KB
testcase_08 AC 19 ms
20,260 KB
testcase_09 AC 15 ms
20,260 KB
testcase_10 AC 16 ms
19,996 KB
testcase_11 AC 12 ms
19,732 KB
testcase_12 AC 48 ms
19,732 KB
testcase_13 AC 83 ms
24,748 KB
testcase_14 AC 97 ms
26,068 KB
testcase_15 AC 8 ms
19,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n);i++)
#define sz(x) int(x.size())
typedef long long ll;
typedef pair<int,int> P;

template <typename T> 
struct SegmentTree {
  
  int n;
  vector<T> node;

  T f(T a, T b) {return (a + b);}

  SegmentTree (int sz, T init = 0) {
    n = 1; while (n < sz) n <<= 1;
    node.assign(2*n, init);
  }

  void update(int k, T x) {
    k += (n - 1);
    node[k] += x;
    while (k > 0) {
      k = (k - 1) >> 1;
      node[k] = f(node[2*k+1], node[2*k+2]);
    } 
  }

  T query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    if (r <= a || b <= l) return 0;
    if (a <= l && r <= b) return node[k];
    T vl = query(a, b, 2*k+1, l, (l+r)/2);
    T vr = query(a, b, 2*k+2, (l+r)/2, r);
    return f(vl, vr);
  }

  T getvalue(int k) {
    return query(k,k+1);
  }

};

int main(){
  int n, k;
  cin >> n >> k;
  SegmentTree<ll> seg(1000010,0);
  map<int,ll> mp;
  rep(i,n) {
    int w;
    cin >> w;
    if (w > 0) {
      if ( seg.query(w,1000001) < k) {
        seg.update(w,1); 
        mp[w]++;
      }
    } else {
      w *= -1;
      if (mp[w] > 0) {
        mp[w]--;
        seg.update(w,-1);
      }   
    }
  }
  ll res = 0;
  for (auto e : mp) res += e.second; 
  cout << res << endl;
  return 0;
}
0