hdu5401 Persistent Link/cut Tree

Source: Internet
Author: User

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

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.