1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| #include <cstdio> #include <cstring> #include <unordered_map> #define mod 1000000007 #define maxN 70 std::unordered_map<long long, std::unordered_map<long long, std::unordered_map<long long, long long> > > rem1; std::unordered_map<long long, std::unordered_map<long long, long long> > rem2; struct edge{ long long size, a, b, c, d, l; } f[maxN]; long long Ans[maxN]; long long to (long long T, long long x, long long y) { if(rem1[T][x][y]) { return rem1[T][x][y]; } long long mid = f[f[T].a].size; if(x == y) { return 0; } if(x > y) { long long t = x; x = y; y = t; } if(x <= mid && y <= mid) { return rem1[T][x][y] = to(f[T].a, x, y); } else if(x > mid && y > mid) { return rem1[T][x][y] = to(f[T].b, x - mid, y - mid); } else { return rem1[T][x][y] = (((to(f[T].a, x, f[T].c) + f[T].l) % mod) + to(f[T].b, y - mid, f[T].d)) % mod; } } long long all (long long T, long long x) { long long mid = f[f[T].a].size; if(rem2[T][x]) { return rem2[T][x]; } if(f[T].size == 1) { return 0; } if(x <= mid) { long long A = (f[f[T].b].size % mod) * (((f[T].l % mod) + to(f[T].a, x, f[T].c)) % mod) % mod; return rem2[T][x] = (((all(f[T].a, x) + all(f[T].b, f[T].d)) % mod) + A) % mod; } else { long long A = (f[f[T].a].size % mod) * (((f[T].l % mod) + to(f[T].b, x - mid, f[T].d)) % mod) % mod; return rem2[T][x] = ((all(f[T].b, x - mid) + all(f[T].a, f[T].c) % mod) + A) % mod; } } int main () { long long T = 0; scanf("%lld", &T); while(T--) { rem1.clear(); rem2.clear(); memset(f, 0, sizeof(f)); memset(Ans, 0, sizeof(Ans)); long long n = 0; scanf("%lld", &n); f[0].size = 1; for(long long i = 1;i <= n; i++) { long long A = 0, B = 0, C = 0, D = 0, L = 0; scanf("%lld %lld %lld %lld %lld", &A, &B, &C, &D, &L); C++, D++; f[i].a = A; f[i].b = B; f[i].c = C; f[i].d = D; f[i].l = L; f[i].size = f[A].size + f[B].size; long long AA = (Ans[A] + Ans[B]) % mod; long long BB = (f[A].size % mod) * (f[B].size % mod) % mod * (f[i].l % mod) % mod; long long CC = (f[A].size % mod) * all(B, D) % mod; long long DD = (f[B].size % mod) * all(A, C) % mod; Ans[i] = (((((AA + BB) % mod) + CC) % mod) + DD) % mod; printf("%lld\n", Ans[i]); } } return 0; }
|