結果

問題 No.649 ここでちょっとQK!
ユーザー momoharamomohara
提出日時 2019-11-13 00:25:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 405 ms / 3,000 ms
コード長 3,431 bytes
コンパイル時間 2,578 ms
コンパイル使用メモリ 105,864 KB
実行使用メモリ 22,472 KB
最終ジャッジ日時 2023-10-19 20:30:19
合計ジャッジ時間 10,024 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,276 KB
testcase_01 AC 3 ms
5,276 KB
testcase_02 AC 3 ms
5,276 KB
testcase_03 AC 261 ms
8,444 KB
testcase_04 AC 182 ms
22,472 KB
testcase_05 AC 184 ms
22,472 KB
testcase_06 AC 61 ms
8,444 KB
testcase_07 AC 3 ms
5,276 KB
testcase_08 AC 3 ms
5,276 KB
testcase_09 AC 3 ms
5,276 KB
testcase_10 AC 4 ms
5,276 KB
testcase_11 AC 3 ms
5,276 KB
testcase_12 AC 174 ms
13,264 KB
testcase_13 AC 176 ms
13,264 KB
testcase_14 AC 174 ms
13,244 KB
testcase_15 AC 172 ms
13,272 KB
testcase_16 AC 159 ms
13,268 KB
testcase_17 AC 187 ms
13,984 KB
testcase_18 AC 212 ms
14,964 KB
testcase_19 AC 230 ms
15,668 KB
testcase_20 AC 256 ms
16,400 KB
testcase_21 AC 290 ms
17,100 KB
testcase_22 AC 307 ms
18,088 KB
testcase_23 AC 337 ms
18,808 KB
testcase_24 AC 361 ms
19,528 KB
testcase_25 AC 379 ms
20,248 KB
testcase_26 AC 405 ms
21,224 KB
testcase_27 AC 4 ms
5,276 KB
testcase_28 AC 4 ms
5,276 KB
testcase_29 AC 4 ms
5,276 KB
testcase_30 AC 149 ms
11,096 KB
testcase_31 AC 134 ms
11,092 KB
testcase_32 AC 3 ms
5,276 KB
testcase_33 AC 3 ms
5,276 KB
testcase_34 AC 3 ms
5,276 KB
testcase_35 AC 4 ms
5,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#include<cstring>
#include<string>
#include<cassert>
#include<cmath>
#include<climits>
#include<iomanip>
#include<bitset>
#include<unordered_map>

using namespace std;

#define REP(i,n) for(ll (i)=0;(i)<(n);(i)++)
#define rep(i,j,n) for(ll (i)=(j);(i)<(n);(i)++)
#define FOR(i,c) for(decltype((c).begin())i=(c).begin();i!=(c).end();++i)
#define ll long long
#define ull unsigned long long
#define all(hoge) (hoge).begin(),(hoge).end()
typedef pair<ll, ll> P;
const long long INF = 1LL << 60;
const long long MOD = 1e9 + 7;
typedef vector<ll> Array;
typedef vector<Array> Matrix;


template<class T> inline bool chmin(T& a, T b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}
template<class T> inline bool chmax(T& a, T b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}

//グラフ関連
struct Edge {//グラフ
	ll to, cap, rev;
	Edge(ll _to, ll _cap, ll _rev) {
		to = _to; cap = _cap; rev = _rev;
	}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph& G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
	G[from].push_back(Edge(to, cap, (ll)G[to].size()));
	if (revFlag)G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

class SumSegTree {
private:

	int _sum(int a, int b, int k, int l, int r) {
		if (r <= a || b <= l)return 0;    // 交差しない
		if (a <= l && r <= b)return dat[k];   // a,l,r,bの順で完全に含まれる
		else {
			int s1 = _sum(a, b, 2 * k + 1, l, (l + r) / 2); // 左の子
			int s2 = _sum(a, b, 2 * k + 2, (l + r) / 2, r); // 右の子
			return s1 + s2;
		}
	}
public:
	int n, height;
	vector<int> dat;

	// 初期化(_nは最大要素数)
	SumSegTree(int _n) {
		n = 1;
		height = 1;
		while (n < _n) {
			n *= 2;
			height++;
		}
		dat = vector<int>(2 * n - 1, 0);
	}

	// 場所i(0-indexed)にxを足す
	void add(int i, int x) {
		i += n - 1; // i番目の葉ノードへ
		dat[i] += x;
		while (i > 0) { // 下から上がっていく
			i = (i - 1) / 2;
			dat[i] += x;
		}
	}

	// 区間[a,b)の総和。ノードk=[l,r)に着目している。
	int sum(int a, int b) {
		return _sum(a, b, 0, 0, n);
	}

	int find_by_order(int k) {
		//k番目の要素を返す
		ll l = -1;
		ll r = n;
		ll x = n;
		while (r - l > 1) {
			ll mid = l + r >> 1;
			if (sum(0, mid + 1) >= k) {
				x = mid;
				r = mid;
			}
			else {
				l = mid;
			}
		}
		return x;
	}

	int order_of_key(int k) {
		//kが何番目かを返す
		return sum(0, k);
	}

	void insert(int k) {
		add(k, 1);
	}
	void erase(int k) {
		add(k, -1);
	}
};

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	ll q, k;
	cin >> q >> k;

	

	vector<P> qs(q);
	map<ll, ll> mp;
	
	REP(i, q) {
		cin >> qs[i].first;
		if (qs[i].first == 1) {
			cin >> qs[i].second;
			mp[qs[i].second]++;
		}
		else {
			qs[i].second = 0;
		}
	}

	ll num = 0;
	Array vs;
	for (auto i : mp) {
		vs.push_back(i.first);
		mp[i.first] = num;
		num++;
	}

	SumSegTree st(2e5+10);

	ll size = 0;
	REP(i, q) {
		if (qs[i].first == 1) {
			st.insert(mp[qs[i].second]);
			//cout << "b:" << qs[i].second << mp[qs[i].second] << endl;
			size++;
		}
		else {
			if (size >= k) {
				ll kk = st.find_by_order(k);
				cout << vs[kk] << endl;
				st.erase(kk);
				size--;
			}
			else {
				cout << -1 << endl;
			}
		}
	}


	return 0;
}
0