#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <set> #include <map> #include <array> #include <random> #include <cmath> #include <list> #include <ctime> #include <sstream> #include <queue> #include <climits> #include <stack> #include <random> #include <bitset> #include <numeric> #include <cassert> using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; typedef long long ll; #define rep(x, b, e) for(int x=(b); x<(e); ++x) #define trav(a, x) for(auto& a : x) #define ford(x, b, e) for(int x=((int)(b))-1; x>=(e); --x) #define all(c) c.begin(),c.end() #define sz(x) ((int)((x).size())) #define pb push_back #define st first #define nd second #define mp(x,y) make_pair(x,y) typedef short int sint; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " const int N = 1003003; const int mod= 1e9 + 7; const int div2 = 500000004; int V; vi za[N]; int ile[N]; int par[N]; int st[N], en[N]; int d1[N], d2[N]; void traverse(int vert, int parent, int* d) { trav(v, za[vert]) { if (v == parent) continue; d[v] = d[vert] + 1; traverse(v, vert, d); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, q; scanf("%d %d", &n, &q); vi a(n - 1); st[0] = 0; // za[0].pb(-1); V = 1; rep(i, 0, n-1) { scanf("%d", &a[i]); st[i + 1] = V; rep(v, st[i], st[i + 1]) { rep(_, 0, a[i]) { par[V] = v; // printf("za[%d].pb(%d)\n", v, V); za[V].pb(v); za[v].pb(V++); } } } st[n] = V; // rep(i, 0, n + 1) { // printf("st[%d] = %d\n", i, st[i]); // } rep(_, 0, q) { int w1, w2, wLCA; scanf("%d %d %d", &w1, &w2, &wLCA); // printf("q: %d, %d, %d\n", w1, w2, wLCA); --w1, --w2, --wLCA; int v1 = st[w1]; int vLCA = st[wLCA]; int v2; if (v1 == vLCA) { v2 = st[w2]; } else { if (w2 == wLCA) { v2 = st[w2]; } else { v2 = vLCA; v2 = za[vLCA][ vLCA == 0 ? 1 : 2 ]; rep(h, wLCA + 1, w2) { v2 = za[v2][1]; } } } // printf("%d<->%d<->%d\n", v1, vLCA, v2); d1[v1] = 0; traverse(v1, -1, d1); d2[v2] = 0; traverse(v2, -1, d2); map<int, vi> infos; ll D1 = 0, D2 = 0; rep(i, 0, V) { // printf("%d: d1= %d, d2= %d\n", i, d1[i], d2[i]); infos[d1[i] + d2[i]].pb(d1[i]); D1 += d1[i]; D2 += d2[i]; // printf("adding: a+b(%d) with (a)%d\n", d1[i] + d2[i], d1[i]); } // go from biggest auto it = infos.rbegin(); int kt = 1; ll diff = 0; while (it != infos.rend()) { // printf("looking at sum: %d\n", it->st); vi zb = it->nd; if ((sz(zb) % 2) == 1) { diff += (kt * it->st); kt *= -1; } it++; } ll pom = ((((D1 - D2 + diff ) % mod + mod) % mod) * div2) % mod; printf("%lld\n", pom); } }
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <set> #include <map> #include <array> #include <random> #include <cmath> #include <list> #include <ctime> #include <sstream> #include <queue> #include <climits> #include <stack> #include <random> #include <bitset> #include <numeric> #include <cassert> using namespace std; typedef vector<int> vi; typedef pair<int,int> pii; typedef long long ll; #define rep(x, b, e) for(int x=(b); x<(e); ++x) #define trav(a, x) for(auto& a : x) #define ford(x, b, e) for(int x=((int)(b))-1; x>=(e); --x) #define all(c) c.begin(),c.end() #define sz(x) ((int)((x).size())) #define pb push_back #define st first #define nd second #define mp(x,y) make_pair(x,y) typedef short int sint; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " const int N = 1003003; const int mod= 1e9 + 7; const int div2 = 500000004; int V; vi za[N]; int ile[N]; int par[N]; int st[N], en[N]; int d1[N], d2[N]; void traverse(int vert, int parent, int* d) { trav(v, za[vert]) { if (v == parent) continue; d[v] = d[vert] + 1; traverse(v, vert, d); } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, q; scanf("%d %d", &n, &q); vi a(n - 1); st[0] = 0; // za[0].pb(-1); V = 1; rep(i, 0, n-1) { scanf("%d", &a[i]); st[i + 1] = V; rep(v, st[i], st[i + 1]) { rep(_, 0, a[i]) { par[V] = v; // printf("za[%d].pb(%d)\n", v, V); za[V].pb(v); za[v].pb(V++); } } } st[n] = V; // rep(i, 0, n + 1) { // printf("st[%d] = %d\n", i, st[i]); // } rep(_, 0, q) { int w1, w2, wLCA; scanf("%d %d %d", &w1, &w2, &wLCA); // printf("q: %d, %d, %d\n", w1, w2, wLCA); --w1, --w2, --wLCA; int v1 = st[w1]; int vLCA = st[wLCA]; int v2; if (v1 == vLCA) { v2 = st[w2]; } else { if (w2 == wLCA) { v2 = st[w2]; } else { v2 = vLCA; v2 = za[vLCA][ vLCA == 0 ? 1 : 2 ]; rep(h, wLCA + 1, w2) { v2 = za[v2][1]; } } } // printf("%d<->%d<->%d\n", v1, vLCA, v2); d1[v1] = 0; traverse(v1, -1, d1); d2[v2] = 0; traverse(v2, -1, d2); map<int, vi> infos; ll D1 = 0, D2 = 0; rep(i, 0, V) { // printf("%d: d1= %d, d2= %d\n", i, d1[i], d2[i]); infos[d1[i] + d2[i]].pb(d1[i]); D1 += d1[i]; D2 += d2[i]; // printf("adding: a+b(%d) with (a)%d\n", d1[i] + d2[i], d1[i]); } // go from biggest auto it = infos.rbegin(); int kt = 1; ll diff = 0; while (it != infos.rend()) { // printf("looking at sum: %d\n", it->st); vi zb = it->nd; if ((sz(zb) % 2) == 1) { diff += (kt * it->st); kt *= -1; } it++; } ll pom = ((((D1 - D2 + diff ) % mod + mod) % mod) * div2) % mod; printf("%lld\n", pom); } } |