結果

問題 No.649 ここでちょっとQK!
ユーザー shobonvipshobonvip
提出日時 2024-03-07 19:41:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 371 ms / 3,000 ms
コード長 2,568 bytes
コンパイル時間 2,432 ms
コンパイル使用メモリ 213,300 KB
実行使用メモリ 12,800 KB
最終ジャッジ日時 2024-03-07 19:41:44
合計ジャッジ時間 10,958 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,548 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 1 ms
6,548 KB
testcase_03 AC 261 ms
6,548 KB
testcase_04 AC 205 ms
12,800 KB
testcase_05 AC 191 ms
12,800 KB
testcase_06 AC 193 ms
12,800 KB
testcase_07 AC 2 ms
6,548 KB
testcase_08 AC 2 ms
6,548 KB
testcase_09 AC 2 ms
6,548 KB
testcase_10 AC 1 ms
6,548 KB
testcase_11 AC 2 ms
6,548 KB
testcase_12 AC 144 ms
7,168 KB
testcase_13 AC 148 ms
7,168 KB
testcase_14 AC 148 ms
7,168 KB
testcase_15 AC 154 ms
7,296 KB
testcase_16 AC 149 ms
7,680 KB
testcase_17 AC 195 ms
7,936 KB
testcase_18 AC 185 ms
8,320 KB
testcase_19 AC 224 ms
8,576 KB
testcase_20 AC 222 ms
8,960 KB
testcase_21 AC 233 ms
9,344 KB
testcase_22 AC 263 ms
9,728 KB
testcase_23 AC 278 ms
10,112 KB
testcase_24 AC 299 ms
10,496 KB
testcase_25 AC 314 ms
10,880 KB
testcase_26 AC 371 ms
11,264 KB
testcase_27 AC 2 ms
6,548 KB
testcase_28 AC 2 ms
6,548 KB
testcase_29 AC 3 ms
6,548 KB
testcase_30 AC 125 ms
7,168 KB
testcase_31 AC 121 ms
7,552 KB
testcase_32 AC 2 ms
6,548 KB
testcase_33 AC 2 ms
6,548 KB
testcase_34 AC 2 ms
6,548 KB
testcase_35 AC 2 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

random_device seed_gen;
mt19937 engine(seed_gen());
uniform_int_distribution<int> dist_treap(0, (1 << 30) - 1);

struct treap {
	struct node {
		node *lch, *rch;
		int par;
		ll val;
		int cnt;
		node(ll v, int p):
			lch(nullptr), rch(nullptr),
			par(p), val(v), cnt(1) {}
	};

	int calc_cnt(node* t){
		if (t == nullptr) return 0;
		return t->cnt;
	}

	node* update(node* t){
		t->cnt = calc_cnt(t->lch) + calc_cnt(t->rch) + 1;
		return t;
	}

	node* merge(node* a, node* b){
		if (a == nullptr || b == nullptr){
			if (a == nullptr) return b;
			return a;
		}

		if (a->par > b->par){
			a->rch = merge(a->rch, b);
			return update(a);
		}else{
			b->lch = merge(a, b->lch);
			return update(b);
		}
	}

	pair<node*, node*> split(node* t, int k){
		if (t == nullptr) return pair(nullptr, nullptr);
		assert(0 <= k && k <= calc_cnt(t));

		if (calc_cnt(t->lch) >= k){
			pair<node*, node*> ret = split(t->lch, k);
			t->lch = ret.second;
			return pair(ret.first, update(t));
		}else{
			pair<node*, node*> ret = split(t->rch, k - calc_cnt(t->lch) - 1);
			t->rch = ret.first;
			return pair(update(t), ret.second);
		}
	}

	node* insert(node* t, int k, ll v){
		return insert(t, k, v, dist_treap(engine));
	}

	node* insert(node* t, int k, ll v, int p){
		pair<node*, node*> tar = split(t, k);
		node* var = new node(v, p);
		return merge(merge(tar.first, var), tar.second);
	}

	node* erase(node* t, int k){
		assert(0 <= k && k < calc_cnt(t));
		pair<node*, node*> spl1 = split(t, k + 1);
		pair<node*, node*> spl2 = split(spl1.first, k);
		delete spl2.second;
		return merge(spl2.first, spl1.second);
	}

	// returns the number of elements less than v
	int lower_bound(node* t, ll v){
		if (t == nullptr) return 0;

		if (v <= t->val){
			return lower_bound(t->lch, v);
		}else{
			return calc_cnt(t->lch) + 1 + lower_bound(t->rch, v);
		}
	}

	node* insert(node* t, ll v){
		return insert(t, lower_bound(t, v), v);
	}

	// returns val
	ll get(node* t, int k){
		assert(t != nullptr);
		if (k < calc_cnt(t->lch)){
			return get(t->lch, k);
		}else if(calc_cnt(t->lch) == k){
			return t->val;
		}else{
			return get(t->rch, k - calc_cnt(t->lch) - 1);
		}
	}

	
};

int main(){
	treap T;
	treap::node* root = nullptr;
	int q,k; cin >> q >> k;
	while(q--){
		int t; cin >> t;
		if (t == 1){
			ll v; cin >> v;
			root = T.insert(root, v);
		}else{
			if (T.calc_cnt(root) >= k){
				cout << T.get(root, k-1) << '\n';
				root = T.erase(root, k-1);
			}else{
				cout << -1 << '\n';
			}
		}
	}
}
0