萌新妹子的同桌,刚学oi,树剖90pts,TLE求助
查看原帖
萌新妹子的同桌,刚学oi,树剖90pts,TLE求助
225048
ghr_226楼主2020/5/4 19:26

rt

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#pragma GCC optimize("-fgcse")
#pragma GCC optimize("-fgcse-lm")
#pragma GCC optimize("-fipa-sra")
#pragma GCC optimize("-ftree-pre")
#pragma GCC optimize("-ftree-vrp")
#pragma GCC optimize("-fpeephole2")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-fsched-spec")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-falign-jumps")
#pragma GCC optimize("-falign-loops")
#pragma GCC optimize("-falign-labels")
#pragma GCC optimize("-fdevirtualize")
#pragma GCC optimize("-fcaller-saves")
#pragma GCC optimize("-fcrossjumping")
#pragma GCC optimize("-fthread-jumps")
#pragma GCC optimize("-funroll-loops")
#pragma GCC optimize("-fwhole-program")
#pragma GCC optimize("-freorder-blocks")
#pragma GCC optimize("-fschedule-insns")
#pragma GCC optimize("inline-functions")
#pragma GCC optimize("-ftree-tail-merge")
#pragma GCC optimize("-fschedule-insns2")
#pragma GCC optimize("-fstrict-aliasing")
#pragma GCC optimize("-fstrict-overflow")
#pragma GCC optimize("-falign-functions")
#pragma GCC optimize("-fcse-skip-blocks")
#pragma GCC optimize("-fcse-follow-jumps")
#pragma GCC optimize("-fsched-interblock")
#pragma GCC optimize("-fpartial-inlining")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-freorder-functions")
#pragma GCC optimize("-findirect-inlining")
#pragma GCC optimize("-fhoist-adjacent-loads")
#pragma GCC optimize("-frerun-cse-after-loop")
#pragma GCC optimize("inline-small-functions")
#pragma GCC optimize("-finline-small-functions")
#pragma GCC optimize("-ftree-switch-conversion")
#pragma GCC optimize("-foptimize-sibling-calls")
#pragma GCC optimize("-fexpensive-optimizations")
#pragma GCC optimize("-funsafe-loop-optimizations")
#pragma GCC optimize("inline-functions-called-once")
#pragma GCC optimize("-fdelete-null-pointer-checks")
#include<bits/stdc++.h>
using namespace std;
#define in Read()
#define re register
#define LL long long
#define lch p<<1
#define rch p<<1|1

inline char ch(){
	static char buf[1<<21],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;
}

inline int in{
	int s=0,f=1;char x;
	for(x=ch();x<'0'||x>'9';x=ch())	if(x=='-')	f=-1;
	for( ;x>='0'&&x<='9';x=ch())	s=(s*10)+(x&15);
	return f==1?s:-s;
}

const int A=3e5+5;
const LL mod=998244353;
int n,m;
int head[A],tot_road=0;
struct Road{
	int nex,to;
}road[2*A];
inline void ljb(int x,int y){
	road[++tot_road]={head[x],y};head[x]=tot_road;
} 
int f[A],dep[A],son[A],size[A],top[A],pos[A],idx[A],tot;
struct Tree{
	int l,r;
	LL val[55];
}tree[4*A];

inline void pushup(int p){
	for(int i=1;i<=50;i++)
		tree[p].val[i]=(tree[lch].val[i]+tree[rch].val[i])%mod;
	return;
}

inline void build(int p,int l,int r){
	tree[p].l=l,tree[p].r=r;
	if(l==r){
		tree[p].val[0]=1;
		for(int i=1;i<=50;i++)
			tree[p].val[i]=(tree[p].val[i-1]*dep[idx[l]])%mod;
		return;
	}
	int mid=(l+r)>>1;
	build(lch,l,mid),build(rch,mid+1,r);
	pushup(p);
	return;
}

inline LL qurey(int p,int l,int r,int k){
	if(tree[p].l>=l&&tree[p].r<=r)	return tree[p].val[k];
	LL ans=0;
	int mid=(tree[p].l+tree[p].r)>>1;
	if(l<=mid)	ans=(ans+qurey(lch,l,r,k))%mod;
	if(r>=mid+1)	ans=(ans+qurey(rch,l,r,k))%mod;
	return ans;
}

inline void DFS1(int fa,int x){
	f[x]=fa,dep[x]=dep[fa]+1,size[x]=1;
	for(int y=head[x];y;y=road[y].nex){
		int z=road[y].to;
		if(z==fa)	continue;
		DFS1(x,z);
		size[x]+=size[z];
		if(size[z]>size[son[x]])	son[x]=z;
	}
	return;
}

inline void DFS2(int x){
	pos[x]=++tot,idx[pos[x]]=x;
	if(son[x]){
		top[son[x]]=top[x];
		DFS2(son[x]);
	}
	for(int y=head[x];y;y=road[y].nex){
		int z=road[y].to;
		if(top[z])	continue;
		top[z]=z;
		DFS2(z);
	}
	return;
}

inline void tree_cut(){
	dep[0]=-1;
	DFS1(0,1);
	top[1]=1;
	DFS2(1);
	return;
}

inline LL qurey_road(int x,int y,int k){
	LL ans=0;
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]])	swap(x,y);
		ans=(ans+qurey(1,pos[top[x]],pos[x],k))%mod;
		x=f[top[x]];
	}
	if(dep[x]>dep[y])	swap(x,y);
	ans=(ans+qurey(1,pos[x],pos[y],k))%mod;
	return (ans%mod+mod)%mod;
}

signed main(){
	n=in;
	for(int i=1;i<n;i++){
		int u=in,v=in;
		ljb(u,v),ljb(v,u);
	}
	tree_cut();
	build(1,1,n);
	m=in;
	while(m--){
		int u=in,v=in,k=in;
		printf("%lld\n",qurey_road(u,v,k));
	}
	return 0;
}
2020/5/4 19:26
加载中...