結果

問題 No.649 ここでちょっとQK!
ユーザー tomatoma
提出日時 2019-09-25 00:10:53
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 615 ms / 3,000 ms
コード長 2,282 bytes
コンパイル時間 1,994 ms
コンパイル使用メモリ 188,080 KB
実行使用メモリ 44,916 KB
最終ジャッジ日時 2023-10-19 18:34:26
合計ジャッジ時間 11,752 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 289 ms
6,440 KB
testcase_04 AC 412 ms
44,916 KB
testcase_05 AC 405 ms
44,916 KB
testcase_06 AC 175 ms
6,440 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 280 ms
22,280 KB
testcase_13 AC 276 ms
22,280 KB
testcase_14 AC 274 ms
22,280 KB
testcase_15 AC 272 ms
22,280 KB
testcase_16 AC 261 ms
22,280 KB
testcase_17 AC 293 ms
24,128 KB
testcase_18 AC 327 ms
25,712 KB
testcase_19 AC 363 ms
27,560 KB
testcase_20 AC 402 ms
29,144 KB
testcase_21 AC 443 ms
33,036 KB
testcase_22 AC 479 ms
34,620 KB
testcase_23 AC 517 ms
36,468 KB
testcase_24 AC 546 ms
38,052 KB
testcase_25 AC 593 ms
39,636 KB
testcase_26 AC 615 ms
41,484 KB
testcase_27 AC 3 ms
4,348 KB
testcase_28 AC 3 ms
4,348 KB
testcase_29 AC 3 ms
4,348 KB
testcase_30 AC 197 ms
16,208 KB
testcase_31 AC 189 ms
16,208 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 2 ms
4,348 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 Q, K;
	cin >> Q >> K;
	vector<pair<ll, ll>> querys(Q);
	rep(i, Q) {
		ll type, v = 0;
		cin >> type;
		if (type == 1)cin >> v;
		querys[i] = { type,v };
	}

	// offline query's compression
	set<ll> st;
	for (const auto& query : querys)
		if (query.first == 1)
			st.insert(query.second);
	map<ll, ll> trans, rev;
	ll cnt = 0;
	for (auto& num : st) {
		trans[num] = cnt;
		rev[cnt] = num;
		cnt++;
	}

	// segment tree init
	auto f = [](ll a, ll b) {return a + b; };
	SegmentTree<ll> seg(f, 0);
	seg.init(cnt);

	// query
	for (auto& query : querys) {
		ll type, v;
		tie(type, v) = query;
		if (type == 1) {
			ll pos = trans[v];
			ll num = seg.query(pos, pos + 1);
			seg.set_val(pos, num + 1);
		}
		else {
			if (seg.query(0, cnt) < K) {
				//cerr << "debug";
				cout << -1 << endl;
				continue;
			}

			// binary search
			ll out = -1, safe = cnt - 1;
			while (abs(safe - out) > 1) {
				ll mid = (out + safe) / 2;
				(seg.query(0, mid + 1) >= K ? safe : out) = mid;
			}
			//cerr << "debug";
			cout << rev[safe] << endl;
			ll num = seg.query(safe, safe + 1);
			assert(num > 0);
			seg.set_val(safe, num - 1);
		}
	}

	return 0;
}
0