Fzu 2082 Tolls

Source: Internet
Author: User

Tree chain split template problem

1#include <cstdio>2#include <iostream>3#include <cstring>4#include <algorithm>5 using namespacestd;6 #defineN 500107 #defineLS o<<18 #defineRS O<<1|19 #definedefine_m int m= (L+R) >>1Ten #definell Long Long One intFirst[n], K; A structedge{ -     intx, Y, Next, W; -}e[n<<1]; the  - voidAdd_edge (intXintYintW) - { -e[k].x = x, e[k].y = y, E[k].next = first[x], E[K].W =W; +FIRST[X] = k++; - } +  A intSz[n], dep[n], fa[n], son[n], top[n], id[n], num; at voidDfsintU,intFintd) - { -Fa[u] = f, sz[u] =1, dep[u]= D, son[u]=0; -     intMX =0; -      for(intI=first[u]; ~i; I=E[i].next) { -         intv =e[i].y; in         if(v = = f)Continue; -DFS (V, u, d+1); tosz[u]+=Sz[v]; +         if(SZ[V]&GT;MX) mx=sz[v], son[u]=v; -     } the } *  $ voidDFS1 (intU,intFinthead)Panax Notoginseng { -Top[u]=head, id[u]=++num; the     if(Son[u]) DFS1 (Son[u], u, head); +      for(intI=first[u]; ~i; I=E[i].next) { A         intv =e[i].y; the         if(v = = F | | v = = Son[u])Continue; + DFS1 (V, U, v); -     } $ } $  -ll sum[n<<2] ; - intVal[n]; the voidPUSH_UP (into) - {WuyiSum[o] = sum[ls]+Sum[rs]; the } -  Wu voidBuildintOintLintR) - { About     if(l==R) { $sum[o]=(LL) val[l]; -         return ; -     } - define_m; A build (LS, L, m); +Build (RS, m+1, R); the push_up (o); - } $  the voidUpdateintOintLintRintPintv) the { the     if(l==R) { thesum[o]=(ll) v; -         return; in     } the define_m; the     if(m>=p) Update (LS, L, M, p, v); About     ElseUpdate (RS, m+1, R, p, v); the push_up (o); the } the  +ll Qsum (intOintLintRintS,intt) - { the     if(L>=s && r<=t)returnSum[o];Bayi define_m; thell res =0; the     if(m>=s) res+=qsum (LS, L, m, S, t); -     if(m<t) Res+=qsum (RS, m+1, R, S, T); -     returnRes; the } the  thell Calpath (intU,intv) the { -     intTOP1 = Top[u], TOP2 =Top[v]; thell ret=0; the      while(top1!=top2) { the         if(dep[top1]<DEP[TOP2]) {94 swap (TOP1, TOP2); the swap (U, v); the         } theRet+=qsum (1,1, NUM, Id[top1], id[u]);98U =FA[TOP1]; AboutTOP1 =Top[u]; -     }101     if(u!=v) {102         if(dep[u]<Dep[v]) swap (U,V);103Ret+=qsum (1,1, Num,id[son[v]],id[u]);104     } the     returnret;106 }107 108 intMain ()109 { the   //freopen ("In.txt", "R", stdin);111     intN, m, x, Y, W; the      while(SCANF ("%d%d", &n, &m)! =EOF)113     { thememset (First,-1,sizeof(first)); theK =0; the          for(intI=0; i<n-1; i++){117scanf"%d%d%d", &x, &y, &W);118 Add_edge (x, Y, W);119 Add_edge (y, X, W); -         }121Dfs1,0,1);122num =0;123DFS1 (1,0,1);124          for(intI=0; i<n-1; i++){ thex = e[i*2].x, y = e[i*2].y;126             if(fa[x]!=y) val[id[y]] = e[i*2].W;127             ElseVAL[ID[X]] = e[i*2].W; -         }129Build1,1, num); the          while(m--){131scanf"%d%d%d", &w, &x, &y); the             if(w) {133printf"%i64d\n", Calpath (x, y));134}Else{135x--;136                 intPos;137                 if(fa[e[x*2].x]!=e[x*2].Y) pos = id[e[x*2].Y];138                 Elsepos = id[e[x*2].x];139Update1,1, num, pos, y); $             }141         }142     }143     return 0;144}

Fzu 2082 Tolls

Related Keywords:
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.