結果

問題 No.649 ここでちょっとQK!
ユーザー Taiki0715Taiki0715
提出日時 2023-08-30 17:21:11
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 261 ms / 3,000 ms
コード長 7,090 bytes
コンパイル時間 8,332 ms
コンパイル使用メモリ 150,312 KB
実行使用メモリ 15,988 KB
最終ジャッジ日時 2023-08-30 17:21:31
合計ジャッジ時間 18,950 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 261 ms
4,384 KB
testcase_04 AC 68 ms
15,960 KB
testcase_05 AC 67 ms
15,972 KB
testcase_06 AC 69 ms
15,988 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 100 ms
8,484 KB
testcase_13 AC 99 ms
8,504 KB
testcase_14 AC 100 ms
8,448 KB
testcase_15 AC 104 ms
8,540 KB
testcase_16 AC 109 ms
9,032 KB
testcase_17 AC 118 ms
9,380 KB
testcase_18 AC 135 ms
9,916 KB
testcase_19 AC 146 ms
10,336 KB
testcase_20 AC 161 ms
10,864 KB
testcase_21 AC 177 ms
11,360 KB
testcase_22 AC 188 ms
11,908 KB
testcase_23 AC 203 ms
12,344 KB
testcase_24 AC 217 ms
12,868 KB
testcase_25 AC 237 ms
13,388 KB
testcase_26 AC 247 ms
13,824 KB
testcase_27 AC 3 ms
4,384 KB
testcase_28 AC 2 ms
4,384 KB
testcase_29 AC 2 ms
4,384 KB
testcase_30 AC 107 ms
8,556 KB
testcase_31 AC 124 ms
8,868 KB
testcase_32 AC 2 ms
4,384 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
template<int mod>istream &operator>>(istream &is,static_modint<mod> &a){long long b;is>>b;a=b;return is;}
istream &operator>>(istream &is,modint &a){long long b;cin>>b;a=b;return is;}
#endif
using ll=long long;
using ull=unsigned long long;
using P=pair<ll,ll>;
template<typename T>using minque=priority_queue<T,vector<T>,greater<T>>;
template<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);}
template<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);}
template<typename T1,typename T2>istream &operator>>(istream &is,pair<T1,T2>&p){is>>p.first>>p.second;return is;}
template<typename T>istream &operator>>(istream &is,vector<T> &a){for(auto &i:a)is>>i;return is;}
template<typename T1,typename T2>void operator++(pair<T1,T2>&a,int n){a.first++,a.second++;}
template<typename T1,typename T2>void operator--(pair<T1,T2>&a,int n){a.first--,a.second--;}
template<typename T>void operator++(vector<T>&a,int n){for(auto &i:a)i++;}
template<typename T>void operator--(vector<T>&a,int n){for(auto &i:a)i--;}
#define reps(i,a,n) for(int i=(a);i<(n);i++)
#define rep(i,n) reps(i,0,n)
#define all(x) x.begin(),x.end()
#define pcnt(x) __builtin_popcount(x)
ll myceil(ll a,ll b){return (a+b-1)/b;}
template<typename T,size_t n,size_t id=0>
auto vec(const int (&d)[n],const T &init=T()){
  if constexpr (id<n)return vector(d[id],vec<T,n,id+1>(d,init));
  else return init;
}
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) static_cast<void>(0)
template<typename T1,typename T2>ostream &operator<<(ostream &os,const pair<T1,T2>&p){os<<p.first<<' '<<p.second;return os;}
#endif
void SOLVE();
int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  #ifdef LOCAL
  clock_t start=clock();
  #endif
  int testcase=1;
  //cin>>testcase;
  for(int i=0;i<testcase;i++){
    SOLVE();
  }
  #ifdef LOCAL
  cout<<"time:";
  cout<<(clock()-start)/1000;
  cout<<"ms\n";
  #endif
}
template<typename S,S(*op)(S,S),S(*e)()>
struct splay_tree{
  struct node{
    node *par,*left,*right;
    int sz;
    S val,sum;
    node():par(nullptr),left(nullptr),right(nullptr),sz(1),val(e()),sum(e()){}
    void set(S v){
      this->val=v;
      this->sum=v;
      if(this->left)this->sum=op(this->left->sum,this->sum);
      if(this->right)this->sum=op(this->sum,this->right->sum);
    }
    void rotate(){
      node *pp,*p,*c;
      p=this->par;
      pp=p->par;
      if(p->left==this){
        c=this->right;
        this->right=p;
        p->left=c;
      }
      else{
        c=this->left;
        this->left=p;
        p->right=c;
      }
      if(pp&&pp->left==p)pp->left=this;
      if(pp&&pp->right==p)pp->right=this;
      this->par=pp;
      p->par=this;
      if(c)c->par=p;
      p->update();
      this->update();
    }
    int state()const{
      if(!this->par)return 0;
      if(this->par->left==this)return 1;
      if(this->par->right==this)return -1;
      return 0;
    }
    void splay(){
      while(this->state()){
        if(this->par->state()==0)this->rotate();
        else if(this->state()==this->par->state()){
          this->par->rotate();
          this->rotate();
        }
        else{
          this->rotate();
          this->rotate();
        }
      }
    }
    void update(){
      this->sz=1;
      this->sum=this->val;
      if(this->left){
        this->sz+=this->left->sz;
        this->sum=op(this->left->sum,this->sum);
      }
      if(this->right){
        this->sz+=this->right->sz;
        this->sum=op(this->sum,this->right->sum);
      }
    }
  };
private:
  node *root=nullptr;
  node *get(int id,node *root){
    node *now=root;
    while(true){
      int lsize=now->left?now->left->sz:0;
      if(id<lsize)now=now->left;
      else if(id==lsize){
        now->splay();
        return now;
      }
      else{
        now=now->right;
        id-=lsize+1;
      }
    }
  }
  node *merge(node *lroot,node *rroot){
    if(!lroot)return rroot;
    if(!rroot)return lroot;
    lroot=get(lroot->sz-1,lroot);
    lroot->right=rroot;
    rroot->par=lroot;
    lroot->update();
    return lroot;
  }
  pair<node*,node*>split(int lcnt,node *root){
    if(lcnt==0)return {nullptr,root};
    if(lcnt==root->sz)return {root,nullptr};
    root=get(lcnt,root);
    node *lroot,*rroot;
    lroot=root->left;
    rroot=root;
    lroot->par=nullptr;
    rroot->left=nullptr;
    rroot->update();
    return {lroot,rroot};
  }
  node *insert(int id,node *nd,node *root){
    auto spl=split(id,root);
    return merge(merge(spl.first,nd),spl.second);
  }
  node *erase(int id,node *root){
    root=get(id,root);
    node *lroot=root->left;
    node *rroot=root->right;
    if(lroot)lroot->par=nullptr;
    if(rroot)rroot->par=nullptr;
    delete(root);
    return merge(lroot,rroot);
  }
  node *between(int l,int r){
    if(l==0&&r==this->root->sz)return this->root;
    if(l==0){
      root=get(r,root);
      return root->left;
    }
    if(r==this->root->sz){
      root=get(l-1,root);
      return root->right;
    }
    node *rht=get(r,root);
    node *lft=rht->left;
    lft->par=nullptr;
    root=lft;
    lft=get(l-1,lft);
    root=rht;
    rht->left=lft;
    lft->par=rht;
    rht->update();
    return lft->right;
  }
public:
  const S &operator[](int id){
    root=get(id,root);
    return root->val;
  }
  void insert(S x){
    node *nd=new node();
    nd->set(x);
    if(!root){
      root=insert(0,nd,root);
      return;
    }
    node *now=root;
    while(true){
      if(now->val==x){
        now->splay();
        root=now;
        int lsize=root->left?root->left->sz:0;
        root=insert(lsize,nd,root);
        return;
      }
      else if(now->val>x){
        if(!now->left){
          now->left=nd;
          nd->par=now;
          nd->splay();
          root=nd;
          return;
        }
        else now=now->left;
      }
      else{
        if(!now->right){
          now->right=nd;
          nd->par=now;
          nd->splay();
          root=nd;
          return;
        }
        else now=now->right;
      }
    }
  }
  void erase(S x){
    if(!root)return;
    node *now=root;
    while(true){
      if(now->val==x){
        now->splay();
        root=now;
        int lsize=now->left?now->left->sz:0;
        root=erase(lsize,root);
        return;
      }
      else if(now->val>x){
        if(!now->left)return;
        else now=now->left;
      }
      else{
        if(!now->right)return;
        else now=now->right;
      }
    }
  }
  int size()const{return root?root->sz:0;}
  S prod(int l,int r){
    if(l==r)return e();
    node *pr=between(l,r);
    return pr->sum;
  }
};//バグってるかも
ll op(ll a,ll b){return 0;}
ll e(){return 0;}
void SOLVE(){
  int q,k;
  cin>>q>>k;
  splay_tree<ll,op,e>s;
  while(q--){
    int t;
    cin>>t;
    if(t==1){
      ll x;
      cin>>x;
      s.insert(x);
    }
    else{
      if(s.size()<k)cout<<-1<<endl;
      else{
        ll ans=s[k-1];
        cout<<ans<<endl;
        s.erase(ans);
      }
    }
  }
}
0