传送门
题意
分析
一开始被唬住了,以为是啥奇奇怪怪的东西然后去做树上差分,后来发现没有这么复杂
画个图就可以分析出来,我们加的那条边的路径上的任何一条边断掉都可以用当前这条边来代替,也就是说这条路径上的每一条边的权值都可以
+
1
+1
+1,所以我们可以考虑用树剖来维护区间的边权和
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10,M = N * 2;
const ll mod = 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a) {
char c = getchar(); T x = 0, f = 1; while (!isdigit(c)) {if (c == '-')f = -1; c = getchar();}
while (isdigit(c)) {x = (x << 1) + (x << 3) + c - '0'; c = getchar();} a = f * x;
}
int gcd(int a, int b) {return (b > 0) ? gcd(b, a % b) : a;}
int h[N],e[M],ne[M],idx;
int id[N],nw[N],cnt;
int dep[N],sz[N],top[N],fa[N],son[N];
int n,m,k;
PII edge[N];
struct Node{
int l,r;
int sum,flag;
}tr[N * 4];
void add(int x,int y){
ne[idx] = h[x],e[idx] = y,h[x] = idx++;
}
void dfs(int u,int f,int d){
dep[u] = d,fa[u] = f,sz[u] = 1;
for(int i = h[u];~i;i = ne[i]){
int j = e[i];
if(j == f) continue;
dfs(j,u,d + 1);
if(sz[j] > sz[son[u]]) son[u] = j;
sz[u] += sz[j];
}
}
void dfs(int u,int t){
id[u] = ++cnt,top[u] = t;
if(!son[u]) return;
dfs(son[u],t);
for(int i = h[u];~i;i = ne[i]){
int j = e[i];
if(j == fa[u]) continue;
if(j == son[u]) continue;
dfs(j,j);
}
}
void push(int u){
tr[u].sum = tr[u << 1].sum + tr[u << 1 | 1].sum;
}
void build(int u,int l,int r){
tr[u].l = l,tr[u].r = r;
if(l == r){
return;
}
int mid = l + r >> 1;
build(u << 1,l,mid),build(u << 1 | 1,mid + 1,r);
}
void down(int u){
if(tr[u].flag){
tr[u << 1].sum += tr[u].flag * (tr[u << 1].r - tr[u << 1].l + 1);
tr[u << 1 | 1].sum += tr[u].flag * (tr[u << 1 | 1].r - tr[u << 1 | 1].l + 1);
tr[u << 1].flag += tr[u].flag;
tr[u << 1 | 1].flag += tr[u].flag;
}
tr[u].flag = 0;
}
void modify(int u,int l,int r){
if(tr[u].l >= l && tr[u].r <= r) {
tr[u].sum += (tr[u].r - tr[u].l + 1);
tr[u].flag++;
return;
}
down(u);
int mid = tr[u].l + tr[u].r >> 1;
if(l <= mid) modify(u << 1,l,r);
if(r > mid) modify(u << 1 | 1,l,r);
push(u);
}
int query(int u,int l,int r){
if(tr[u].l >= l && tr[u].r <= r) return tr[u].sum;
down(u);
int ans = 0;
int mid = tr[u].l + tr[u].r >> 1;
if(l <= mid) ans = query(u << 1,l,r);
if(r > mid) ans += query(u << 1 | 1,l,r);
return ans;
}
void path_add(int l,int r){
while(top[l] != top[r]){
if(dep[top[l]] < dep[top[r]]) swap(l,r);
modify(1,id[top[l]],id[l]);
l = fa[top[l]];
}
if(dep[l] > dep[r]) swap(l,r);
modify(1,id[son[l]],id[r]);
}
int query_path(int l,int r){
int sum = 0;
while(top[l] != top[r]){
if(dep[top[l]] < dep[top[r]]) swap(l,r);
sum += query(1,id[top[l]],id[l]);
l = fa[top[l]];
}
if(dep[l] > dep[r]) swap(l,r);
sum += query(1,id[son[l]],id[r]);
return sum;
}
int main() {
memset(h,-1,sizeof h);
read(n),read(m),read(k);
for(int i = 1;i < n;i++){
int a,b;
read(a),read(b);
add(a,b),add(b,a);
edge[i] = mp(a,b);
}
dfs(1,1,1);
dfs(1,1);
build(1,1,n);
while(m--){
int a,b;
read(a),read(b);
path_add(a,b);
}
while(k--){
int id;
read(id);
int x = edge[id].fi,y = edge[id].se;
di(query_path(x,y));
}
return 0;
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/