跳到正文

题解

「LKJOI R1 D」和谐灯带 题解

bz02约 14 分钟
原文
文章信息与版本

文章信息

收录
2026年1月20日
更新
2026年1月31日
篇幅
约 14 分钟

版本

  1. v6当前
  2. v5
  3. v4
  4. v3
更早的 2 个版本
  1. v2
  2. v1

题目传送门

题目传送门

题目大意

给你一棵树,上面的每个点有一个颜色,每次问 l,rl,r 之间的最短路径是否 有颜色大于长度除以二 且 每个颜色都有。

题解

5分

直接暴力计算。

不给出代码。

25分

我们可以发现,只要我们可以知道 l,rl,r 间的各颜色数量,即可判断,我们考虑用LCA同时维护。

设 fi,j,1f_{i,j,1} 为 ii 的第 2j2^j 层祖先与其路径中所有红色数量,不包括第 2j2^j 层祖先,包括 ii 点。

设 fi,j,2f_{i,j,2} 为 ii 的第 2j2^j 层祖先与其路径中所有绿色数量,不包括第 2j2^j 层祖先,包括 ii 点。

设 fi,j,3f_{i,j,3} 为 ii 的第 2j2^j 层祖先与其路径中所有蓝色数量,不包括第 2j2^j 层祖先,包括 ii 点。

设 fi,j,0f_{i,j,0} 为 ii 的第 2j2^j 层祖先节点。

则可以在往上跳祖先节点时也计一个量叠加颜色数量,要注意,需要将祖先节点加上,因为之前未记录,只要按正常LCA就可以了。

代码见下:

C++
#include<bits/stdc++.h>
using namespace std;
long long n,q,gg,rf=2000,de[300005],x,y;
char s[300005];
vector<long long> v[300005];
struct one{
	long long x,a,b,c;
}f[300005][21],rc;
one operator+(one a1,one b1){
	return (one){a1.x+b1.x,a1.a+b1.a,a1.b+b1.b,a1.c+b1.c};
}
void abc(long long a1,long long b1){
	f[a1][0].x=b1;
	de[a1]=de[b1]+1;
	if(s[a1]=='R'){
		f[a1][0].a=1;
	}
	else if(s[a1]=='G'){
		f[a1][0].b=1;
	}
	else{
		f[a1][0].c=1;
	}
	for(int i=0;i<v[a1].size();i++){
		long long tt=v[a1][i];
		if(tt!=b1){
			abc(tt,a1);
		}
	}
	return ;
}
int main(){
	cin>>n>>q;
	for(int i=1;i<=n;i++){
		cin>>s[i];
	}
	for(int i=2;i<=n;i++){
		cin>>gg;
		v[i].push_back(gg);
		v[gg].push_back(i);
	}
	abc(1,0);
	for(int j=1;j<=20;j++){
		for(int i=1;i<=n;i++){
			f[i][j].x=f[f[i][j-1].x][j-1].x;
			f[i][j].a=f[f[i][j-1].x][j-1].a+f[i][j-1].a;
			f[i][j].b=f[f[i][j-1].x][j-1].b+f[i][j-1].b;
			f[i][j].c=f[f[i][j-1].x][j-1].c+f[i][j-1].c;
		}
	}
	while(q--){
		cin>>x>>y;
		if(de[x]<=de[y]){
			swap(x,y);
		}
		rc={0,0,0,0};
		for(int j=20;j>=0;j--){
			if(de[f[x][j].x]>=de[y]){
				rc=rc+f[x][j];
				x=f[x][j].x;
			}
		}
		if(x==y){
			rc=rc+f[x][0];

		}
		else{
			for(int j=20;j>=0;j--){
				if(f[x][j].x!=f[y][j].x){
					rc=rc+f[x][j];
					rc=rc+f[y][j];
					x=f[x][j].x;
					y=f[y][j].x;
				}
			}
			rc=rc+f[x][0];
			rc=rc+f[y][0];
			x=f[x][0].x;
			rc=rc+f[x][0];
		}
        //cout<<rc.a<<" "<<rc.b<<" "<<rc.c<<endl;
		if(rc.a!=0&&rc.b!=0&&rc.c!=0&&max({rc.a,rc.b,rc.c})<=(rc.a+rc.b+rc.c)/2){
			cout<<"Yes"<<endl;
		}
		else{
			cout<<"No"<<endl;
		}
	}
	return 0;
}

40分

LCA空间太大了,可以使用树剖来做,类似树剖模板,直接实现 xx ~ yy 点简单路径值求和即可。

代码见下:

C++
#include<bits/stdc++.h>
using namespace std;
int n,aa,bb,a[1500006],m,u,v1;
string str;
vector<int> v[1500006];
int siz[1500006],de[1500006],fa[1500006],son[1500006];
int w[1500006],top[1500006],db=0;
struct two{
	int a,b,c;
}te[4500006];
inline two operator+(two a1,two b1){
	return {a1.a+b1.a,a1.b+b1.b,a1.c+b1.c};
}
inline void abc(int a1){
	siz[a1]=1;
	de[a1]=de[fa[a1]]+1;
	for(int i=0;i<v[a1].size();i++){
		int tt=v[a1][i];
		if(tt!=fa[a1]){
			fa[tt]=a1;
			abc(tt);
			siz[a1]+=siz[tt];
			if(siz[tt]>siz[son[a1]]){
				son[a1]=tt;
			}
		}
	}
	return ;
}
inline void abc2(int a1,int tp){
	w[a1]=++db;
	top[a1]=tp;
	if(son[a1]!=0){
		abc2(son[a1],tp);
	}
	for(int i=0;i<v[a1].size();i++){
		int tt=v[a1][i];
		if(tt!=fa[a1]&&tt!=son[a1]){
			abc2(tt,tt);
		}
	}
	return ;
}
char s[1500006],s2[1500006];
inline void bu(int a1,int l,int r){
    //cout<<l<<" "<<r<<endl;
	if(l==r){
        //cout<<l<<" "<<s2[l]<<endl;
		if(s2[l]=='R'){
			te[a1]={1,0,0};
		}
		else if(s2[l]=='G'){
			te[a1]={0,1,0};
		}
		else{
			te[a1]={0,0,1};
		}
		return ;
	}
	int mid=(l+r)/2;
	bu(a1*2,l,mid);
	bu(a1*2+1,mid+1,r);
	te[a1]=te[a1*2]+te[a1*2+1];
	return ;
}
inline two akx(int a1,int l,int r,int x,int y){
	if(x<=l&&r<=y){
		return te[a1];
	}
	int mid=(l+r)/2;
	two cc={0,0,0};
	if(x<=mid){
		cc=cc+akx(a1*2,l,mid,x,y);
	}
	if(y>mid){
		cc=cc+akx(a1*2+1,mid+1,r,x,y);
	}
	return cc;
}
inline two cox(int a1,int b1){
	two cc={0,0,0};
	while(top[a1]!=top[b1]){
		if(de[top[a1]]<de[top[b1]]){
			swap(a1,b1);
		}
		cc=cc+akx(1,1,n,w[top[a1]],w[a1]);
		a1=fa[top[a1]];
	}
	if(de[a1]>de[b1]){
		swap(a1,b1);
	}
	return cc+akx(1,1,n,w[a1],w[b1]);
}
char *p1,*p2,buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
inline int read(){
	int x=0;
	char ch=nc();
	while(ch<=47||ch>=58){
		ch=nc();
	}
	while(ch>=48&&ch<=57){
		x=(x<<3)+(x<<1)+ch-48;
		ch=nc();
	}
	return x;
}
int main(){
	cin>>n>>m;
    //cout<<n<<endl;
	for(int i=1;i<=n;i++){
		cin>>s[i];
	}
	// nc();
	for(int i=2;i<=n;i++){
		bb=read();
		v[i].push_back(bb);
		v[bb].push_back(i);
	}
   // cout<<n<<endl;
	abc(1);
	abc2(1,1);
	for(int i=1;i<=n;i++){
		s2[w[i]]=s[i];
        //cout<<w[i]<<endl;
	}
   // cout<<n<<endl;
	bu(1,1,n);
   // cout<<1<<endl;
	for(int i=1;i<=m;i++){
		u=read();
		v1=read();
		two aa=cox(u,v1);
		if(aa.a!=0&&aa.b!=0&&aa.c!=0&&max({aa.a,aa.b,aa.c})<=(aa.a+aa.b+aa.c)/2){
			cout<<"Yes\n";
		}
		else{
			cout<<"No\n";
		}
	}
	return 0;
}

100分(AC)

正常树剖是 O(nlog2n)O(nlog^2n) 的,可以发现,我们只需初始化和求和即可,于是考虑前缀和,即用 territerr_i 表示所有在剖分后重新编号的树上的编号小于等于 ii 点值和,由于每条重链上点值连续,所以直接差分即可,为 O(nlogn)O(nlogn) 。

代码见下(AC):

C++
#include<bits/stdc++.h>
using namespace std;
int n,aa,bb,a[1500006],m,u,v1;
string str;
vector<int> v[1500006];
int siz[1500006],de[1500006],fa[1500006],son[1500006];
int w[1500006],top[1500006],db=0;
struct two{
	int a,b,c;
}te[4500006];
inline two operator+(two a1,two b1){
	return {a1.a+b1.a,a1.b+b1.b,a1.c+b1.c};
}
inline two operator-(two a1,two b1){
	return {a1.a-b1.a,a1.b-b1.b,a1.c-b1.c};
}
inline void abc(int a1){
	siz[a1]=1;
	de[a1]=de[fa[a1]]+1;
	for(int i=0;i<v[a1].size();i++){
		int tt=v[a1][i];
		if(tt!=fa[a1]){
			fa[tt]=a1;
			abc(tt);
			siz[a1]+=siz[tt];
			if(siz[tt]>siz[son[a1]]){
				son[a1]=tt;
			}
		}
	}
	return ;
}
inline void abc2(int a1,int tp){
	w[a1]=++db;
	top[a1]=tp;
	if(son[a1]!=0){
		abc2(son[a1],tp);
	}
	for(int i=0;i<v[a1].size();i++){
		int tt=v[a1][i];
		if(tt!=fa[a1]&&tt!=son[a1]){
			abc2(tt,tt);
		}
	}
	return ;
}
char s[1500006],s2[1500006];
inline two cox(int a1,int b1){
	two cc={0,0,0};
	while(top[a1]!=top[b1]){
		if(de[top[a1]]<de[top[b1]]){
			swap(a1,b1);
		}
		cc=cc+te[w[a1]]-te[w[top[a1]]-1];
		//cout<<w[a1]<<" "<<w[top[a1]]-1<<endl;
		a1=fa[top[a1]];
	}
	if(de[a1]<de[b1]){
		swap(a1,b1);
	}
	return cc+te[w[a1]]-te[w[b1]-1];
}
char *p1,*p2,buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
inline int read(){
	int x=0;
	char ch=nc();
	while(ch<=47||ch>=58){
		ch=nc();
	}
	while(ch>=48&&ch<=57){
		x=(x<<3)+(x<<1)+ch-48;
		ch=nc();
	}
	return x;
}
int main(){
	cin>>n>>m;
	//cout<<n<<endl;
	for(int i=1;i<=n;i++){
		cin>>s[i];
	}
	// nc();
	for(int i=2;i<=n;i++){
		bb=read();
		v[i].push_back(bb);
		v[bb].push_back(i);
	}
	// cout<<n<<endl;
	abc(1);
	abc2(1,1);
	for(int i=1;i<=n;i++){
		if(s[i]=='R'){
			te[w[i]]={1,0,0};
		}
		else if(s[i]=='G'){
			te[w[i]]={0,1,0};
		}
		else{
			te[w[i]]={0,0,1};
		}
		//cout<<w[i]<<endl;
	}
	for(int i=1;i<=n;i++){
		te[i]=te[i]+te[i-1];
	}
	// cout<<n<<endl;
	// cout<<1<<endl;
	for(int i=1;i<=m;i++){
		u=read();
		v1=read();
		//cout<<u<<" "<<v1<<endl;
		two aa=cox(u,v1);
		//cout<<aa.a<<" "<<aa.b<<" "<<aa.c<<endl;
		if(aa.a!=0&&aa.b!=0&&aa.c!=0&&max({aa.a,aa.b,aa.c})<=(aa.a+aa.b+aa.c)/2){
			cout<<"Yes\n";
		}
		else{
			cout<<"No\n";
		}
	}
	return 0;
}
原文