mx求调代码
查看原帖
mx求调代码
309320
懵逼小蒟蒻楼主2022/7/17 16:50

树剖求调 最小的样例过了,大样例输出错误,评测全WA,实在调不出来了,哪位好心人能帮忙看一下(

#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int n,tt,m;
struct E{
	int v,nt;
}e[200005];
int hd[maxn],cnt=0;
int dep[maxn],nid[maxn],son[maxn],fa[maxn],topf[maxn],sz[maxn],ct=0;
struct T{
	int l,r,lc,rc,ans,mid,tg;
}t[400005];
void add(int uu,int vv){
	e[cnt].v = vv;
	e[cnt].nt = hd[uu];
	hd[uu]=cnt++;
}
void dfs1(int p,int f,int ndep){
	dep[p] = ndep;
	fa[p] = f;
	sz[p]=1;
	son[p]=0;
	int maxs=-1;
	for(int i = hd[p];i;i = e[i].nt){
		if(e[i].v==f)continue;
		dfs1(e[i].v,p,ndep+1);
		sz[p]+=sz[e[i].v];
		if(sz[e[i].v]>maxs){
			maxs = sz[e[i].v];son[p] = e[i].v;
		}
	}
}
void dfs2(int p,int topn){
	int y;
	topf[p] = topn;
	nid[p] = ++ct;
	if(!son[p])return;
	dfs2(son[p],topn);
	for(int i = hd[p];i;i = e[i].nt){
		y = e[i].v;
		if(y==fa[p]||y==son[p])continue;
		dfs2(y,y);
	}
}
void ud(int p){
	t[p].ans = t[p*2].ans+t[p*2+1].ans+((t[p*2].rc==t[p*2+1].lc)&&t[p*2].rc);
	t[p].lc = t[p*2].lc;t[p].rc = t[p*2+1].rc;
}
void tag(int p){
	if(t[p].tg){
		t[p*2].lc = t[p].tg;t[p*2].rc=t[p].tg;
		t[p*2].tg = t[p].tg;t[p*2].ans = t[p*2].r-t[p*2].l;
		t[p*2+1].lc = t[p].tg;t[p*2+1].rc = t[p].tg;
		t[p*2+1].tg = t[p].tg;t[p*2+1].ans = t[p*2+1].r-t[p*2+1].l;
		t[p].tg = 0;
	}                 
}
void build(int p,int l,int r){
	t[p].l = l;t[p].r = r;t[p].mid = (l+r)>>1;
	if(l==r){
		t[p].lc = 0;t[p].rc = 0;t[p].ans = 0;t[p].tg = 0;
		return;
	}
	build(p*2,l,t[p].mid);
	build(p*2+1,t[p].mid+1,r);
	ud(p);
}
T hb(T a,T b){
	if(!a.l)return b;
	if(!b.l)return a;
	T c;
	c.ans = a.ans + b.ans + ((a.rc==b.lc)&&a.rc);
	c.lc = a.lc;c.rc=  b.rc;
	return c;
}
T qr(int p,int l,int r){
	if(l<=t[p].l&&t[p].r<=r)return t[p];
	tag(p);
	T a={0},b={0};
	if(t[p].mid>=l){a = qr(p*2,l,r);}
	if(t[p].mid<r){b = qr(p*2+1,l,r);}
	return hb(a,b);
}
void au(int p,int l,int r,int c){
	if(t[p].l>=l&&t[p].r<=r){
		t[p].ans = t[p].r-t[p].l;
		t[p].lc = c;t[p].rc = c;
		t[p].tg = c;
		return;
	}
	tag(p);
	if(t[p].mid>=l)au(p*2,l,r,c);
	if(t[p].mid<r)au(p*2+1,l,r,c);
	ud(p);
}
int qrot(int x,int y){
	T ax={0,0,0,0,0,0,0},ay={0,0,0,0,0,0,0};
	while(topf[x]!=topf[y]){
		if(dep[topf[x]]<dep[topf[y]]){
			ay = hb(qr(1,nid[topf[y]],nid[y]),ay);
			y = fa[topf[y]];
		}else{
			ax = hb(qr(1,nid[topf[x]],nid[x]),ax);
			x = fa[topf[x]];
		}
	}
	if(dep[x]>dep[y])swap(x,y);
	T s = qr(1,nid[x],nid[y]);
	int ans=ax.ans+ay.ans+s.ans;
	ans+=((ax.lc==s.lc)&&ax.lc)+((s.rc==ay.lc)&&s.rc);
	return ans;
}
void auot(int x,int y,int c){
	while(topf[x]!=topf[y]){
		if(dep[topf[x]]<dep[topf[y]])swap(x,y);
		au(1,nid[topf[x]],nid[x],c);
		x = fa[topf[x]];
	}
	if(dep[x]>dep[y])swap(x,y);
	au(1,nid[x],nid[y],c);
}
int main(){
	//freopen("edge2.in","r",stdin);
	scanf("%d",&tt);
	int uu,vv,op,a,b,ncid=1;
	while(tt--){
		scanf("%d%d",&n,&m);
		memset(e,0,sizeof(e));
		memset(hd,0,sizeof(hd));
		cnt=1;ct=0;ncid=1;
		for(int i = 1;i<n;i++){
			scanf("%d%d",&uu,&vv);
			add(uu,vv);add(vv,uu);
		}
		dfs1(1,0,1);
		dfs2(1,1);
		build(1,1,n);
		while(m--){
			scanf("%d%d%d",&op,&a,&b);
			if(op-1){
				printf("%d\n",qrot(a,b));
			}else auot(a,b,++ncid);
		}
	}
	return 0;
}
2022/7/17 16:50
加载中...