[WC2013]糖果公園
阿新 • • 發佈:2018-12-07
嘟嘟嘟
樹上莫隊。
要會樹上莫隊,得先會樹上分塊,A了王室聯邦再說。(歡迎檢視我的題解)
其實樹上莫隊和線性的莫隊很像,大體思路完全一樣:先把詢問分塊,然後按塊排序。接著如果帶修改的話就維護三個指標,否則兩個。
但是有一個區別,就是移動指標的時候該怎麼移。線性莫隊往左往右移並且統計答案就行了。但是樹上的話,答案統計就不是很方便。
有一個很方便的方法就是開一個vis陣列。每次經過一個點,就把這個點的狀態取反(加上/減去這個點的貢獻)。這樣可以證明,當我從\(x, y\)移到當前詢問的點對\(px, py\)的時候,選了的點剛好是\(px\)到\(py\)路徑上的點。證明還是看兔哥的部落格吧。
然後會發現\(lca(x, y)\)
然後就沒啦,程式碼跟帶修改莫隊很像,注意細節。
#include<cstdio> #include<iostream> #include<cmath> #include<algorithm> #include<cstring> #include<cstdlib> #include<cctype> #include<vector> #include<stack> #include<queue> using namespace std; #define enter puts("") #define space putchar(' ') #define Mem(a, x) memset(a, x, sizeof(a)) #define rg register typedef long long ll; typedef double db; const int INF = 0x3f3f3f3f; const db eps = 1e-8; const int maxn = 1e5 + 5; inline ll read() { ll ans = 0; char ch = getchar(), last = ' '; while(!isdigit(ch)) last = ch, ch = getchar(); while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar(); if(last == '-') ans = -ans; return ans; } inline void write(ll x) { if(x < 0) x = -x, putchar('-'); if(x >= 10) write(x / 10); putchar(x % 10 + '0'); } const int S = 2154; int n, m, t; int c[maxn]; ll v[maxn], w[maxn]; struct Edge { int nxt, to; }e[maxn << 1]; int head[maxn], ecnt = -1; void addEdge(int x, int y) { e[++ecnt] = (Edge){head[x], y}; head[x] = ecnt; } int bel[maxn], cntB = 0; int st[maxn], top = 0; int dep[maxn], fa[21][maxn]; void dfs(int now, int _f) { for(int i = 1; (1 << i) <= dep[now]; ++i) fa[i][now] = fa[i - 1][fa[i - 1][now]]; int tp = top; for(int i = head[now], v; i != -1; i = e[i].nxt) { if((v = e[i].to) != _f) { fa[0][v] = now; dep[v] = dep[now] + 1; dfs(v, now); if(top - tp >= S) { cntB++; while(top > tp) bel[st[top--]] = cntB; } } } st[++top] = now; } int lca(int x, int y) { if(dep[x] < dep[y]) swap(x, y); for(int i = 20; i >= 0; --i) if(dep[x] - (1 << i) >= dep[y]) x = fa[i][x]; if(x == y) return x; for(int i = 20; i >= 0; --i) if(fa[i][x] != fa[i][y]) x = fa[i][x], y = fa[i][y]; return fa[0][x]; } struct Node { int x, y, id, tim; bool operator < (const Node& oth)const { if(bel[x] != bel[oth.x]) return bel[x] < bel[oth.x]; if(bel[y] != bel[oth.y]) return bel[y] < bel[oth.y]; return tim < oth.tim; } }q[maxn]; int px = 1, py = 1, cntQ = 0, cur = 0; //cur:時間指標 int cntC = 0, tim[maxn], pos[maxn], val[maxn], pre[maxn]; bool vis[maxn]; ll cnt = 0, tot[maxn], ans[maxn]; void Rev(int now) //反轉,並計算貢獻 { if(vis[now]) cnt -= w[tot[c[now]]--] * v[c[now]]; else cnt += w[++tot[c[now]]] * v[c[now]]; vis[now] ^= 1; } void Tim_go(int cur) { bool flg = 0; if(vis[pos[cur]]) flg = 1, Rev(pos[cur]); //表示這個點在當前的答案序列中,所以先減去 pre[cur] = c[pos[cur]]; c[pos[cur]] = val[cur]; if(flg) Rev(pos[cur]); //再加上新的貢獻 } void Tim_back(int cur) { bool flg = 0; if(vis[pos[cur]]) flg = 1, Rev(pos[cur]); c[pos[cur]] = pre[cur]; if(flg) Rev(pos[cur]); } void timCraft(int now) { while(cur < cntC && tim[cur + 1] <= now) Tim_go(++cur); while(cur && tim[cur] > now) Tim_back(cur--); } void move(int x, int y) { int z = lca(x, y); while(x != z) Rev(x), x = fa[0][x]; while(y != z) Rev(y), y = fa[0][y]; } int main() { Mem(head, -1); n = read(); m = read(); t = read(); for(int i = 1; i <= m; ++i) v[i] = read(); for(int i = 1; i <= n; ++i) w[i] = read(); for(int i = 1; i < n; ++i) { int x = read(), y = read(); addEdge(x, y); addEdge(y, x); } for(int i = 1; i <= n; ++i) c[i] = read(); dfs(1, 0); while(top) bel[st[top--]] = cntB; for(int i = 1; i <= t; ++i) { int op = read(), x = read(), y = read(); if(!op) tim[++cntC] = i, pos[cntC] = x, val[cntC] = y; else q[++cntQ].x = x, q[cntQ].y = y, q[cntQ].id = cntQ, q[cntQ].tim = i; } sort(q + 1, q + cntQ + 1); for(int i = 1; i <= cntQ; ++i) { timCraft(q[i].tim); move(px, q[i].x), px = q[i].x; move(py, q[i].y), py = q[i].y; Rev(lca(px, py)); ans[q[i].id] = cnt; Rev(lca(px, py)); //剛開始我忘了在減去lca的貢獻,debug了半天 } for(int i = 1; i <= cntQ; ++i) write(ans[i]), enter; return 0; }