zoukankan      html  css  js  c++  java
  • 杜教的AAA树

    膜膜膜,常数挺小的。。。

      1 #include<iostream>
      2 #include<cstdio>
      3 #include<cmath>
      4 #include<algorithm>
      5 #include<queue>
      6 #include<cstring>
      7 #define PAU putchar(' ')
      8 #define ENT putchar('
    ')
      9 #define rep(i,a,n) for(int i=a;i<n;i++)
     10 using namespace std;
     11 const int maxn=1e5+1000,inf=-1u>>1;
     12 int getint(){
     13     int res=0,f=1;char c=getchar();
     14     while(!isdigit(c))f=f==-1||c=='-'?-1:1,c=getchar();
     15     while(isdigit(c))res=res*10+c-'0',c=getchar();
     16     return res*f;
     17 }
     18 int n,m;
     19 struct info{
     20     int mx,mn,sum,siz;
     21     info(){}
     22     info(int mx,int mn,int sum,int siz):
     23         mx(mx),mn(mn),sum(sum),siz(siz){}
     24 };
     25 struct flag{
     26     int mul,add;
     27     flag(){mul=1;}
     28     flag(int mul,int add):
     29         mul(mul),add(add){}
     30     bool empty(){return mul==1&&add==0;}
     31 };
     32 info operator+(const info &a,const flag &b) {
     33     return a.siz?info(a.mx*b.mul+b.add,a.mn*b.mul+b.add,a.sum*b.mul+b.add*a.siz,a.siz):a;
     34 }
     35 info operator+(const info &a,const info &b) {
     36     return info(max(a.mx,b.mx),min(a.mn,b.mn),a.sum+b.sum,a.siz+b.siz);
     37 }
     38 flag operator+(const flag &a,const flag &b) {
     39     return flag(a.mul*b.mul,a.add*b.mul+b.add);
     40 }
     41 struct node{
     42     node *ch[4],*f;
     43     flag Cha,All;
     44     info cha,sub,all;
     45     bool rev,inr;
     46     int val;
     47     void revt(){rev^=1;swap(ch[0],ch[1]);}
     48     void makec(const flag &a){
     49         Cha=Cha+a;cha=cha+a;val=val*a.mul+a.add;
     50         all=cha+sub;
     51     }
     52     void makes(const flag &a,bool _=1){
     53         All=All+a;all=all+a;sub=sub+a;
     54         if(_)makec(a);
     55     }
     56     void update(){
     57         cha=all=sub=info(-inf,inf,0,0);
     58         if(!inr)all=cha=info(val,val,val,1);
     59         rep(i,0,2)if(ch[i])cha=cha+ch[i]->cha,sub=sub+ch[i]->sub;
     60         rep(i,0,4)if(ch[i])all=all+ch[i]->all;
     61         rep(i,2,4)if(ch[i])sub=sub+ch[i]->all;
     62     }
     63     void down(){
     64         if(rev){
     65             if(ch[0])ch[0]->revt();
     66             if(ch[1])ch[1]->revt();
     67             rev=0;
     68         }
     69         if(!All.empty()){
     70             rep(i,0,4)if(ch[i])ch[i]->makes(All,i>=2);
     71             All=flag(1,0);
     72         }
     73         if(!Cha.empty()){
     74             rep(i,0,2)if(ch[i])ch[i]->makec(Cha);
     75             Cha=flag(1,0);
     76         }
     77     }
     78     node *C(int i){if(ch[i])ch[i]->down();return ch[i];}
     79     bool d(int ty){return f->ch[ty+1]==this;}
     80     int D(){rep(i,0,4)if(f->ch[i]==this)return i;}
     81     void sets(node *x,int d){if(x)x->f=this;ch[d]=x;}
     82     bool rt(int ty){
     83         if(ty==0)return !f||(f->ch[0]!=this&&f->ch[1]!=this);
     84         else return !f||!f->inr||!inr;
     85     }
     86 }nd[maxn*2],*cur=nd+maxn,*pool[maxn],**Cur=pool;
     87 int _cnt;
     88 node *newnode(){
     89     _cnt++;
     90     node *x=(Cur==pool)?cur++:*(--Cur);
     91     rep(i,0,4)x->ch[i]=0;x->f=0;
     92     x->All=x->Cha=flag(1,0);
     93     x->all=x->cha=info(-inf,inf,0,0);
     94     x->inr=1;x->rev=0;x->val=0;
     95     return x;   
     96 }
     97 void dele(node *x){*(Cur++)=x;}
     98 void rot(node *x,int ty){
     99     node *p=x->f;int d=x->d(ty);
    100     if(!p->f)x->f=0;else p->f->sets(x,p->D());
    101     p->sets(x->ch[!d+ty],d+ty);x->sets(p,!d+ty);p->update();
    102 }
    103 void splay(node *x,int ty=0){
    104     while(!x->rt(ty)){
    105         if(x->f->rt(ty))rot(x,ty);
    106         else if(x->d(ty)==x->f->d(ty))rot(x->f,ty),rot(x,ty);
    107         else rot(x,ty),rot(x,ty);
    108     }x->update();
    109 }
    110 void add(node *u,node *w){
    111     w->down();
    112     rep(i,2,4)if(!w->ch[i]){w->sets(u,i);return;}
    113     node *x=newnode(),*v;
    114     for(v=w;v->ch[2]->inr;v=v->C(2));
    115     x->sets(v->ch[2],2);x->sets(u,3);
    116     v->sets(x,2);splay(x,2); 
    117 }
    118 void del(node *w){
    119     if(w->f->inr){
    120         w->f->f->sets(w->f->ch[5-w->D()],w->f->D());
    121         dele(w->f);splay(w->f->f,2);
    122     }else w->f->sets(0,w->D());
    123     w->f=0;
    124 }
    125 void access(node *w){
    126     static node *sta[maxn];
    127     static int top=0;
    128     node *v=w,*u;
    129     for(u=w;u;u=u->f)sta[top++]=u;
    130     while(top)sta[--top]->down();
    131     splay(w);
    132     if(w->ch[1])u=w->ch[1],w->ch[1]=0,add(u,w),w->update();
    133     while(w->f){
    134         for(u=w->f;u->inr;u=u->f);
    135         splay(u);
    136         if(u->ch[1])w->f->sets(u->ch[1],w->D()),splay(w->f,2);
    137         else del(w);
    138         u->sets(w,1);
    139         (w=u)->update();
    140     }splay(v);
    141 }
    142 void makert(node *x){
    143     access(x);x->revt();
    144 }
    145 node *findp(node *u){
    146     access(u);u=u->C(0);
    147     while(u&&u->ch[1])u=u->C(1);
    148     return u;
    149 }
    150 node *findr(node *u){for(;u->f;u=u->f);return u;}
    151 node* cut(node *u){
    152     node *v=findp(u);
    153     if(v)access(v),del(u),v->update();
    154     return v;
    155 }
    156 void link(node *u,node *v) {
    157     node* p=cut(u);
    158     if(findr(u)!=findr(v))p=v;
    159     if(p)access(p),add(u,p),p->update();
    160 }
    161 int main(){
    162     n=getint();m=getint();
    163     static int _u[maxn],_v[maxn];
    164     rep(i,1,n)_u[i]=getint(),_v[i]=getint();
    165     rep(i,1,n+1){
    166         nd[i].val=getint();
    167         nd[i].update();
    168     }
    169     rep(i,1,n)makert(nd+_u[i]),link(nd+_u[i],nd+_v[i]);
    170     int root=getint();
    171     makert(nd+root);
    172     int x,y,z;
    173     node *u,*v;
    174     while(m--){
    175         int k=getint();x=getint();
    176         u=nd+x;
    177         if(k==0||k==3||k==4||k==5||k==11){
    178             access(u);
    179             if(k==3||k==4||k==11){
    180                 int ans=u->val;
    181                 rep(i,2,4)if(u->ch[i]){
    182                     info res=u->ch[i]->all;                    
    183                     if(k==3) ans=min(ans,res.mn);
    184                     else if(k==4) ans=max(ans,res.mx);
    185                     else if(k==11) ans+=res.sum;
    186                 }printf("%d
    ",ans);
    187             }else{
    188                 y=getint();
    189                 flag fg(k==5,y);
    190                 u->val=u->val*fg.mul+fg.add;
    191                 rep(i,2,4)if(u->ch[i])u->ch[i]->makes(fg);
    192                 u->update();
    193             }           
    194         }else if(k==2||k==6||k==7||k==8||k==10){
    195             y=getint();
    196             makert(u),access(nd+y),splay(u);
    197             if (k==7||k==8||k==10) {
    198                 info ans=u->cha;
    199                 if (k==7) printf("%d
    ",ans.mn);
    200                 else if (k==8) printf("%d
    ",ans.mx);
    201                 else printf("%d
    ",ans.sum);
    202             }else u->makec(flag(k==6,getint())); 
    203             makert(nd+root);
    204         }else if(k==9)link(u,nd+getint());
    205         else if(k==1)makert(u),root=x;  
    206     }
    207     return 0;
    208 }
  • 相关阅读:
    【CentOS 7】关于php留言本网站的搭建
    linux系统的初化始配置(临时生效和永久生效)
    时间同步ntp服务的安装与配置(作为客户端的配置)
    CentOS 7设置服务的开机启动
    辅助模型——通信图
    一.面向对象概论
    辅助模型——包图
    构建图
    部署图
    辅助模型——状态机图
  • 原文地址:https://www.cnblogs.com/chxer/p/4575549.html
Copyright © 2011-2022 走看看