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 96 97 98 99 100 101 102 103 104
| #include <bits/stdc++.h> using namespace std; template <typename T> inline void read(T &a){ T w=1; a=0; char ch=getchar(); for(;ch < '0' || ch > '9';ch=getchar()) if(ch == '-') w=-1; for(;ch>='0'&&ch<='9';ch=getchar()) a=(a<<3)+(a<<1)+(ch^48); a*=w; } template <typename T> inline void ckmax(T &a, T b){a = a > b ? a : b;} template <typename T> inline void ckmin(T &a, T b){a = a < b ? a : b;} #define fi first #define se second #define pb push_back #define mp make_pair #define mii map<int, int> #define pii pair<int, int> #define vi vector<int> #define Debug(x) cout << #x << " = " << x << endl #define For(i,l,r) for (int i = l; i <= r; ++i) #define foR(i,l,r) for (int i = l; i >= r; --i) const int M = 66; const int Mod = 1e9 + 7; #define int __int128 inline int Add (int x, int y) { return (x + y) >= Mod ? (x + y - Mod) : (x + y); } inline int Mul (int x, int y) { return (1ll * x * y >= Mod) ? (1ll * x * y % Mod) : (x * y); } #define ll long long struct Tree { ll a, b, v; ll ans, lid, rid; ll siz; } tr[100]; #define lsiz tr[tr[id].lid].siz
map < pair<ll, pii> , ll> mckc;
int Dis (int id, int u, int v) { if (u == v) return 0; if (id == 0) return 0; if (u > v) swap(u, v); if (mckc[mp(id, mp(u, v))]) return mckc[mp(id, mp(u, v))]; if (v <= lsiz) return mckc[mp(id, mp(u, v))] = Dis(tr[id].lid, u, v); if (u > lsiz) return mckc[mp(id, mp(u, v))] = Dis(tr[id].rid, u - lsiz, v - lsiz); return mckc[mp(id, mp(u, v))] = Add(Add(Dis(tr[id].lid, tr[id].a, u), tr[id].v), Dis(tr[id].rid, tr[id].b - lsiz, v - lsiz)); }
map < pii, ll > mck;
int sum (int id, int x) { if (mck[mp(id, x)]) return mck[mp(id, x)] ; if (id == 0) return 0; if (x <= lsiz) { int res = sum(tr[id].rid, tr[id].b - lsiz); res = Add(res, Mul(tr[tr[id].rid].siz, tr[id].v)); res = Add(res, Mul(tr[tr[id].rid].siz, Dis(tr[id].lid, tr[id].a, x))); return mck[mp(id, x)] = Add(res, sum(tr[id].lid, x)); } else { int res = sum(tr[id].lid, tr[id].a); res = Add(res, Mul(tr[tr[id].lid].siz, tr[id].v)); res = Add(res, Mul(tr[tr[id].lid].siz, Dis(tr[id].rid, tr[id].b - lsiz, x - lsiz))); return mck[mp(id, x)] = Add(res, sum(tr[id].rid, x - lsiz)); } }
inline void write(int x){ if (x < 0) x = ~x + 1, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); }
inline void merge (int id, int id1, int a, int id2, int b, int v) { tr[id].ans = Add(tr[id1].ans, tr[id2].ans); tr[id].ans = Add(tr[id].ans, Mul(tr[id1].siz, sum(id2, b))); tr[id].ans = Add(tr[id].ans, Mul(tr[id2].siz, sum(id1, a))); tr[id].ans = Add(tr[id].ans, Mul(tr[id1].siz, Mul(tr[id2].siz, v))); tr[id].siz = tr[id1].siz + tr[id2].siz; tr[id].a = a, tr[id].b = b + tr[id1].siz; tr[id].v = v; tr[id].lid = id1, tr[id].rid = id2; write(tr[id].ans); putchar('\n'); }
signed main() { int cases; read(cases); while (cases--) { int m; read(m); tr[0].siz = 1; For (i, 1, m) { int a, b, c, d, v; read(a); read(b); read(c); read(d); read(v); c++, d++; merge(i, a, c, b, d, v); } mck.clear(); mckc.clear(); For (i, 0, m) tr[i].siz = tr[i].a = tr[i].b = tr[i].lid = tr[i].rid = tr[i].v = 0; } }
|