結果

問題 No.649 ここでちょっとQK!
ユーザー Taiki0715Taiki0715
提出日時 2023-08-30 17:21:11
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 236 ms / 3,000 ms
コード長 7,090 bytes
コンパイル時間 4,193 ms
コンパイル使用メモリ 180,352 KB
実行使用メモリ 15,872 KB
最終ジャッジ日時 2024-06-10 17:03:36
合計ジャッジ時間 9,656 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 236 ms
5,376 KB
testcase_04 AC 58 ms
15,872 KB
testcase_05 AC 59 ms
15,872 KB
testcase_06 AC 60 ms
15,872 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 88 ms
8,320 KB
testcase_13 AC 89 ms
8,320 KB
testcase_14 AC 86 ms
8,320 KB
testcase_15 AC 90 ms
8,448 KB
testcase_16 AC 89 ms
8,960 KB
testcase_17 AC 102 ms
9,216 KB
testcase_18 AC 114 ms
9,856 KB
testcase_19 AC 128 ms
10,240 KB
testcase_20 AC 136 ms
10,752 KB
testcase_21 AC 144 ms
11,264 KB
testcase_22 AC 163 ms
11,776 KB
testcase_23 AC 168 ms
12,288 KB
testcase_24 AC 179 ms
12,928 KB
testcase_25 AC 195 ms
13,312 KB
testcase_26 AC 203 ms
13,696 KB
testcase_27 AC 3 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 94 ms
8,320 KB
testcase_31 AC 98 ms
8,576 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 2 ms
5,376 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