結果

問題 No.59 鉄道の旅
ユーザー tomatoma
提出日時 2019-09-24 22:30:19
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 65 ms / 5,000 ms
コード長 1,646 bytes
コンパイル時間 1,837 ms
コンパイル使用メモリ 173,456 KB
実行使用メモリ 20,596 KB
最終ジャッジ日時 2023-10-19 16:45:55
合計ジャッジ時間 3,029 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
19,804 KB
testcase_01 AC 8 ms
19,804 KB
testcase_02 AC 7 ms
19,804 KB
testcase_03 AC 8 ms
19,804 KB
testcase_04 AC 65 ms
20,596 KB
testcase_05 AC 8 ms
19,804 KB
testcase_06 AC 7 ms
19,804 KB
testcase_07 AC 8 ms
19,804 KB
testcase_08 AC 14 ms
19,804 KB
testcase_09 AC 14 ms
19,804 KB
testcase_10 AC 13 ms
19,804 KB
testcase_11 AC 10 ms
19,804 KB
testcase_12 AC 32 ms
20,596 KB
testcase_13 AC 53 ms
20,596 KB
testcase_14 AC 58 ms
20,596 KB
testcase_15 AC 8 ms
19,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;

template<typename T>
class SegmentTree {
private:
	using F = function<T(T, T)>; // モノイド型
	int n; // 横幅
	F f;   // モノイド
	T e;   // モノイド単位元
	vector<T> data;

public:
	// init忘れに注意
	SegmentTree() {}
	SegmentTree(F f, T e) :f(f), e(e) {}
	void init(int n_) {
		n = 1;
		while (n < n_)n <<= 1;
		data.assign(n << 1, e);
	}
	void build(const vector<T>& v) {
		int n_ = v.size();
		init(n_);
		rep(i, n_)data[n + i] = v[i];
		for (int i = n - 1; i >= 0; i--) {
			data[i] = f(data[(i << 1) | 0], data[(i << 1) | 1]);
		}
	}
	void set_val(int idx, T val) {
		idx += n;
		data[idx] = val;
		while (idx >>= 1) {
			data[idx] = f(data[(idx << 1) | 0], data[(idx << 1) | 1]);
		}
	}
	T query(int a, int b) {
		// [a,b)
		T vl = e, vr = e;
		for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
			if (l & 1)vl = f(vl, data[l++]); // unknown
			if (r & 1)vr = f(data[--r], vr); // unknown
		}
		return f(vl, vr);
	}
};

int main()
{
	// input
	ll N, K;
	cin >> N >> K;
	vector<ll> W(N);
	rep(i, N)cin >> W[i];

	// preprocess
	constexpr ll lim = 1e6 + 10;
	auto f = [](ll a, ll b) {return a + b; };
	SegmentTree<ll> seg(f, 0);
	seg.init(lim);

	// query
	for (auto w : W) {
		if (w > 0) {
			ll now = seg.query(w, w + 1);
			if (seg.query(w, lim) < K) {
				seg.set_val(w, now + 1);
			}
		}
		else {
			w *= -1;
			ll now = seg.query(w, w + 1);
			now = max(now - 1, 0ll);
			seg.set_val(w, now);
		}
	}
	cout << seg.query(0, lim) << endl;




	return 0;
}
0