只过了Hack和样例 其他全WA 求调QAQ
查看原帖
只过了Hack和样例 其他全WA 求调QAQ
375337
Silent1019楼主2023/11/17 08:54
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct Edge{
	int v,next;
}edge[N<<1];
int head[N],tot;
void add(int u,int v)
{
	tot++;
	edge[tot].v=v; edge[tot].next=head[u];
	head[u]=tot;
}
struct Tree{
	int l_c,r_c,tag;
	int sum;
}tree[N<<2];
int n,m;
int val[N];
int dep[N],son[N],siz[N],fa[N];
void dfs1(int u,int f,int deep)
{
	dep[u]=deep; siz[u]=1; fa[u]=f;
	for(int i=head[u];i;i=edge[i].next)
	{
		int e=edge[i].v;
		if(e==f) continue;
		dfs1(e,u,deep+1);
		siz[u]+=siz[e];
		if(siz[e]>siz[son[u]]||!son[u]) son[u]=e;
	}
}
int top[N],cnt,id[N],wt[N];
void dfs2(int u,int tp)
{
	top[u]=tp; wt[++cnt]=val[u]; id[u]=cnt;
	if(!son[u]) return ;
	dfs2(son[u],tp);
	for(int i=head[u];i;i=edge[i].next)
	{
		int e=edge[i].v;
		if(e==son[u]||e==fa[u]) continue;
		dfs2(e,e);
	}
}
void built(int p,int l,int r)
{
	if(l==r)
	{
		tree[p].l_c=tree[p].r_c=wt[l];
		tree[p].sum=1;
		return ;
	}
	int mid=(l+r)>>1;
	built(p<<1,l,mid);
	built(p<<1|1,mid+1,r);
	tree[p].l_c=tree[p<<1].l_c;
	tree[p].r_c=tree[p<<1|1].r_c;
	tree[p].sum=tree[p<<1].sum+tree[p<<1|1].sum-(tree[p<<1].r_c==tree[p<<1|1].l_c?1:0);
}
void down(int p,int l,int r)
{
	if(tree[p].tag==1)
	{
		tree[p<<1].l_c=tree[p<<1|1].l_c=tree[p].l_c;
		tree[p<<1].r_c=tree[p<<1|1].r_c=tree[p].r_c;
		tree[p<<1].sum=tree[p<<1|1].sum=1;
		tree[p<<1].tag=tree[p<<1|1].tag=1;
		tree[p].tag=0;
	}
}
void up(int p,int l,int r)
{
	tree[p].l_c=tree[p<<1].l_c;
	tree[p].r_c=tree[p<<1|1].r_c;
	tree[p].sum=tree[p<<1].sum+tree[p<<1|1].sum-(tree[p<<1].r_c==tree[p<<1|1].l_c?1:0);
}
void update(int p,int x,int y,int l,int r,int k)
{
	if(x<=l&&r<=y)
	{
		tree[p].sum=tree[p].tag=1;
		tree[p].l_c=tree[p].r_c=k;
		return ;
	}
	down(p,l,r);
	int mid=(l+r)>>1;
	if(mid>=x) update(p<<1,x,y,l,mid,k);
	if(mid<y) update(p<<1|1,x,y,mid+1,r,k);
	up(p,l,r);
}
void update_L(int x,int y,int k)
{
	while(top[x]!=top[y])
	{
		if(dep[top[x]]<dep[top[y]]) swap(x,y);
		update(1,id[top[x]],id[x],1,n,k);
		x=fa[top[x]];
	}
	if(dep[x]>dep[y]) swap(x,y);
	update(1,id[x],id[y],1,n,k);
}
int query(int p,int x,int y,int l,int r)
{
	if(x<=l&&r<=y) return tree[p].sum;
	down(p,l,r);
	int mid=(l+r)>>1;
	int res=0,flag=0;
	if(mid>=x) flag++,res+=query(p<<1,x,y,l,mid);
	if(mid<y) flag++,res+=query(p<<1|1,x,y,mid+1,r);
	if(flag==2) res-=(tree[p<<1].r_c==tree[p<<1|1].l_c?1:0);
	return res;
}
int query_D(int p,int x,int y,int l,int r,int k)
{
	if(x<=l&&r<=y)
	{
		if(k==0) return tree[p].l_c;
		if(k==1) return tree[p].r_c;
		return false;
	}
	down(p,l,y);
	int mid=(l+r)>>1,res=0;
	if(mid>=x) res=query_D(p<<1,x,y,l,mid,k);
	if(mid<y) res=query_D(p<<1|1,x,y,mid+1,r,k);
	return res;
}
int query_L(int x,int y)
{
	int ans=0,res,l1=-1,l2=-1;
	while(top[x]!=top[y])
	{
		if(dep[top[x]]<dep[top[y]])
		{
			swap(x,y);
			swap(l1,l2);
		}
		res=query(1,id[top[x]],id[x],1,n);
		ans+=res;
		if(query_D(1,id[x],id[x],1,n,1)==l1) ans--;
		l1=query_D(1,id[top[x]],id[top[x]],1,n,0);
		x=fa[top[x]];
	}
	if(dep[x]>dep[y])
	{
		swap(x,y);
		swap(l1,l2);
	}
	res=query(1,id[x],id[y],1,n);
	ans+=res;
	if(query_D(1,id[y],id[y],1,n,1)==l1) ans--;
	if(query_D(1,id[x],id[x],1,n,0)==l2) ans--;
	return ans;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%d",&val[i]);
	for(int i=1;i<n;i++)
	{
		int u,v;
		scanf("%d%d",&u,&v);
		add(u,v); add(v,u);
	}
	dfs1(1,0,1);
	dfs2(1,1);
	built(1,1,n);
	//for(int i=1;i<=n;i++)
	//	cout<<wt[i]<<"  ";
	
	for(int i=1;i<=m;i++)
	{
		char opt;
		cin>>opt;
		if(opt=='C')
		{
			int x,y,k;
			scanf("%d%d%d",&x,&y,&k);
			update_L(x,y,k);
		}
		if(opt=='Q')
		{
			int x,y;
			scanf("%d%d",&x,&y);
			printf("%d\n",query_L(x,y));
		}
	}
	
	return 0;
}
2023/11/17 08:54
加载中...