P3899 [湖南集训]谈笑风生
题目描述
Solution
我们考虑离线询问,将询问放在相对应的子树 p p p中计算答案。
显然 a , b , c a,b,c a,b,c的位置关系有两种情况:
- b b b是 a a a的祖先, c c c是 a a a的子孙。
- a a a是 b b b的祖先, c c c是 b b b的子孙。
第一种位置关系很容易统计答案。
方案数为
m
i
n
(
d
e
p
[
a
]
−
1
,
k
)
∗
(
s
i
z
e
[
a
]
−
1
)
min(dep[a]-1,k)*(size[a]-1)
min(dep[a]−1,k)∗(size[a]−1)。
第二种位置关系可以
D
S
U
o
n
t
r
e
e
DSU\;on\;\;tree
DSUontree或长链剖分计算。
显然
D
S
U
o
n
t
r
e
e
DSU\;on\;\;tree
DSUontree简单啊
Q
A
Q
QAQ
QAQ。
对于每一个 b b b,合法的 c c c为 b b b的子孙节点,因此相当于每一个 b b b的贡献为 s i z e [ b ] − 1 size[b]-1 size[b]−1。我们在 D S U o n t r e e DSU\;on\;\;tree DSUontree的过程中记录一个值 f [ x ] f[x] f[x]表示当前子树中深度为 x x x的所有 b b b的贡献和,那么总答案就是每一个子树 a a a的 ∑ i = 1 k f [ d e p [ a ] + i ] \sum_{i=1}^{k}{f[dep[a]+i]} ∑i=1kf[dep[a]+i]。
相当于我们要支持单点修改,区间求和,用树状数组维护 f [ x ] f[x] f[x]即可。
时间复杂度 O ( n l g 2 n ) O(nlg^2n) O(nlg2n),但实测挺快的。
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <cassert>
#include <string.h>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
#define MP(A,B) make_pair(A,B)
#define PB(A) push_back(A)
#define SIZE(A) ((int)A.size())
#define LEN(A) ((int)A.length())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define fi first
#define se second
using namespace std;
template<typename T>inline bool upmin(T &x,T y) { return y<x?x=y,1:0; }
template<typename T>inline bool upmax(T &x,T y) { return x<y?x=y,1:0; }
typedef long long ll;
typedef unsigned long long ull;
typedef long double lod;
typedef pair<int,int> PR;
typedef vector<int> VI;
const lod eps=1e-11;
const lod pi=acos(-1);
const int oo=1<<30;
const ll loo=1ll<<62;
const int mods=998244353;
const int MAXN=600005;
const int INF=0x3f3f3f3f;//1061109567
/*--------------------------------------------------------------------*/
inline int read()
{
int f=1,x=0; char c=getchar();
while (c<'0'||c>'9') { if (c=='-') f=-1; c=getchar(); }
while (c>='0'&&c<='9') { x=(x<<3)+(x<<1)+(c^48); c=getchar(); }
return x*f;
}
vector<int> e[MAXN];
vector<PR> Q[MAXN];
int sz[MAXN],mx[MAXN],dep[MAXN],n,q;
ll sum[MAXN],Ans[MAXN];
int lowbit(int x){ return x&(-x); }
void change(int x,ll y){ for (;x<=n;x+=lowbit(x)) sum[x]+=y; }
ll query(int x){ ll ans=0; for (;x;x-=lowbit(x)) ans+=sum[x]; return ans; }
void dfs1(int x,int father)
{
sz[x]=1,mx[x]=0,dep[x]=dep[father]+1;
for (auto v:e[x])
{
if (v==father) continue;
dfs1(v,x);
sz[x]+=sz[v];
if (sz[mx[x]]<sz[v]) mx[x]=v;
}
}
void modify(int x,int c,int father,int mx)
{
// cout<<x<<" "<<dep[x]<<":"<<c*(sz[x]-1)<<endl;
change(dep[x],c*(sz[x]-1));
for (auto v:e[x])
{
if (v==father||v==mx) continue;
modify(v,c,x,mx);
}
}
void dfs2(int x,int father,int flag)
{
for (auto v:e[x])
{
if (v==father||v==mx[x]) continue;
dfs2(v,x,0);
}
if (mx[x]) dfs2(mx[x],x,1);
modify(x,1,father,mx[x]);
for (auto y:Q[x])
{
Ans[y.fi]=query(min(n,dep[x]+y.se))-query(dep[x])+1ll*(min(y.se,dep[x]-1))*(sz[x]-1);
// cout<<y.fi<<" "<<x<<" "<<y.se<<" "<<query(min(n,dep[x]+y.se))<<" "<<query(dep[x])<<endl;
}
if (!flag) modify(x,-1,father,0);
}
int main()
{
n=read(),q=read();
for (int i=1;i<n;i++)
{
int u=read(),v=read();
e[u].PB(v);
e[v].PB(u);
}
for (int i=1;i<=q;i++)
{
int x=read(),y=read();
Q[x].PB(MP(i,y));
}
dfs1(1,0);
dfs2(1,0,1);
for (int i=1;i<=q;i++) printf("%lld\n",Ans[i]);
return 0;
}