結果

問題 No.59 鉄道の旅
ユーザー koba-e964koba-e964
提出日時 2015-05-25 21:53:18
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 73 ms / 5,000 ms
コード長 2,254 bytes
コンパイル時間 988 ms
コンパイル使用メモリ 101,000 KB
実行使用メモリ 11,444 KB
最終ジャッジ日時 2023-08-26 05:39:35
合計ジャッジ時間 2,101 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
11,372 KB
testcase_01 AC 6 ms
11,380 KB
testcase_02 AC 7 ms
11,308 KB
testcase_03 AC 7 ms
11,376 KB
testcase_04 AC 73 ms
11,272 KB
testcase_05 AC 6 ms
11,268 KB
testcase_06 AC 7 ms
11,308 KB
testcase_07 AC 6 ms
11,344 KB
testcase_08 AC 14 ms
11,228 KB
testcase_09 AC 12 ms
11,260 KB
testcase_10 AC 13 ms
11,260 KB
testcase_11 AC 9 ms
11,408 KB
testcase_12 AC 37 ms
11,320 KB
testcase_13 AC 55 ms
11,272 KB
testcase_14 AC 61 ms
11,312 KB
testcase_15 AC 7 ms
11,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef long long int ll;
typedef vector<int> VI;
typedef pair<int, int> PI;
const double EPS=1e-9;

/**
 * Segment Tree. This data structure is useful for fast folding on intervals of an array
 * whose elements are elements of monoid M. Note that constructing this tree requires the identity
 * element of M and the operation of M.
 */
template<class I, class BiOp = I (*) (I, I)>
class SegTree {
	int n;
	vector<I> dat;
	BiOp op;
	I e;
public:
	SegTree(int n_, BiOp op, I e) : op(op), e(e) {
		n = 1;
		while (n < n_) { n *= 2; } // n is a power of 2
		dat.resize(2 * n);
		for (int i = 0; i < 2 * n - 1; i++) {
			dat[i] = e;
		}
	}
	/* ary[k] <- v */
	void update(int k, I v) {
		k += n - 1;
		dat[k] = v;
		while (k > 0) {
			k = (k - 1) / 2;
			dat[k] = op(dat[2 * k + 1], dat[2 * k + 2]);
		}
	}
	void update_array(int k, int len, I *vals) {
		for (int i = 0; i < len; ++i) {
			update(k + i, vals[i]);
		}
	}
	/* l,r are for simplicity */
	I querySub(int a, int b, int k, int l, int r) {
		// [a,b) and  [l,r) intersects?
		if (r <= a || b <= l) return e;
		if (a <= l && r <= b) return dat[k];
		I vl = querySub(a, b, 2 * k + 1, l, (l + r) / 2);
		I vr = querySub(a, b, 2 * k + 2, (l + r) / 2, r);
		return op(vl, vr);
	}
	/* [a, b] (note: inclusive) */
	I query(int a, int b) {
		return querySub(a, b + 1, 0, 0, n);
	}
};

int main(void){
  const int M = 1048500;
  int n, k;
  cin >> n >> k;
  SegTree<int, plus<int> > st(M, plus<int>(), 0);
  REP (loop_var, 0, n) {
    int w; cin >> w;
    if (w > 0) {
      int q = st.query(w, M);
      if (q < k) {
	st.update(w, st.query(w, w) + 1);
      }
    } else {
      w = -w;
      int v = st.query(w, w);
      if (v > 0) {
	st.update(w, v - 1);
      }
    }
  }
  cout << st.query(1, M) << endl;
}
0