結果

問題 No.618 labo-index
ユーザー beet
提出日時 2018-12-04 12:11:50
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 611 ms / 6,000 ms
コード長 3,889 bytes
コンパイル時間 2,397 ms
コンパイル使用メモリ 197,676 KB
最終ジャッジ日時 2025-01-06 18:08:14
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0