結果

問題 No.618 labo-index
ユーザー beetbeet
提出日時 2018-12-04 12:11:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 535 ms / 6,000 ms
コード長 3,889 bytes
コンパイル時間 2,131 ms
コンパイル使用メモリ 203,660 KB
実行使用メモリ 76,212 KB
最終ジャッジ日時 2023-09-21 08:32:22
合計ジャッジ時間 12,963 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 5 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,384 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 4 ms
4,380 KB
testcase_10 AC 5 ms
4,380 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 4 ms
4,380 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 6 ms
4,380 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 8 ms
4,380 KB
testcase_19 AC 364 ms
4,388 KB
testcase_20 AC 371 ms
4,384 KB
testcase_21 AC 363 ms
4,380 KB
testcase_22 AC 370 ms
4,380 KB
testcase_23 AC 365 ms
4,380 KB
testcase_24 AC 364 ms
4,380 KB
testcase_25 AC 370 ms
4,380 KB
testcase_26 AC 365 ms
4,380 KB
testcase_27 AC 372 ms
4,380 KB
testcase_28 AC 362 ms
4,376 KB
testcase_29 AC 363 ms
4,380 KB
testcase_30 AC 364 ms
4,376 KB
testcase_31 AC 366 ms
4,380 KB
testcase_32 AC 367 ms
4,384 KB
testcase_33 AC 363 ms
4,380 KB
testcase_34 AC 535 ms
76,212 KB
testcase_35 AC 438 ms
13,212 KB
testcase_36 AC 300 ms
4,380 KB
testcase_37 AC 503 ms
39,756 KB
testcase_38 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


template<typename T,size_t X>
struct BinaryTrie{
  struct Node{
    size_t cnt;
    Node *p,*l,*r;
    Node(Node* p):cnt(0),p(p){l=r=nullptr;}
  };
  
  T acc;
  Node *root;
  BinaryTrie():acc(0){root=emplace(nullptr);}

  void dfs(Node *a){
    if(!a) return;
    dfs(a->l);dfs(a->r);
    delete(a);
  }
  
  inline Node* emplace(Node* p){
    return new Node(p);
  }

  inline size_t count(Node* a){
    return a?a->cnt:0;
  }
  
  void add(const T b,size_t k=1){
    const T nb=b^acc;
    Node* a=root;
    for(Int i=X-1;i>=0;i--){
      bool f=(nb>>i)&1;
      if(!f&&!a->l) a->l=emplace(a);
      if( f&&!a->r) a->r=emplace(a);
      a=f?a->r:a->l;
    }
    a->cnt+=k;    
    while((a=a->p)) a->cnt=count(a->l)+count(a->r);    
  }

  inline void update(const T b){acc^=b;}

  Node* find(const T b){
    const T nb=b^acc;
    Node* a=root;
    for(Int i=X-1;i>=0;i--){
      bool f=(nb>>i)&1;
      a=f?a->r:a->l;
      if(!a) return a;
    }
    return a;
  }

  Node* check(Node *a){
    if(!a||count(a)) return a;
    delete(a);
    return nullptr;
  }

  void sub(Node* a,size_t k=1){
    assert(a&&a->cnt>=k);
    a->cnt-=k;
    while((a=a->p)){
      a->l=check(a->l);
      a->r=check(a->r);
      a->cnt=count(a->l)+count(a->r);
    }
  }
  
  Node* xmax(const T b){
    assert(count(root));
    const T nb=b^acc;
    Node* a=root;    
    for(Int i=X-1;i>=0;i--){
      bool f=(nb>>i)&1;
      if(!a->l||!a->r) a=a->l?a->l:a->r;
      else a=f?a->l:a->r;
    }
    return a;
  }

  Node* xmin(const T b){
    return xmax(~b&((T(1)<<X)-1));
  }

  Node* ge(Node *a,Int i){
    if(!a) return a;
    Node *l=a->l,*r=a->r;
    if((acc>>i)&1) swap(l,r);
    if(l||r) return ge(l?l:r,i+1);
    return a;
  }
  
  Node* next(Node* a,Int i){
    if(!(a->p)) return nullptr;
    Node *l=a->p->l,*r=a->p->r;
    if((acc>>(i+1))&1) swap(l,r);
    if(a==l&&r) return ge(r,i);
    return next(a->p,i+1);
  }
  
  Node* lower_bound(const T b){
    const T nb=b^acc;
    Node* a=root;
    for(Int i=X-1;i>=0;i--){
      bool f=(nb>>i)&1;
      if(!f&&a->l){a=a->l;continue;}
      if( f&&a->r){a=a->r;continue;}
      if((b>>i)&1) return next(a,i);
      return ge(a,i);
    }
    return a;
  }

  Node* upper_bound(const T b){
    return lower_bound(b+1);
  }
  
  T val(Node* a){
    T res(0);
    for(Int i=0;i<(Int)X;i++){
      assert(a->p);
      res|=(T(a==a->p->r)<<i);
      a=a->p;
    }
    return res^acc;
  }

  Node* find_by_order(size_t k){
    Node *a=root;
    if(count(a)<=k) return nullptr;
    for(Int i=X-1;i>=0;i--){
      bool f=(acc>>i)&1;
      if(count(f?a->r:a->l)<=k){
        k-=count(f?a->r:a->l);
        a=f?a->l:a->r;
      }else{
        a=f?a->r:a->l;
      }
    }
    return a;
  }
  
  size_t order_of_key(const T b){
    Node *a=root;
    size_t res=0;
    for(Int i=X-1;i>=0;i--){      
      Node *l=a->l,*r=a->r;
      if((acc>>i)&1) swap(l,r);
      bool f=(b>>i)&1;
      if(f) res+=count(l); 
      a=f?r:l;
      if(!a) break;
    }
    return res;
  }
};

//INSERT ABOVE HERE
signed main(){
  Int q;
  cin>>q;
  using BT = BinaryTrie<Int, 60>;
  const Int OFS = 1e16;
  vector<Int> xs;
  Int sum=0,cnt=0;

  BT bt;
  for(Int i=0;i<q;i++){
    Int t,x;
    cin>>t>>x;
    if(t==1){
      bt.add(OFS+x-sum);
      xs.emplace_back(OFS+x-sum);
      cnt++;
    }
    if(t==2){
      x--;
      bt.sub(bt.find(xs[x]));
      cnt--;
    }
    if(t==3){
      sum+=x;
    }
    auto check=
      [&](Int k)->Int{
        return cnt-(Int)bt.order_of_key(OFS+k-sum)>=k;
      };
    Int l=0,r=q+1;
    while(l+1<r){
      Int m=(l+r)>>1;
      if(check(m)) l=m;
      else r=m;
    }
    cout<<l<<endl;
  }
  
  return 0;
}
0