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 172 173 174
| #include <bits/stdc++.h> #define nmf(i, s, e) for (int i = s; i <= e; i++) #define ref(i, s, e) for (int i = s; i >= e; i--) using namespace std; typedef long long LL; typedef unsigned long long uLL; const int N = 1e5 + 5; int n, m; int a[N], ans[N]; vector<int> grh[N]; struct Query { int l, r, id; }; vector<Query> qrys[N]; bool vis[N]; namespace GetCentroid { int sz[N], root, centroid; void getsz(int u, int fa) { sz[u] = 1; for (int v : grh[u]) { if (v == fa || vis[v]) continue; getsz(v, u); sz[u] += sz[v]; } } void find(int u, int fa) { if (sz[u] * 2 >= sz[root]) centroid = u; else return; for (int v : grh[u]) { if (v == fa || vis[v]) continue; find(v, u); } } int getcentroid(int u) { root = u; centroid = 0; getsz(u, 0); find(u, 0); return centroid; } } using GetCentroid::getcentroid; class BIT { private: int tree[100005]; int sz;
public: void init(int n) { nmf(i, 1, n) tree[i] = 0; sz = n; } void upd(int x, int val) { while (x <= sz) { tree[x] += val; x += (x & -x); } } int query(int x) { if (x > sz) x = sz; int ret = 0; while (x > 0) { ret += tree[x]; x -= (x & -x); } return ret; } int query(int l, int r) { return query(r) - query(l - 1); } } tr; vector<Query> vecqrys; vector<tuple<int, int, int>> vec; int pos[N]; void dfs(int u, int fa, int l, int r) { vec.emplace_back(l, r, a[u]); for (auto it : qrys[u]) if (it.l <= l && r <= it.r) vecqrys.push_back(it); for (int v : grh[u]) { if (v == fa || vis[v]) continue; dfs(v, u, min(l, v), max(r, v)); } } void sol(int u) { vecqrys.clear(); vec.clear(); dfs(u, 0, u, u); sort(vecqrys.begin(), vecqrys.end(), [](const Query &x, const Query &y) { return x.r < y.r; }); sort(vec.begin(), vec.end(), [](const tuple<int, int, int> &x, const tuple<int, int, int> &y) { return get<1>(x) < get<1>(y); }); int j = 0; nmf(i, 0, (int)vecqrys.size() - 1) { while (j < (int)vec.size() && get<1>(vec[j]) <= vecqrys[i].r) { auto [l, r, col] = vec[j]; if (pos[col]) tr.upd(pos[col], -1); pos[col] = max(pos[col], l); tr.upd(pos[col], 1); j++; } ans[vecqrys[i].id] = max(ans[vecqrys[i].id], tr.query(vecqrys[i].l, 1e5)); } for (auto [_, __, col] : vec) { if (pos[col]) { tr.upd(pos[col], -1); pos[col] = 0; } } } void solve(int u) { int rt = getcentroid(u); vis[rt] = 1; sol(rt); for (auto v : grh[rt]) { if (!vis[v]) solve(v); } } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; nmf(i, 1, n) cin >> a[i]; nmf(i, 2, n) { int u, v; cin >> u >> v; grh[u].emplace_back(v); grh[v].emplace_back(u); } nmf(i, 1, m) { int l, r, x; cin >> l >> r >> x; qrys[x].push_back({l, r, i}); } tr.init(1e5); solve(1); nmf(i, 1, m) cout << ans[i] << '\n'; return 0; }
|