蒟蒻求助,只过了一个点,剩下全TLE
查看原帖
蒟蒻求助,只过了一个点,剩下全TLE
375668
Leville楼主2021/7/15 16:37
#include<cstdio>
#include<iostream>
using namespace std;
const int N=100001;
struct Edge{
	int to,next;
}edge[N<<1];
void read(int &x){
	char ch,last=' ';
	for(ch=getchar();ch<'0'||ch>'9';ch=getchar())
		last=ch;
	x=ch-'0';
	for(ch=getchar();ch>='0'&&ch<='9';ch=getchar())
		x=x*10+ch-'0';
	if(last=='-')
		x=-x;
}
int head[N],n,q,num,w[N],x,y,z;
int father[N],son[N],size[N],depth[N];
int top[N],a[N],pos[N],idx;
int tree[N<<2];
inline void add(int from,int to){
	edge[++num].to=to;
	edge[num].next=head[from];
	head[from]=num;
}
void init(){
	read(n);read(q);
	for(int t=1;t<=n;t++)
		read(w[t]);
	for(int t=1,a,b;t<n;t++){
		read(a);read(b);
		add(a,b);
		add(b,a);
	}
}
void dfs1(int u,int fa){
	father[u]=fa;
	depth[u]=depth[fa]+1;
	size[u]=1;
	for(int t=head[u],v;t;t=edge[t].next){
		v=edge[t].to;
		if(fa^v){
			dfs1(v,u);
			size[u]+=size[v];
			if(size[v]>size[son[u]])
				son[u]=v;
		}
	}
}
void dfs2(int u){
	if(son[u]){
		top[son[u]]=top[u];
		a[++idx]=son[u];
		pos[son[u]]=idx;
		dfs2(son[u]);
	}
	for(int t=head[u],v;t;t=edge[t].next){
		v=edge[t].to;
		if(!top[v]){
			top[v]=v;
			a[++idx]=v;
			pos[v]=idx;
			dfs2(v);
		}
	}
}
inline void up(int root){
	tree[root]=tree[root<<1]^tree[root<<1|1];
}
void create(int root,int l,int r){
	if(l==r){
		tree[root]=w[a[l]];
		return;
	}
	int mid=l+r>>1;
	create(root<<1,l,mid);
	create(root<<1|1,mid+1,r);
	up(root);
}
void update(int root,int l,int r,int x,int y){
	if(l==r){
		tree[root]=y;
		return;
	}
	int mid=l+r>>1;
	if(x>mid)
		update(root<<1|1,mid+1,r,x,y);
	else update(root<<1,l,mid,x,y);
	up(root);
}
int query(int root,int l,int r,int x,int y){
	if(l>y||r<x)
		return 0;
	if(x<=l&&r<=y)
		return tree[root];
	int mid=l+r>>1;
	return query(root<<1,l,mid,x,y)^query(root<<1|1,mid+1,r,x,y);
}
void chaxun(int u,int v){
	int ans=0;
	while(top[u]^top[v]){
		if(depth[top[u]]<depth[top[v]])
			swap(u,v);
		ans^=query(1,1,n,pos[top[u]],pos[u]);
	}
	if(depth[u]>depth[v])
		swap(u,v);
	ans^=query(1,1,n,pos[u],pos[v]);
	printf("%d\n",ans);
}
void solve(){
	while(q--){
		read(x);read(y);read(z);
		if(x==1)
			update(1,1,n,pos[y],z);
		else chaxun(y,z);
	}
}
int main(){
	init();
	depth[0]=-1;
	dfs1(1,0);
	top[1]=1;a[1]=1;pos[1]=1;idx=1;
	dfs2(1);
	create(1,1,n);
	solve();
}
2021/7/15 16:37
加载中...