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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
| #include <bits/stdc++.h>
using namespace std;
typedef long long ll; const int N = 5e5 + 10, mod = 1e9 + 7; int root[N], tot = 0, num[N], len, a[N]; struct Info { int sum, l, r; } info[N << 5];
int getid(int x) { return lower_bound(num + 1, num + len + 1, x) - num; }
void build(int &x, int l, int r) { x = ++tot; info[x].sum = 0; if (l == r) return; int mid = (l + r) / 2; build(info[x].l, l, mid); build(info[x].r, mid + 1, r); }
void update(int pre, int &now, int l, int r, int q, int x) { now = ++tot; info[now] = info[pre]; info[now].sum += x; info[now].sum %= mod; if (l == r) return; int mid = (l + r) / 2; if (mid >= q) update(info[pre].l, info[now].l, l, mid, q, x); else update(info[pre].r, info[now].r, mid + 1, r, q, x); }
int query_sum(int pre, int now, int l, int r, int k) { if (l == r) return (info[now].sum - info[pre].sum + mod) % mod; int mid = (l + r) >> 1; if (k <= mid) return query_sum(info[pre].l, info[now].l, l, mid, k); else return ((info[info[now].l].sum - info[info[pre].l].sum + mod) % mod + query_sum(info[pre].r, info[now].r, mid + 1, r, k)) % mod; }
vector<int> e[N]; int lu[N], ru[N], id[N], pid, sz[N], ans, n, inv2 = 500000004, prex[N];
void dfs(int u, int fa){ lu[u] = ++pid; id[pid] = u; sz[u] = 1; for(int i : e[u]){ if(i == fa) continue; dfs(i, u); sz[u] += sz[i]; } ru[u] = pid; }
template <typename T> struct Fenwick { const int n; vector<T> a; Fenwick(int n) : n(n), a(n + 1) {} void add(int x, T v) { while(x <= n){ a[x] += v; a[x] %= mod; x += x & -x; } } T sum(int x) { T ans = 0; for (int i = x; i; i -= i & -i) { ans += a[i]; ans %= mod; } return ans; } T rangeSum(int l, int r) { return (sum(r) - sum(l - 1) + mod) % mod; } };
Fenwick<int> fsum(N), fcnt(N), fdp(N);
void run(int u, int fa){ int pos = getid(a[u]), cnt = fcnt.sum(pos); ll sum = 0, sum1 = ((1ll * cnt * n % mod - fdp.sum(pos)) + mod) % mod * sz[u] % mod; ll sum2 = prex[pos] - query_sum(root[lu[u] - 1], root[ru[u]], 1, len, pos); sum2 -= fsum.sum(pos); sum2 %= mod; if(sum2 < 0) sum2 += mod; sum2 = sum2 * sz[u] % mod; ll sum3 = 1ll * (sz[u] - 1) * (sz[u] - 2) % mod * inv2 % mod, sum4 = 0; sum3 = (sum3 + 1ll * (n - sz[u] + 1) * sz[u] % mod) % mod; for(int i : e[u]){ if(i == fa) continue; sum4 += 1ll * (n - sz[i]) * query_sum(root[lu[i] - 1], root[ru[i]], 1, len, pos); sum4 %= mod; sum3 -= 1ll * (sz[i] - 1) * sz[i] % mod * inv2 % mod; sum3 %= mod; if(sum3 < 0) sum3 += mod; } sum = (sum1 + sum2 + sum3 + sum4) % mod; sum = sum * a[u] % mod; ans = (ans + sum) % mod; fcnt.add(pos, 1); fsum.add(pos, sz[u]); for(int i : e[u]){ if(i == fa) continue; fdp.add(pos, sz[i]); run(i, u); fdp.add(pos, -sz[i]); } fsum.add(pos, -sz[u]); fcnt.add(pos, -1); }
void solve() { cin >> n; for(int i = 1; i <= n; i++) cin >> a[i], num[i] = a[i]; for(int i = 0, u, v; i < n - 1; i++){ cin >> u >> v; e[u].push_back(v); e[v].push_back(u); } dfs(1, 0); sort(num + 1, num + 1 + n); len = unique(num + 1, num + 1 + n) - num - 1; build(root[0], 1, len); for(int i = 1; i <= pid; i++){ int p = getid(a[id[i]]); update(root[i - 1], root[i], 1, len, p, sz[id[i]]); prex[p] = sz[id[i]]; } for(int i = 1; i <= n; i++){ prex[i] += prex[i - 1]; prex[i] %= mod; } run(1, 0); cout << ans; }
int main() { int T = 1; ios::sync_with_stdio(false); cin.tie(nullptr); while (T--) solve(); return 0; }
|