結果

問題 No.649 ここでちょっとQK!
ユーザー chocoruskchocorusk
提出日時 2018-10-14 03:54:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 411 ms / 3,000 ms
コード長 2,449 bytes
コンパイル時間 1,678 ms
コンパイル使用メモリ 96,424 KB
実行使用メモリ 12,800 KB
最終ジャッジ日時 2024-04-20 20:14:11
合計ジャッジ時間 8,526 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 301 ms
5,376 KB
testcase_04 AC 215 ms
12,800 KB
testcase_05 AC 219 ms
12,800 KB
testcase_06 AC 219 ms
12,800 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 182 ms
7,552 KB
testcase_13 AC 180 ms
7,680 KB
testcase_14 AC 175 ms
7,552 KB
testcase_15 AC 178 ms
7,680 KB
testcase_16 AC 174 ms
7,680 KB
testcase_17 AC 211 ms
7,936 KB
testcase_18 AC 234 ms
8,448 KB
testcase_19 AC 254 ms
8,960 KB
testcase_20 AC 287 ms
9,344 KB
testcase_21 AC 320 ms
9,728 KB
testcase_22 AC 329 ms
10,240 KB
testcase_23 AC 358 ms
10,624 KB
testcase_24 AC 363 ms
11,008 KB
testcase_25 AC 386 ms
11,520 KB
testcase_26 AC 411 ms
11,904 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 3 ms
5,376 KB
testcase_29 AC 3 ms
5,376 KB
testcase_30 AC 135 ms
7,552 KB
testcase_31 AC 135 ms
7,680 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <random>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

random_device rnd;
mt19937 mt(rnd());
uniform_real_distribution<> rnd1(0, 1.0);

struct node_t{
	ll val;
	node_t *lch;
	node_t *rch;
	double pri;
	int cnt;
	node_t() {}
	node_t(ll v, double p):val(v), pri(p), cnt(1), lch(nullptr), rch(nullptr){}

};

int count(node_t *t){
	return !t ? 0 : t->cnt;
}

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

node_t *merge(node_t *l, node_t *r){
	if(!l || !r) return !l ? r : l;
	if(l->pri > r->pri){
		l->rch=merge(l->rch, r);
		return update(l);
	}else{
		r->lch=merge(l, r->lch);
		return update(r);
	}
}

pair<node_t*, node_t*> split(node_t *t, ll x){
	if(!t) return make_pair(nullptr, nullptr);
	if(x <= t->val){
		pair<node_t*, node_t*> s=split(t->lch, x);
		t->lch=s.second;
		return make_pair(s.first, update(t));
	}else{
		pair<node_t*, node_t*> s=split(t->rch, x);
		t->rch=s.first;
		return make_pair(update(t), s.second);
	}
}

pair<node_t*, node_t*> split2(node_t *t, int k){ //[0, k), [k,n)
	if(!t) return make_pair(nullptr, nullptr);
	if(k <= count(t->lch)){
		pair<node_t*, node_t*> s=split2(t->lch, k);
		t->lch=s.second;
		return make_pair(s.first, update(t));
	}else{
		pair<node_t*, node_t*> s=split2(t->rch, k-1-count(t->lch));
		t->rch=s.first;
		return make_pair(update(t), s.second);
	}
}

node_t *insert(node_t *t, ll x){
	pair<node_t*, node_t*> s=split(t, x);
	node_t *u; u=new node_t;
	*u=node_t(x, rnd1(mt));
	return merge(merge(s.first, u), s.second);
}

node_t *erase(node_t *t, int k){ //erase a[k]
	pair<node_t*, node_t*> s=split2(t, k);
	pair<node_t*, node_t*> s2=split2(s.second, 1);
	return merge(s.first, s2.second);
}

ll find(node_t *t, int k){ //a[k]
	if(count(t)<=k) return -1;
	if(count(t->lch)==k){
		return t->val;
	}else if(count(t->lch)<k){
		return find(t->rch, k-count(t->lch)-1);
	}else{
		return find(t->lch, k);
	}
}

int main()
{
	int q;
	int k;
	cin>>q>>k;
	node_t* s=nullptr;
	for(int i=0; i<q; i++){
		int t; cin>>t;
		if(t==1){
			ll x; cin>>x;
			s=insert(s, x);
		}else{
			ll y=find(s, k-1);
			cout<<y<<endl;
			if(y>=0) s=erase(s, k-1);
		}
    }
    return 0;
}
0