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
|
#include <cctype> #include <cstdio> #include <cstring> #include <algorithm> using namespace std;
#define DEBUG(args...) fprintf(stderr, ##args)
namespace IO { const int MAXSIZE = 1 << 18 | 1; char buf[MAXSIZE], *p1, *p2;
inline int Gc() { return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2)? EOF: *p1++; } template<typename T> inline void read(T& x) { x = 0; int f = 0, ch = Gc(); while (!isdigit(ch)) f |= ch == '-', ch = Gc(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = Gc(); if (f) x = -x; } } using IO::read;
template<typename T> inline bool ckmax(T& a, const T& b) { return (a < b)? a = b, true: false; }
typedef long long LL; const int MAXN = 4e5 + 5;
int n, q; LL ans;
namespace LCT { int ch[2][MAXN], pre[MAXN]; LL s[MAXN], is[MAXN], val[MAXN];
inline void maintain(const int& nd) { s[nd] = is[nd] + val[nd]; if (ch[0][nd]) s[nd] += s[ch[0][nd]]; if (ch[1][nd]) s[nd] += s[ch[1][nd]]; }
inline int which(const int& u) { return pre[u]? ch[1][pre[u]] == u: 0; } inline bool nroot(const int& u) { return pre[u]? ch[0][pre[u]] == u || ch[1][pre[u]] == u: false; }
inline void rotate(const int& u) { int fa = pre[u], w = which(u); pre[u] = pre[fa]; if (nroot(fa)) ch[which(fa)][pre[fa]] = u; ch[w][fa] = ch[w ^ 1][u]; if (ch[w ^ 1][u]) pre[ch[w ^ 1][u]] = fa; ch[w ^ 1][u] = fa, pre[fa] = u; maintain(fa); }
void splay(const int& u) { while (nroot(u)) { int fa = pre[u]; if (nroot(fa)) which(fa) == which(u)? rotate(fa): rotate(u); rotate(u); } maintain(u); }
inline LL calc(int u, LL t, LL h) { if (ch[1][u]) return 2 * (t - h); if (2 * val[u] >= t + 1) return 2 * (t - val[u]); return t - 1; }
void access(int u, const int& w) { int v = u; for (u = pre[u]; u; v = u, u = pre[u]) { splay(u); LL t = s[u] - s[ch[0][u]], h = s[ch[1][u]]; ans -= calc(u, t, h); s[u] += w, t += w, is[u] += w; if (2 * h < t + 1) ch[1][u] = 0, is[u] += h; if (2 * s[v] >= t + 1) ch[1][u] = v, h = s[v], is[u] -= h; ans += calc(u, t, h); maintain(u); } }
void Mdy(int u, const int& w) { splay(u); LL t = s[u] - s[ch[0][u]], h = s[ch[1][u]]; ans -= calc(u, t, h); val[u] += w, t += w, s[u] += w; if (2 * h < t + 1) ch[1][u] = 0, is[u] += h; ans += calc(u, t, h); maintain(u), access(u, w); } }
namespace Graph { struct Edge { int nxt, to; } edges[MAXN << 1]; int head[MAXN], eidx;
inline void init() { memset(head, -1, sizeof head), eidx = 1; } inline void AddEdge(int from, int to) { edges[++eidx] = (Edge){ head[from], to }, head[from] = eidx; }
void dfs(int u) { using namespace LCT; int rson = -1; LL &t = s[u], h = val[u]; t = val[u]; for (int v, i = head[u]; ~i; i = edges[i].nxt) { if ((v = edges[i].to) == pre[u]) continue; pre[v] = u, dfs(v), t += s[v]; if (ckmax(h, s[v])) rson = v; } ans += min(t - 1, 2 * (t - h)); if (rson > 0 && 2 * h >= t + 1) ch[1][u] = rson, is[u] = s[u] - s[rson] - val[u]; else is[u] = s[u] - val[u]; } }
int main() { #ifndef ONLINE_JUDGE freopen("data/ex_history2.in", "r", stdin); #endif Graph::init();
read(n), read(q); for (int i = 1; i <= n; ++i) read(LCT::val[i]); for (int u, v, i = 1; i < n; ++i) read(u), read(v), Graph::AddEdge(u, v), Graph::AddEdge(v, u);
Graph::dfs(1), printf("%lld\n", ans);
for (int x, w; q; --q) { read(x), read(w), LCT::Mdy(x, w); printf("%lld\n", ans); } return 0; }
|