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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
#include <cstdio> #include <cctype> #include <vector> #include <algorithm>
typedef long long LL; typedef std::pair<int, int> pii; typedef struct Node* ptr;
#define fir first #define sec second #define debug(...) fprintf(stderr, __VA_ARGS__) #define Debug(s) debug("The message in line %d, Function %s: %s\n", __LINE__, __FUNCTION__, s) #define getchar() (ipos == iend and (iend = (ipos = _ibuf) + fread(_ibuf, 1, __bufsize, stdin), ipos == iend) ? EOF : *ipos++) #define putchar(ch) (opos == oend ? fwrite(_obuf, 1, __bufsize, stdout), opos = _obuf : 0, *opos++ = (ch)) #define __bufsize (1 << 21 | 1)
char _ibuf[__bufsize], _obuf[__bufsize], _stk[50]; char *ipos = _ibuf, *iend = _ibuf, *opos = _obuf, *oend = _obuf + __bufsize, *stkpos = _stk;
struct END { ~END() { fwrite(_obuf, 1, opos - _obuf, stdout); } } __;
inline int read() { register int x = 0, flag = 1; register char ch; while (!isdigit(ch = getchar())) if (ch == '-') flag = -1; while (x = (x << 3) + (x << 1) + (ch & 15), isdigit(ch = getchar())); return x * flag; }
template <typename _INT> inline void write(_INT x) { if (x < 0) putchar('-'), x = -x; while (*++stkpos = x % 10 ^ 48, x /= 10, x); while (stkpos != _stk) putchar(*stkpos--); }
template <typename _Tp> inline bool Chkmax(_Tp& x, const _Tp& y) { return x < y ? x = y, true : false; }
template <typename _Tp> inline bool Chkmin(_Tp& x, const _Tp& y) { return x > y ? x = y, true : false; }
const int maxN = 2e5 + 2;
int n, m; LL ans; int col[maxN], fa[maxN]; LL delta[maxN << 1]; ptr root; std::vector<int> G[maxN]; std::vector<pii> modi[maxN];
struct Node { int size, virt; LL vir_2; ptr son[2], fa;
Node() : size(1), virt(0), vir_2(0) { son[0] = son[1] = fa = nullptr; }
int Ident() { return !this->fa ? -1 : this->fa->son[0] == this ? 0 : this->fa->son[1] == this ? 1 : -1; }
static int Size(ptr x) { return x ? x->size : 0; }
void Pushup() { this->size = Size(this->son[0]) + Size(this->son[1]) + this->virt + 1; }
void Rotate() { ptr curfa = this->fa; int dir = this->Ident(); if (~curfa->Ident()) curfa->fa->son[curfa->Ident()] = this; if (this->son[dir ^ 1]) this->son[dir ^ 1]->fa = curfa; curfa->son[dir] = this->son[dir ^ 1], this->son[dir ^ 1] = curfa; this->fa = curfa->fa, curfa->fa = this; curfa->Pushup(); }
void Splay() { ptr curfa; while (~this->Ident()) { curfa = this->fa; if (~curfa->Ident()) curfa->Ident() ^ this->Ident() ? this->Rotate() : curfa->Rotate(); this->Rotate(); } this->Pushup(); } };
inline void Access(ptr cur) { for (ptr son = nullptr; cur; son = cur, cur = cur->fa) { cur->Splay(); (cur->virt += Node::Size(cur->son[1])) -= Node::Size(son); cur->vir_2 += (LL)Node::Size(cur->son[1]) * Node::Size(cur->son[1]); cur->vir_2 -= (LL)Node::Size(son) * Node::Size(son); cur->son[1] = son; } }
inline ptr Findroot(ptr cur) { Access(cur), cur->Splay(); while (cur->son[0]) cur = cur->son[0]; cur->Splay(); return cur; }
inline void Link(int u) { ptr cur = root + u, curfa = root + fa[u]; Access(cur), ans += cur->vir_2; ptr currt = Findroot(curfa); Access(currt), ans += currt->vir_2; Access(curfa), curfa->Splay(); cur->fa = curfa; curfa->virt += cur->size; curfa->vir_2 += (LL)cur->size * cur->size; curfa->size += cur->size; Access(currt), ans -= currt->vir_2; }
inline void Cut(int u) { ptr cur = root + u, curfa = root + fa[u], currt = Findroot(cur); Access(currt), ans += currt->vir_2; Access(curfa), curfa->Splay(); curfa->virt -= cur->size; curfa->vir_2 -= (LL)cur->size * cur->size; curfa->size -= cur->size; cur->fa = nullptr; Access(cur), ans -= cur->vir_2; Access(currt), ans -= currt->vir_2; }
void DFS(int u) { for (int v : G[u]) if (v != fa[u]) fa[v] = u, DFS(v); }
int main() { #ifndef ONLINE_JUDGE freopen("color.in", "r", stdin); freopen("color.out", "w", stdout); #endif n = read(), m = read(); root = new Node[n + 2]; for (int i = 1; i <= n; ++i) modi[col[i] = read()].push_back(pii(i, i)); for (int i = 2; i <= n; ++i) { int u = read(), v = read(); G[u].push_back(v), G[v].push_back(u); } fa[1] = n + 1, DFS(1); for (int i = 1; i <= n; ++i) Link(i); for (int i = 1; i <= m; ++i) { int u = read(), x = read(); modi[col[u]].push_back(pii(u, -i - n)); modi[x].push_back(pii(u, i + n)); col[u] = x; } ans = 0; for (int i = 1; i <= n; ++i) { for (int j = 0; j < modi[i].size(); ++j) { int u = modi[i][j].fir, t = modi[i][j].sec; if (t < 0) Link(u); else Cut(u); if (j != modi[i].size() - 1) delta[std::abs(t)] += ans, delta[std::abs(modi[i][j + 1].sec)] -= ans; else delta[std::abs(t)] += ans; } for (int j = modi[i].size() - 1; ~j; --j) { int u = modi[i][j].fir, t = modi[i][j].sec; if (t < 0) Cut(u); else Link(u); } } ans = 0; for (int i = 1; i <= n; ++i) ans += delta[i]; write(ans), putchar('\n'); for (int i = 1; i <= m; ++i) write(ans += delta[i + n]), putchar('\n'); return 0; }
|