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 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; }
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; }