ii find_min(node *t,int left,int beg,int end){ if(t==NULL) return inf; ii r_left=left; ii r_right=left+t->cnt-1; ii sz=tree.get_count(t->left)+1; ii curr_ind=left+tree.get_count(t->left); if(beg<=r_left && end>=r_right){ return t->diff; } ii ret=inf; if(beg>curr_ind) ret=mini(ret,find_min(t->right,curr_ind+1,beg,end)); else if(end<curr_ind) ret=mini(ret,find_min(t->left,left,beg,end)); else ret=mini(mini(find_min(t->left,left,beg,end) ,find_min(t->right,curr_ind+1,beg,end)),ret); if(curr_ind>beg && curr_ind<=end && t->left){ ret=mini(ret,t->value - t->left->max_val); } if(curr_ind<end && curr_ind>=beg && t->right){ ret=mini(ret,t->right->min_val - t->value); } return ret; }
int find_count(node *t,int value){ if(t==NULL) return 0; if(value==t->value) return tree.get_count(t->left); else if(value > t->value) return tree.get_count(t->left) +find_count(t->right,value)+1; else return find_count(t->left,value); }
void mtree(node*a,int u,int d){ access(a); splay(a); if(d==1){ a->val+=u; if(a->vir) trp.down(a->vir),a->vir->add=u; }else if(d==2){ a->val=u; if(a->vir) trp.down(a->vir),a->vir->sam=u; } update(a); }
int main() { int i; for (i=3;i>=1;i--) T.insert(T.root,i); T.print(T.root); for (i=3;i>=1;i--) { T.remove(T.root,i); T.print(T.root); } system("pause"); return 0; }
int qtree(node*a,int d){ access(a); splay(a); int ret=a->val; if(d==1){ if(a->vir) trp.down(a->vir),gmin(ret,a->vir->mi); }else if(d==2){ if(a->vir) trp.down(a->vir),gmax(ret,a->vir->mx); }else if(d==3){ if(a->vir) trp.down(a->vir),ret+=a->vir->sum; } return ret; }
ii find_kth(node *t,int kth){ if(tree.get_count(t)<kth){ return -1; } int sz=tree.get_count(t->left)+1; if(sz==kth){ return t->value; } if(kth<sz){ return find_kth(t->left,kth); } else{ return find_kth(t->right,kth-sz); } }
int main(){ int i,j,k,l,test,t=1,val; //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); scanf("%d",&test); while(test--){ scanf("%s",type); //printf("%s\n",type); if(type[0]=='I'){ scanf("%d",&val); tree.insert(val); } if(type[0]=='D'){ scanf("%d",&val); tree.erase(val); } if(type[0]=='N'){ scanf("%d %d",&k,&l); printf("%d\n",find_min(k+1,l+1)); } if(type[0]=='X'){ scanf("%d %d",&k,&l); printf("%d\n",find_max(k+1,l+1)); } } return 0; }
void update(node*a){ for(int i=0;i<=1;++i) if(a->ch[i]) down(a->ch[i]); if(a->vir) trp.down(a->vir); a->mi=a->val; for(int i=0;i<=1;++i) if(a->ch[i]) gmin(a->mi,a->ch[i]->mi); a->virmi=inf; for(int i=0;i<=1;++i) if(a->ch[i]) gmin(a->virmi,a->ch[i]->virmi); if(a->vir) gmin(a->virmi,a->vir->mi); a->mx=a->val; for(int i=0;i<=1;++i) if(a->ch[i]) gmax(a->mx,a->ch[i]->mx); a->virmx=-inf; for(int i=0;i<=1;++i) if(a->ch[i]) gmax(a->virmx,a->ch[i]->virmx); if(a->vir) gmax(a->virmx,a->vir->mx); a->sum=a->val; for(int i=0;i<=1;++i) if(a->ch[i]) a->sum+=a->ch[i]->sum; a->virsum=0; for(int i=0;i<=1;++i) if(a->ch[i]) a->virsum+=a->ch[i]->virsum; if(a->vir) a->virsum+=a->vir->sum; a->siz=1; for(int i=0;i<=1;++i) if(a->ch[i]) a->siz+=a->ch[i]->siz; a->virsiz=0; for(int i=0;i<=1;++i) if(a->ch[i]) a->virsiz+=a->ch[i]->virsiz; if(a->vir) a->virsiz+=a->vir->siz; }
bool operator() ( treap<A> const & a, treap<A> const & b ) const { if( a.empty() ) { if( b.empty() ) return false; return b.highest().sd < ""; } if( b.empty() ) { return a.highest().sd < ""; } return a.highest() < b.highest(); }
void disconnect(node*a,node*b){ trp.erase(b->vir,a-&ns[0]); }
void connect(node*a,node*b){ down(a); *(&trp.ns[0]+(a-&ns[0]))=treap::node(a-&ns[0],min(a->virmi,a->mi),max(a->virmx,a->mx),a->virsum+a->sum,a->virsiz+a->siz); trp.insert(b->vir,&trp.ns[0]+(a-&ns[0])); }