题解:P5399 [Ynoi2018] 駄作
yangzichen1203 · · 题解
::::info[Top Cluster 树分块(含图)]
这里放一下一篇重要材料(强烈建议阅读):周欣《浅谈一类树分块的构建算法及其应用》。
如果你还不会,建议先仔细阅读论文,然后结合代码学习。
同时建议看下图以深入理解(
:::info[原树] ::: :::info[界点选取] ::: :::info[簇划分] ::: :::: :::info[Solution]
先对原树 Top Cluster 分块,然后对于每一个邻域进行分解,以下设
分类讨论(以下用簇表示树分块的簇,块表示邻域分解的块):
1.两个块在不同簇内:我们考虑树形 dp 求解,设当前块为
借用一下图:
2.两个块在同一个簇内,且两个块的中心都在簇的界点上:我们考虑预处理出所有半径取值所对应的答案,然后做二维前缀和(由于 Top Cluster 树分解的优秀性质,最多只有
3.两个块在同一个簇内,至少一个块的中心不在簇的界点上:这种情况每一次询问只发生
总时间复杂度:
总空间复杂度:
有关卡常:https://www.luogu.com.cn/discuss/1226443
::: :::success[Code(含注释)]
#include<bits/stdc++.h>
#define For(i,j,k) for(int i=j;i<=k;i++)
#define dFor(i,j,k) for(int i=j;i>=k;i--)
using namespace std;
struct IO {
#define MAXSIZE (1 << 20)
#define isdigit(x) (x >= '0' && x <= '9')
char buf[MAXSIZE], *p1, *p2;
char pbuf[MAXSIZE], *pp;
#if DEBUG
#else
IO() : p1(buf), p2(buf), pp(pbuf) {}
~IO() { fwrite(pbuf, 1, pp - pbuf, stdout); }
#endif
char gc() {
#if DEBUG // ??,?????
return getchar();
#endif
if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin);
return p1 == p2 ? ' ' : *p1++;
}
void read(int &x) {
bool neg = false;
x = 0;
char ch = gc();
for (; !isdigit(ch); ch = gc())
if (ch == '-') neg = true;
if (neg)
for (; isdigit(ch); ch = gc()) x = x * 10 + ('0' - ch);
else
for (; isdigit(ch); ch = gc()) x = x * 10 + (ch - '0');
}
void read(char *s) {
char ch = gc();
for (; isspace(ch); ch = gc());
for (; !isspace(ch); ch = gc()) *s++ = ch;
*s = 0;
}
void read(char &c) { for (c = gc(); isspace(c); c = gc()); }
void push(const char &c) {
#if DEBUG // ??,?????
putchar(c);
#else
if (pp - pbuf == MAXSIZE) fwrite(pbuf, 1, MAXSIZE, stdout), pp = pbuf;
*pp++ = c;
#endif
}
void write(long long x) {
bool neg = false;
if (x < 0) {
neg = true;
push('-');
}
static int sta[40];
int top = 0;
do {
sta[top++] = x % 10;
x /= 10;
} while (x);
if (neg)
while (top) push('0' - sta[--top]);
else
while (top) push('0' + sta[--top]);
}
void write(long long x, char lastChar) { write(x), push(lastChar); }
} io;
#define MAXN 100005
#define MAXM 1005
int n,B,sz[MAXN];
bool has[MAXN],vis[MAXN];
vector<int> e[MAXN];
void dfs1(int u){//划分界点
int sum=0;
for(int v:e[u]){
dfs1(v);
sz[u]+=sz[v]+1;
has[u]|=has[v];
sum+=has[v];
}
if(sz[u]>=B||sum>=2||u==1){
vis[u]=1;
sz[u]=0;
has[u]=1;
}
}
int tot=0,dfn[MAXN],pos[MAXN];
int cnt=0,rt[MAXM][2],bel[MAXN];//0:上界点 1:下界点
bool ok[MAXM];//ok:块内有下界点
int L[MAXM],R[MAXM],rpd[MAXN];//L/R:簇的dfn序范围 rpd:点所在块的下界点
vector<int> clu[MAXN];//点在哪些簇内
int find(int u){//找下界点
bel[dfn[u]]=cnt;
if(!L[cnt]) L[cnt]=dfn[u];
R[cnt]=dfn[u];
if(vis[u]) return u;
for(int v:e[u]){
int res=find(v);
if(res){
return res;
}
}
return 0;
}
void dfs2(int u){//划分簇
dfn[u]=++tot;pos[tot]=u;
sort(e[u].begin(),e[u].end(),[&](const int x,const int y){return has[x]<has[y];});
for(int v:e[u]){
dfs2(v);
}
if(vis[u]){
bool flag=0;
for(int v:e[u]){
if(!flag||(ok[cnt]&&has[v])||(R[cnt]-L[cnt]+1)+sz[v]+1>=B){
cnt++;
rt[cnt][0]=rt[cnt][1]=dfn[u];
flag=1;
clu[dfn[u]].push_back(cnt);
}
int res=find(v);
if(res){
rt[cnt][1]=dfn[res];
ok[cnt]=1;
rpd[dfn[res]]=cnt;
}
}
}
}
void initclu(){
dfs1(1);
dfs2(1);
cnt++;
L[cnt]=R[cnt]=1;
bel[1]=cnt;
rt[cnt][0]=rt[cnt][1]=1;
rpd[1]=cnt;
memset(sz,0,sizeof(sz));
}
int fa[MAXN],dep[MAXN];
void dfs3(int u){
sz[dfn[u]]=1;
for(int v:e[u]){
fa[dfn[v]]=dfn[u];
dep[dfn[v]]=dep[dfn[u]]+1;
dfs3(v);
sz[dfn[u]]+=sz[dfn[v]];
}
}
int lg[MAXN],mn[17][MAXN];//O(nlogn)-O(1)求LCA
void initLCA(){
dfs3(1);
For(i,2,n){
lg[i]=lg[i>>1]+1;
}
For(i,1,n){
mn[0][i]=fa[i];
}
For(i,1,lg[n]){
For(j,1,n+1-(1<<i)){
mn[i][j]=min(mn[i-1][j],mn[i-1][j+(1<<(i-1))]);
}
}
}
inline int LCA(int u,int v){
if(u==v) return u;
if(u>v) swap(u,v);
int s=lg[v-u];
return min(mn[s][u+1],mn[s][v-(1<<s)+1]);
}
inline int dis(int u,int v){
return dep[u]+dep[v]-2*dep[LCA(u,v)];
}
int disr0[MAXN],disr1[MAXN];
int presz[2][MAXM][MAXM],predep[2][MAXM][MAXM],predis[2][MAXM][MAXM];
//上/下界点的k-邻域的点数/深度和/到下界点的距离和
int bl[MAXM],br[MAXM];
vector<int> c[MAXM];//簇内有哪些点
void init(){
For(i,1,cnt){
int clusz=R[i]-L[i]+1;
int r0=rt[i][0],r1=rt[i][1];
For(j,L[i],R[i]){
disr0[j]=dis(j,r0);
disr1[j]=dis(j,r1);
}
For(x,0,1){
For(j,L[i],R[i]){
int d=dis(j,rt[i][x]);
presz[x][i][d]++;
predep[x][i][d]+=dep[j];
predis[x][i][d]+=dis(j,rt[i][1]);
}
For(j,1,clusz){
presz[x][i][j]+=presz[x][i][j-1];
predep[x][i][j]+=predep[x][i][j-1];
predis[x][i][j]+=predis[x][i][j-1];
}
}
bl[i]=rt[i][1];br[i]=rt[i][1]+sz[rt[i][1]]-1;
}
For(i,1,n){
if(vis[pos[i]]){
for(int x:clu[i]){
c[rpd[i]].push_back(x);
}
}
}
}
//以上是预处理
//以下是计算答案
inline pair<int,int> calc(int pos,int bel){
if(pos>=bl[bel]&&pos<=br[bel]){
return {1,dep[pos]-dep[rt[bel][1]]};
}else{
return {0,dis(pos,rt[bel][0])};
}
}
int p0[MAXN],d0[MAXN],p1[MAXN],d1[MAXN];
long long ans[MAXN];
int q,sumsz[2][MAXM],sumdep[2][MAXM],sumdis[2][MAXM];//同presz/predep/predis
long long f[MAXM][2][2];
void work1(){//处理不同块之间的答案,树形dp
For(i,1,q){
memset(sumsz,0,sizeof(sumsz));
memset(sumdep,0,sizeof(sumdep));
memset(sumdis,0,sizeof(sumdis));
For(j,1,cnt){
int clusz=R[j]-L[j]+1;
if(bel[p0[i]]==j){
For(x,L[j],R[j]){
if(dis(x,p0[i])<=d0[i]){
sumsz[0][j]++;
sumdep[0][j]+=dep[x];
sumdis[0][j]+=disr1[x];
}
}
}else{
pair<int,int> res=calc(p0[i],j);
int u=res.first,d=res.second;
if(d<=d0[i]){
int t=min(d0[i]-d,clusz);
sumsz[0][j]=presz[u][j][t];
sumdep[0][j]=predep[u][j][t];
sumdis[0][j]=predis[u][j][t];
}
}
if(bel[p1[i]]==j){
For(x,L[j],R[j]){
if(dis(x,p1[i])<=d1[i]){
sumsz[1][j]++;
sumdep[1][j]+=dep[x];
sumdis[1][j]+=disr1[x];
}
}
}else{
pair<int,int> res=calc(p1[i],j);
int u=res.first,d=res.second;
if(d<=d1[i]){
int t=min(d1[i]-d,clusz);
sumsz[1][j]=presz[u][j][t];
sumdep[1][j]=predep[u][j][t];
sumdis[1][j]=predis[u][j][t];
}
}
}
memset(f,0,sizeof(f));
For(j,1,cnt){
long long sum=0;
int d=dep[rt[j][1]];
for(int x:c[j]){
f[j][0][0]+=f[x][0][0];
f[j][0][1]+=f[x][0][1];
f[j][1][0]+=f[x][1][0];
f[j][1][1]+=f[x][1][1];
sum+=f[x][0][1]*sumdis[0][j]-f[x][0][1]*sumsz[0][j]*d+f[x][1][1]*sumsz[0][j];
sum+=f[x][0][0]*sumdis[1][j]-f[x][0][0]*sumsz[1][j]*d+f[x][1][0]*sumsz[1][j];
}
for(int x:c[j]){
sum-=1ll*d*f[x][0][0]*(f[j][0][1]-f[x][0][1]);
sum-=1ll*d*f[x][0][1]*(f[j][0][0]-f[x][0][0]);
sum+=f[x][1][0]*(f[j][0][1]-f[x][0][1]);
sum+=f[x][1][1]*(f[j][0][0]-f[x][0][0]);
}
f[j][0][0]+=sumsz[0][j];
f[j][0][1]+=sumsz[1][j];
f[j][1][0]+=sumdep[0][j];
f[j][1][1]+=sumdep[1][j];
ans[i]+=sum;
}
}
}
int pre[MAXM][MAXM][2][2];
//邻域中心固定时单个块内的前缀答案
//注意到pre数组正常要开O(nsqrt(n))的空间,所以这里必须离线卡空间
void work2(){//处理同块内邻域中心在端点的答案,二维前缀和
For(i,1,cnt){
int clusz=R[i]-L[i]+1;
For(a,L[i],R[i]){
For(b,L[i],R[i]){
int d=dis(a,b);
pre[disr0[a]][disr0[b]][0][0]+=d;
pre[disr0[a]][disr1[b]][0][1]+=d;
pre[disr1[a]][disr0[b]][1][0]+=d;
pre[disr1[a]][disr1[b]][1][1]+=d;
}
}
For(a,1,clusz){
For(b,0,clusz){
pre[a][b][0][0]+=pre[a-1][b][0][0];
pre[a][b][0][1]+=pre[a-1][b][0][1];
pre[a][b][1][0]+=pre[a-1][b][1][0];
pre[a][b][1][1]+=pre[a-1][b][1][1];
}
}
For(a,0,clusz){
For(b,1,clusz){
pre[a][b][0][0]+=pre[a][b-1][0][0];
pre[a][b][0][1]+=pre[a][b-1][0][1];
pre[a][b][1][0]+=pre[a][b-1][1][0];
pre[a][b][1][1]+=pre[a][b-1][1][1];
}
}
For(j,1,q){
if(bel[p0[j]]!=i&&bel[p1[j]]!=i){
pair<int,int> t0=calc(p0[j],i);
if(t0.second<=d0[j]){
pair<int,int> t1=calc(p1[j],i);
if(t1.second<=d1[j]){
ans[j]+=pre[min(d0[j]-t0.second,clusz)][min(d1[j]-t1.second,clusz)][t0.first][t1.first];
}
}
}
}
For(a,0,clusz){
For(b,0,clusz){
pre[a][b][0][0]=0;
pre[a][b][0][1]=0;
pre[a][b][1][0]=0;
pre[a][b][1][1]=0;
}
}
}
}
int dp[MAXM][2];
void work3(){//处理同块内邻域中心不在端点的答案,树上差分
For(i,1,q){
For(j,1,cnt-1){
if(bel[p0[i]]==j||bel[p1[i]]==j){
int sum0=0,cnt0=0,sum1=0,cnt1=0;
For(x,L[j],R[j]){
if(dis(x,p0[i])<=d0[i]){
dp[x][0]++;
sum0+=dep[x];
cnt0++;
}
}
dFor(x,R[j],L[j]){
if(fa[x]!=rt[j][0]){
dp[fa[x]][0]+=dp[x][0];
}
}
For(x,L[j],R[j]){
if(dis(x,p1[i])<=d1[i]){
dp[x][1]++;
sum1+=dep[x];
cnt1++;
}
}
dFor(x,R[j],L[j]){
if(fa[x]!=rt[j][0]){
dp[fa[x]][1]+=dp[x][1];
}
}
ans[i]+=1ll*sum0*cnt1;
ans[i]+=1ll*sum1*cnt0;
For(x,L[j],R[j]){
ans[i]-=2ll*dp[x][0]*dp[x][1];
dp[x][0]=dp[x][1]=0;
}
ans[i]-=2ll*dep[rt[j][0]]*cnt0*cnt1;
}
}
}
}
int main(){
io.read(n);
B=sqrt(n)*2.12;//调块长!!!!!!!!!!!!
For(i,2,n){
int f;
io.read(f);
e[f].push_back(i);
}
initclu();
initLCA();
init();
io.read(q);
For(i,1,q){
io.read(p0[i]);io.read(d0[i]);io.read(p1[i]);io.read(d1[i]);
p0[i]=dfn[p0[i]];
p1[i]=dfn[p1[i]];
}
work1();
work2();
work3();
For(i,1,q){
io.write(ans[i],'\n');
}
return 0;
}
| AC 记录 |
|---|