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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
| const int maxn = 200200; int n, m, N; int a[maxn], buf[maxn];
// == Graph structure == int head[maxn], nxt[maxn], ver[maxn]; int tot = 0; void init() { Set(head, 0); Set(nxt, 0); Set(ver, 0); tot = 0; }
void add(int x, int y) { ver[++tot] = y; nxt[tot] = head[x]; head[x] = tot; } // == Graph structure finished ==
// == discrete == void discrete() { sort(buf + 1, buf + 1 + n); N = unique(buf + 1, buf + 1 + n) - buf - 1; _rep(i, 1, n) a[i] = lower_bound(buf + 1, buf + 1 + N, a[i]) - buf; } // == dicrete finsihed ==
// == dfs order and lca == int f[maxn][30], dep[maxn]; int h = 0;
int fst[maxn], lst[maxn]; int ord[maxn], dfsn; void initdfs() { Set(dep, 0); Set(fst, 0); Set(lst, 0); Set(ord, 0); dfsn = 0;
dep[1] = 1; h = 20 + 5; }
void dfs(int x) { assert(dep[1] == 1); ord[++dfsn] = x; fst[x] = dfsn; for(int i = head[x]; i; i = nxt[i]) { int y = ver[i]; if(dep[y]) continue;
dep[y] = dep[x] + 1; f[y][0] = x; _rep(k, 1, h) f[y][k] = f[f[y][k - 1]][k - 1]; dfs(y); } ord[++dfsn] = x; lst[x] = dfsn; }
int LCA(int x, int y) { if(dep[x] > dep[y]) swap(x, y); _forDown(i, h, 0) if(dep[f[y][i]] >= dep[x]) y = f[y][i];
if(x == y) return x;
_forDown(i, h, 0) if(f[y][i] != f[x][i]) { x = f[x][i], y = f[y][i]; } return f[x][0]; } // == dfs order and lca finshed ==
// == query and block == class Qry { public: int l, r, lca, id; }; Qry qry[maxn];
int belong[maxn]; int sz, t;
bool cmp(const Qry& a, const Qry& b) { if(belong[a.l] ^ belong[b.l]) return belong[a.l] < belong[b.l]; if(belong[a.l] & 1) return a.r < b.r; return a.r > b.r; }
// [1, dfsn] void block() { sz = sqrt(dfsn); t = dfsn / sz; _rep(i, 1, t) _rep(k, (i - 1) * sz + 1, i * sz) belong[k] = i; if(t * sz < n) { t++; _rep(k, (t - 1) * sz + 1, n) belong[k] = t; } }
// == query and block finsihed ==
// == Mo algorithm == int CNT[maxn]; int visMo[maxn];
void initMo() { Set(CNT, 0); Set(visMo, 0); }
inline void work(int pos, int& ans) { if(visMo[pos] == 0) if(++CNT[a[pos]] == 1) ans++; if(visMo[pos]) if(--CNT[a[pos]] == 0) ans--; visMo[pos] ^= 1; }
int ANS[maxn]; int l = 1, r = 0, res = 0; void solve() { sort(qry + 1, qry + 1 + m, cmp);
_rep(i, 1, m) { int ql = qry[i].l, qr = qry[i].r, qlca = qry[i].lca; // printf("%d %d\n", ql, qr); while (l < ql) work(ord[l++], res); while (l > ql) work(ord[--l], res); while (r < qr) work(ord[++r], res); while (r > qr) work(ord[r--], res);
if(qlca) work(qlca, res); ANS[qry[i].id] = res; if(qlca) work(qlca, res); } } // == Mo;s algo finished ==
int main() { freopen("input.txt", "r", stdin); init();
// == input data == scanf("%d%d", &n, &m); _rep(i, 1, n) { scanf("%d", &a[i]); buf[i] = a[i]; } _for(i, 1, n) { int x, y; scanf("%d%d", &x, &y); add(x, y); add(y, x); } // == input finished ==
// == discrete == discrete(); // == discrete finished ==
// == get dfs order and lca == initdfs(); dfs(1); // == dfs order finished
// == block query ==
// == block query finished == // == check the arr ord[] block();
_rep(i, 1, m) { int l, r; scanf("%d%d", &l, &r); int lca = LCA(l, r); //debug(lca);
if(fst[l] > fst[r]) swap(l, r); qry[i].id = i;
if(l == lca) { qry[i].l = fst[l]; qry[i].r = fst[r]; } else { qry[i].l = lst[l]; qry[i].r = fst[r]; qry[i].lca = lca; } } // == Mo algorithm ==
initMo(); solve();
_rep(i, 1, m) printf("%d\n", ANS[i]); }
|