Tree chain split Edge update, Segment tree single point update, interval query
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <cstdlib>5 using namespacestd;6 #defineLson l,m,rt<<17 #defineRson m+1,r,rt<<1|18 Const intMAXN =100005;9 intsiz[maxn],son[maxn],fa[maxn],dep[maxn],top[maxn],tid[maxn],lable;Ten inthead[maxn],cnt; One structEdge A { - intV,w,en,next; -}e[maxn*2]; the intn,m,s; - intsum[maxn<<2]; - voidInit () - { +memset (head,-1,sizeof(head)); -CNT = lable =0; + } A voidAddintUintVintW) at { -E[CNT].V =v; -E[CNT].W =W; -E[cnt].next =Head[u]; -Head[u] = cnt++; - } in voidFind_heavy (intRtintFatherintdepth) - { to //printf ("rt=%d fa=%d \ n", rt,father); +FA[RT] =father; -SON[RT] =0; theDEP[RT] =depth; *SIZ[RT] =1; $ intMaxSize =0;Panax Notoginseng for(inti = head[rt];i!=-1; i = e[i].next)if(e[i].v!=father) - { theE[i].en = e[i^1].en =e[i].v; +Find_heavy (e[i].v,rt,depth+1); Asiz[rt]+=SIZ[E[I].V]; the if(siz[e[i].v]>maxsize) +MaxSize = Siz[e[i].v],son[rt] =e[i].v; - } $ } $ voidConnectintRtintANC) - { -TID[RT] = + +lable; theTOP[RT] =ANC; - if(Son[rt]) connect (SON[RT],ANC);Wuyi for(inti = head[rt];i!=-1; i =e[i].next) the if(e[i].v!=fa[rt]&&e[i].v!=Son[rt]) - Connect (E[I].V,E[I].V); Wu } - voidPushup (intRT) About { $SUM[RT] = sum[rt<<1]+sum[rt<<1|1]; - } - voidUpdateintPosintValintLintRintRT) - { A if(l==R) { +SUM[RT] =Val; the return; - } $ intm = (l+r) >>1; the if(pos<=m) update (Pos,val,lson); the ElseUpdate (Pos,val,rson); the pushup (RT); the } - intQueryintLintRintLintRintRT) in { the if(L<=L&&R<=R)returnSum[rt]; the intm = (l+r) >>1, ret =0; About if(l<=m) ret+=query (L,r,lson); the if(M<r) ret+=query (L,r,rson); the returnret; the } + intGetsum (intXinty) - { the intAns =0;Bayi while(top[x]!=Top[y]) the { the if(dep[top[x]]<Dep[top[y]]) swap (x, y); -Ans+=query (Tid[top[x]],tid[x],1N1); -x =Fa[top[x]]; the } the if(dep[x]>Dep[y]) swap (x, y); the if(x==y)returnans; theAns+=query (Tid[son[x]],tid[y],1N1); - returnans; the } the intMain () the {94 while(~SCANF ("%d%d%d",&n,&m,&s)) the { the init (); the for(inti =1; i<n;++i)98 { About intU,V,W;SCANF ("%d%d%d",&u,&v,&W); - Add (u,v,w);101 Add (v,u,w);102 }103 //for (int i = 1;i<=n;++i) {104 //printf ("i=%d:", i); the //For (int j = head[i];j!=-1;j = E[j].next)106 // {107 //printf ("%d", e[j].v);108 //System ("pause");109 // } the //puts ("");111 // } theFind_heavy (1,1,1);//puts ("----------");113Connect1,1); the the for(inti =0; i<cnt;i+=2) Update (TID[E[I].EN],E[I].W,1N1); the while(m--)117 {118 intA,B,C;SCANF ("%d",&a);119 if(a) { -scanf"%d%d",&b,&c);121Update (tid[e[2*b-1].en],c,1N1);122 }123 Else {124scanf"%d",&b); theprintf"%d\n", Getsum (s,b));126s =b;127 } - }129 } the return 0;131}
POJ2763 Housewife Wind