結果
| 問題 |
No.649 ここでちょっとQK!
|
| コンテスト | |
| ユーザー |
chocorusk
|
| 提出日時 | 2018-10-14 03:54:19 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 406 ms / 3,000 ms |
| コード長 | 2,449 bytes |
| コンパイル時間 | 1,114 ms |
| コンパイル使用メモリ | 96,504 KB |
| 実行使用メモリ | 12,800 KB |
| 最終ジャッジ日時 | 2024-10-12 18:16:11 |
| 合計ジャッジ時間 | 10,108 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 32 |
ソースコード
#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;
}
chocorusk