样例没过,求调
查看原帖
样例没过,求调
978088
Emily666西瓜π楼主2024/9/30 20:34

样例的第二行一直输出 77,调不出来,有没有大佬能帮看一下,大概率会关注。可能犯了一些神奇低级的错误,但我看久了就找不出来了QAQ

#include <iostream>
#include <vector>
using namespace std;
const int N=1e5+3;
int n,m,r,q,op,x,y,z;
int sz[N],dep[N],son[N],fa[N],top[N];
int id[N],st[N],tot,b[N];
vector<int>v[N];
struct node{
	int l,r;
	long long x,lz;
}a[N<<2];
void dfs(int x,int f){
	sz[x]=1;
	for(int y:v[x]){
		if(y==f) continue;
		dep[y]=dep[x]+1;
		fa[y]=x;
		dfs(y,x);
		sz[x]+=sz[y];
		if(sz[son[x]]<sz[y]) son[x]=y;
	}
}
void dfs2(int x,int f,int tp){
	id[x]=++tot,st[tot]=x;
	top[x]=tp;
	if(son[x]) dfs2(son[x],x,tp);
	for(int y:v[x]){
		if(y==f||y==son[x]) continue;
		dfs2(y,x,y);
	}
}

void build(int p,int l,int r){
	a[p].l=l,a[p].r=r;
	if(l==r){
		a[p].x=b[st[l]];
		return;
	}
	int mid=(l+r)>>1;
	build(p<<1,l,mid);
	build(p<<1|1,mid+1,r);
	a[p].x=a[p<<1].x+a[p<<1|1].x;
}
void push_down(int p){
	if(!a[p].lz) return;
	a[p<<1].lz+=a[p].lz;
	a[p<<1|1].lz+=a[p].lz;
	int mid=(a[p].l+a[p].r)>>1;
	a[p<<1].x+=a[p<<1].lz*(mid-a[p].l+1);
	a[p<<1|1].x+=a[p<<1|1].lz*(a[p].r-mid);
	a[p].lz=0;
}
void update(int p,int l,int r){
//	cout<<a[p].l<<" "<<a[p].r<<"\n"; 
	if(l<=a[p].l&&a[p].r<=r){
		a[p].x+=(a[p].r-a[p].l+1)*z;
		a[p].lz+=z;
		return;
	}
	push_down(p);
	int mid=(a[p].l+a[p].r)>>1;
	if(l<=mid) update(p<<1,l,r);
	if(mid<r) update(p<<1|1,l,r);
	a[p].x=a[p<<1].x+a[p<<1|1].x;
}
long long query(int p,int l,int r){
//	cout<<a[p].l<<" "<<a[p].r<<"\n";
	if(l<=a[p].l&&a[p].r<=r){
		return a[p].x;
	}
	push_down(p);
	int mid=(a[p].l+a[p].r)>>1;
	long long sum=0;
	if(l<=mid) sum+=query(p<<1,l,r);
	if(mid<r) sum+=query(p<<1|1,l,r);
//	a[p].x=a[p<<1].x+a[p<<1|1].x;
	return sum;
}

int LCA(int x,int y){
	while(top[x]!=top[y]){
		if(dep[top[x]]>dep[top[y]]) x=fa[top[x]];
		else y=fa[top[y]];
	}
	return (dep[x]<dep[y]?x:y);
}
void f1(){
	while(top[x]!=top[y]){
		if(dep[top[x]]>dep[top[y]]) update(1,id[top[x]],id[x]),x=fa[top[x]];
		else update(1,id[top[y]],id[y]),y=fa[top[y]];
	}
	if(id[x]<id[y]) update(1,id[x],id[y]);
	else update(1,id[y],id[x]);
}
int f2(){
	long long res=0;
	int cc=0;
	while(top[x]!=top[y]){
//		if(cc<=1) printf("%d %d %d %d\n",x,y,top[x],top[y]);
//		cout<<"$"<<x<<" "<<y<<" "<<top[x]<<" "<<top[y]<<"\n";
		if(dep[top[x]]>dep[top[y]]) res+=query(1,id[top[x]],id[x]),x=fa[top[x]];
		else res+=query(1,id[top[y]],id[y]),y=fa[top[y]];
		cc++;
	}
//	cout<<x<<" "<<y<<" "<<top[x]<<" "<<top[y]<<"\n";
	if(id[x]<id[y]) res+=query(1,id[x],id[y]);
	else res+=query(1,id[y],id[x]);
	return res;
}
void f3(){
//	while(top[x]!=top[y]){
//		if(dep[top[x]]>dep[top[y]]) update(1,id[top[x]],id[x]),x=fa[top[x]];
//		else update(1,id[top[y]],id[y]),y=fa[top[y]];
//	}
//	if(dep[x]<dep[y]) res+=query(1,id[x],id[y]);
//	else res+=query(1,id[y],id[x]);
//	cout<<"yes:"<<id[x]<<" "<<sz[x]-1<<"\n";
	update(1,id[x],id[x]+sz[x]-1);
}
long long f4(){
	return query(1,id[x],id[x]+sz[x]-1);
}
int main(){
	cin>>n>>m>>r>>q;
	for(int i=1;i<=n;i++) cin>>b[i];
	for(int i=1;i<n;i++){
		cin>>x>>y;
		v[x].push_back(y);
		v[y].push_back(x);
	}
	build(1,1,n);
	dfs(r,0);
	dfs2(r,0,r);
//	for(int i=1;i<=n;i++) cout<<sz[i]<<" ";
//	cout<<"\n";
	while(m--){
		cin>>op>>x;
		if(op==1){
			cin>>y>>z;
//			cout<<"         "<<LCA(x,y)<<"\n";
			f1();
		}
		else if(op==2){
			cin>>y;
//			cout<<"asdfasdfasdf";
//			cout<<"############### "<<LCA(x,y)<<"\n";
			cout<<f2()%q<<"\n";
		}
		else if(op==3){
			cin>>z;
			f3();
		}
		else cout<<f4()%q<<"\n";
	}
	return 0;
}

注释凌乱敬请谅解~

2024/9/30 20:34
加载中...