Memory recursive search, note that the size of the tree may be large (2m), with 64-bit integers also need to calculate the edge of the edge modulo to prevent overflow.
http://acm.hdu.edu.cn/showproblem.php?pid=5401
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <map>5 6 using namespacestd;7 typedef __int64 LL;8 9 ConstLL M = 1e9 +7;Ten Const intMAXN = -; One intm; A LL A, B, C, D, l; - LL L[MAXN], R[MAXN]; - LL F[MAXN], L1[MAXN]; the LL G[MAXN]; - intA[MAXN], B[MAXN]; - LL MS[MAXN]; - +Map<ll, ll>MAP1[MAXN]; -Map<pair<ll, Ll>, ll>MAP2[MAXN]; + A ll Dis (ll tr, ll X, ll y) { at if(!tr | | x = = y)return 0; - if(X >y) Swap (x, y); - if(Map2[tr].count (Make_pair (x, y)))returnMap2[tr][make_pair (x, y)]; - if(Y < g[a[tr]])returnMap2[tr][make_pair (x, y)] =dis (a[tr], x, y); - if(x >= g[a[tr])returnMap2[tr][make_pair (x, y)] = dis (B[TR], x-g[a[tr]], y-G[A[TR]]); - returnMap2[tr][make_pair (x, y)] = (DIS (a[tr], X, l[tr]) + DIS (b[tr], y-g[a[tr] [r[tr]) + l1[tr])%M; in } - toLLGet(ll X, ll tr) { + if(!TR)return 0; - if(Map1[tr].count (x))returnMap1[tr][x]; the if(X <G[a[tr]]) * returnMAP1[TR][X] = $(Get(x, a[tr]) +Get(R[tr], b[tr]) + MS[B[TR]] * (L1[TR] + dis (a[tr], l[tr], x)))%M;Panax Notoginseng returnMAP1[TR][X] = -(Get(X-g[a[tr]], B[TR]) +Get(L[tr], a[tr]) + MS[A[TR]] * (L1[TR] + dis (b[tr], r[tr], x-g[a[tr])))%M; the } + A voidUpdateintu) { theL1[u] =l; +L[u] = c, r[u] =D; -A[u] = A, b[u] =b; $G[u] = G[a] +G[b]; $Ms[u] = G[u]%M; -LL ans =0; -Ans + = (ms[a] * ms[b])% M * L + f[a] +F[b]; theAns%=M; -LL LHS =Get(c, a);WuyiLL RHS =Get(d, b); theAns + = (ms[a] * RHS + ms[b] * lhs)%M; -Ans%=M; Wuprintf"%i64d\n", ans); -F[u] =ans; About } $ - intMain () { - //freopen ("In.txt", "R", stdin); - while(~SCANF ("%d", &m)) { A for(inti =0; I <= m; i++) map1[i].clear (); + for(inti =0; I <= m; i++) map2[i].clear (); thems[0] = g[0] =1; -f[0] =0; $ for(inti =1; I <= m; i++) scanf ("%i64d%i64d%i64d%i64d%i64d", &a, &b, &c, &d, &l), update (i); the } the return 0; the}
View Code
hdu5401 Persistent Link/cut Tree