嘟嘟嘟
树上莫队。
要会树上莫队,得先会树上分块,A了王室联邦再说。(欢迎查看我的题解)
其实树上莫队和线性的莫队很像,大体思路完全一样:先把询问分块,然后按块排序。接着如果带修改的话就维护三个指针,否则两个。
但是有一个区别,就是移动指针的时候该怎么移。线性莫队往左往右移并且统计答案就行了。但是树上的话,答案统计就不是很方便。
有一个很方便的方法就是开一个vis数组。每次经过一个点,就把这个点的状态取反(加上/减去这个点的贡献)。这样可以证明,当我从\(x, y\)移到当前询问的点对\(px, py\)的时候,选了的点刚好是\(px\)到\(py\)路径上的点。证明还是看兔哥的博客吧。
然后会发现\(lca(x, y)\)很不好办,所以上述的维护是不包含\(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;
}
原文:https://www.cnblogs.com/mrclr/p/10082230.html