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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| #include <bits/stdc++.h> using namespace std; typedef long long LL; #define FOR(i, x, y) for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i) #define FORD(i, x, y) for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i) #ifdef zerol #define dbg(args...) do { cout << "\033[32;1m" << #args<< " -> "; err(args); } while (0) #else #define dbg(...) #endif void err() { cout << "\033[39;0m" << endl; } template<typename T, typename... Args> void err(T a, Args... args) { cout << a << ' '; err(args...); } // ----------------------------------------------------------------------------- const int maxn = 1E5 + 10; const int M = 300; const int B = 334; int son[maxn], sz[maxn], fa[maxn], idx[maxn], dep[maxn], top[maxn]; int w[maxn], a[maxn], c[maxn][B + 2]; vector<int> G[maxn]; int clk;
void predfs(int u, int d) { dep[u] = d; int& maxs = son[u] = -1; sz[u] = 1; for (int v: G[u]) if (v != fa[u]) { fa[v] = u; predfs(v, d + 1); sz[u] += sz[v]; if (maxs == -1 || son[maxs] < son[v]) maxs = v; } }
void dfs(int u, int tp) { top[u] = tp; idx[u] = ++clk; w[clk - 1] = a[u]; if (son[u] == -1) return; dfs(son[u], tp); for (int v: G[u]) if (v != fa[u] && v != son[u]) dfs(v, v); }
inline int sum(int l, int r, int bl, int br) { l--; r--; bl--; br--; int ret = c[r][br]; if (l) ret -= c[l - 1][br]; if (bl) ret -= c[r][bl - 1]; if (l && bl) ret += c[l - 1][bl - 1]; return ret; }
struct P { int l, r; };
typedef vector<P> VI; void divide(int l, int r, VI& b, VI& t) { int x = l / M, y = r / M; if (x == y) t.push_back({l, r}); else { t.push_back({l, (x + 1) * M - 1}); t.push_back({y * M, r}); if (x + 1 <= y - 1) b.push_back({x + 1, y - 1}); } }
void query(int u, int v, VI& b, VI& t) { b.clear(); t.clear(); int uu = top[u], vv = top[v]; while (uu != vv) { if (dep[uu] < dep[vv]) { swap(uu, vv); swap(u, v); } divide(idx[uu], idx[u], b, t); u = fa[uu]; uu = top[u]; }; if (dep[u] < dep[v]) { swap(uu, vv); swap(u, v); } divide(idx[v], idx[u], b, t); }
int go(VI& b, VI& t, VI& bb, VI& tt) { int ans = 0; for (P& p: b) for (P& q: bb) { int l = p.l * M, r = (p.r + 1) * M - 1; ans += sum(l, r, q.l, q.r); } for (P& p: b) for (P& q: tt) ans += sum(q.l, q.r, p.l, p.r); for (P& p: bb) for (P& q: t) ans += sum(q.l, q.r, p.l, p.r); static int cnt[maxn]; memset(cnt, 0, sizeof cnt); for (P& p: t) FOR (i, p.l, p.r + 1) cnt[w[i - 1]]++; for (P& p: tt) FOR (i, p.l, p.r + 1) ans += cnt[w[i - 1]]; return ans; }
int lca(int u, int v) { int uu = top[u], vv = top[v]; while (uu != vv) { if (dep[uu] < dep[vv]) { swap(u, v); swap(uu, vv); } u = fa[uu]; uu = top[u]; } if (dep[u] < dep[v]) return u; else return v; }
int intersection(int x, int y, int xx, int yy) { int t[4] = {lca(x, xx), lca(x, yy), lca(y, xx), lca(y, yy)}; sort(t, t + 4); int r = lca(x, y), rr = lca(xx, yy); if (dep[t[0]] < min(dep[r], dep[rr]) || dep[t[2]] < max(dep[r], dep[rr])) return 0; int tt = lca(t[2], t[3]); int ret = 1 + dep[t[2]] + dep[t[3]] - dep[tt] * 2; return ret; }
int cnt[maxn]; int main() { int n, Q, u, v, ttt = 0; map<int, int> mp; cin >> n >> Q; FOR (i, 1, n + 1) { scanf("%d", &a[i]); auto it = mp.find(a[i]); if (it == mp.end()) a[i] = mp[a[i]] = ++ttt; else a[i] = it->second; } FOR (i, 1, n) { scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); } predfs(1, 1); dfs(1, 1); FOR (b, 0, B) { memset(cnt, 0, sizeof cnt); FOR (i, b * M, (b + 1) * M) if (i >= n) break; else cnt[w[i]]++; FOR (i, 0, n) c[i][b] = cnt[w[i]]; } FOR (i, 0, n) FOR (b, 0, B) { int& t = c[i][b]; if (i) t += c[i - 1][b]; if (b) t += c[i][b - 1]; if (i && b) t -= c[i - 1][b - 1]; } while (Q--) { static int x, y, xx, yy; scanf("%d%d%d%d", &x, &y, &xx, &yy); VI b, t, bb, tt; query(x, y, b, t); query(xx, yy, bb, tt); printf("%d\n", go(b, t, bb, tt) - intersection(x, y, xx, yy)); } }
|