AC_CSP @ 2023-02-13 14:25:39
record
改的能AC板子的代码,却得了44分(
#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int n,m;
int pre,nxt,lstans,ans;
struct Node{
Node *son[2];
int size,val,cnt,rank;
Node(int _val){
val=_val,cnt=size=1,rank=rand();
son[0]=son[1]=nullptr;
}
inline void _update(){
size=cnt;
if(son[0]!=nullptr) size+=son[0]->size;
if(son[1]!=nullptr) size+=son[1]->size;
}
};
inline void _rotate(Node *&cur,bool opt){
Node *tmp=cur->son[opt];
cur->son[opt]=tmp->son[!opt];
tmp->son[!opt]=cur;
cur->_update(),tmp->_update();
cur=tmp;
}
inline void _insert(Node *&cur,int val){
if(cur==nullptr) cur=new Node(val);
else if(cur->val==val) cur->cnt++,cur->size++;
else if(cur->val>val){
_insert(cur->son[0],val);
if(cur->son[0]->rank<cur->rank) _rotate(cur,0);
cur->_update();
}else{
_insert(cur->son[1],val);
if(cur->son[1]->rank<cur->rank) _rotate(cur,1);
cur->_update();
}
}
inline void _delete(Node *&cur,int val){
if(cur->val>val) _delete(cur->son[0],val),cur->_update();
else if(cur->val<val) _delete(cur->son[1],val),cur->_update();
else if(cur->cnt>1) cur->cnt--,cur->size--;
else{
uint8_t state=(cur->son[0]!=nullptr)|((cur->son[1]!=nullptr)<<1);
Node *tmp=cur;
switch(state){
case 0:delete cur;cur=nullptr;break;
case 1:cur=tmp->son[0];delete tmp;break;
case 2:cur=tmp->son[1];delete tmp;break;
case 3:int opt=cur->son[0]->rank<cur->son[1]->rank?0:1;
_rotate(cur,opt);_delete(cur->son[!opt],val);cur->_update();break;
}
}
}
inline int query_rank(Node *&cur,int val){
int less_size=cur->son[0]==nullptr?0:cur->son[0]->size;
if(cur->val==val) return less_size+1;
else if(cur->val>val){
if(cur->son[0]!=nullptr) return query_rank(cur->son[0],val);
else return 1;
}else if(cur->son[1]!=nullptr) return less_size+cur->cnt+query_rank(cur->son[1],val);
else return cur->size+1;
}
inline int query_val(Node *&cur,int rank){
int less_size=cur->son[0]==nullptr?0:cur->son[0]->size;
if(rank<=less_size) return query_val(cur->son[0],rank);
else if(rank<=less_size+cur->cnt) return cur->val;
else return query_val(cur->son[1],rank-less_size-cur->cnt);
}
inline int query_pre(Node *&cur,int val){
if(cur->val>=val){
if(cur->son[0]!=nullptr) return query_pre(cur->son[0],val);
}else{
pre=cur->val;
if(cur->son[1]!=nullptr) query_pre(cur->son[1],val);
return pre;
}
return -INF;
}
inline int query_nxt(Node *&cur,int val){
if(cur->val<=val){
if(cur->son[1]!=nullptr) return query_nxt(cur->son[1],val);
}else{
nxt=cur->val;
if(cur->son[0]!=nullptr) query_nxt(cur->son[0],val);
return nxt;
}
return INF;
}
int main(){
srand(time(0));
Node *root=new Node(INF);
scanf("%d%d",&n,&m);
while(n--){
int x;
scanf("%d",&x);
_insert(root,x);
}
while(m--){
int opt,x;
scanf("%d%d",&opt,&x);x^=lstans;
if(opt==1) _insert(root,x);
if(opt==2) _delete(root,x);
if(opt==3) ans^=lstans=query_rank(root,x);
if(opt==4) ans^=lstans=query_val(root,x);
if(opt==5) ans^=lstans=query_pre(root,x);
if(opt==6) ans^=lstans=query_nxt(root,x);
}
printf("%d\n",ans);
return 0;
}
by TheSky233 @ 2023-02-13 22:33:22
@AC_CSP 注意数据范围,inf 开小了,换成
https://www.luogu.com.cn/record/102106281 小号测的
by AC_CSP @ 2023-02-13 22:52:36
@TheSky233 感谢,已关
by retamian @ 2023-10-15 20:23:53
@TheSky233 感谢大佬,我开的1e9
死活过不掉,调了三个小时 (下次开2e9)