二截贪食蛇(BCS Plus)

来源:互联网 发布:淘宝店铺域名怎么设置 编辑:程序博客网 时间:2024/06/12 08:37

class binary_cut_snake{
typedef unsigned long long _ull;
private :
struct node{
int value;
int degree[2];
_ull up,down;
node *left;
node *right;
node (_ull _up,_ull _down){
up = _up ;
down = _down ;
left = NULL ;
right = NULL ;
degree[0] = degree[1] = 0 ;
value = 0;
}
};
static const _ull _maxull = 18446744073709551615ull;
node *first;
public :
binary_cut_snake() : first(new node(0,_maxull)){}
void push(int num,int value){
_ull _lf=0,_ri = _maxull;
node **now = &first;
_ull _noi = 9223372036854775808ull;
bool _isnimi = 1 ;
while(_lf ^ _ri){
_ull _mid = (_lf + _ri)>>1;
if(_noi & num){
if((*now)->right == NULL){
(*now)->right = new node(_mid + 1 ,_ri);
}
(*now)->degree[1] += _isnimi ;
now = & ((*now)->right);
_lf = _mid + 1;
(*now) ->down= _lf ;
(*now) -> up = _ri ;
}else{
if((*now)->left == NULL){
(*now)->left = new node(_lf ,_mid);
}
(*now)->degree[0] += _isnimi ;
now = & ((*now)->left);
_ri = _mid ;
(*now) ->down= _lf ;
(*now) -> up = _ri ;
}
_noi >>= 1 ;
if(_lf == _ri)(*now) -> value += value ;
}
}

    int get(int num){        _ull _noi = 9223372036854775808ull;        node **now = &first;        while(_noi){            if((*now)==NULL)return 0;            if(_noi & num){                now = & ((*now)->right) ;            }else{                now = & ((*now)->left)  ;            }            _noi >>= 1;         }        if((*now)==NULL)return 0;        return (*now) ->value;    }    int search_fore(_ull num){        _ull _noi = 9223372036854775808ull;        node **now = &first;        int _ans = 0;        while(_noi){            if(*now==NULL)return _ans;            if(_noi & num){                _ans += (*now)->degree[0];                now = & ((*now)->right);            }else{                now = & ((*now)->left);            }            _noi >>=1 ;        }        if((*now)==NULL)return _ans;        return _ans + 1;    }    bool include(_ull num){        _ull _noi = 9223372036854775808ull;        node **now = &first;        while(_noi){            if(*now==NULL)return 0;            if(_noi & num){                now = & ((*now)->right);            }else{                now = & ((*now)->left);            }            _noi >>=1 ;        }        if(*now==NULL)return 0;        return 1;    }    _ull fore(int num){        int _rank = search_fore(num);        return search_index(_rank-include(num));     }    _ull next(int num){        int _rank = search_fore(num);        int g=get(num);        return search_index(_rank+(g?g:1));    }    _ull search_index(int index){        _ull _noi = 9223372036854775808ull;        node **now = &first;        while(_noi){            if(*now==NULL)return 0;            if(index>(*now)->degree[0]){                index -= (*now)->degree[0];                now = & ((*now)->right);            }else{                now = & ((*now)->left);            }            _noi >>=1 ;        }        if(*now==NULL)return 0;        return (*now)->down;    }    int count(){        return first->degree[0]+first->degree[1];    }    void del(int num){        _ull _noi = 9223372036854775808ull;        node **now = &first;        while(_noi){            if(*now==NULL)return;            if(_noi & num){                (*now) -> degree[1] --;                if((*now) ->degree[1] + (*now) -> degree[0] ==0){                    (*now)=NULL;                    return ;                }                now = & ((*now) -> right);            }else{                (*now) -> degree[0] --;                if((*now) ->degree[0] + (*now) -> degree[1] ==0){                    (*now)=NULL;                    return ;                }                now = & ((*now) -> left);            }            _noi >>=1;        }        if(*now==NULL)return;        (*now)->value -- ;        if((*now)->value==0)*now=NULL;        return ;    }    _ull least(){        _ull _noi = 9223372036854775808ull;        _ull _ans = 0;        node **now = &first;        while(_noi){            if(*now==NULL)return 0;            if((*now)->degree[0]){                now = &((*now)->left);                _ans <<= 1;            }else{                now = &((*now)->right);                _ans <<= 1;                _ans = _ans | 1;            }            _noi >>= 1;        }        if(*now==NULL)return 0;        return _ans;    }

};

原创粉丝点击