QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#218378#2918. Tree Number Generatorucup-team1004WA 3ms53052kbC++141.9kb2023-10-18 09:11:592023-10-18 09:11:59

Judging History

你现在查看的是最新测评结果

  • [2023-10-18 09:11:59]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:53052kb
  • [2023-10-18 09:11:59]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<typename T>
ostream& operator << (ostream &out,const vector<T>&x){
	if(x.empty())return out<<"[]";
	out<<'['<<x[0];
	for(int len=x.size(),i=1;i<len;i++)out<<','<<x[i];
	return out<<']';
}
template<typename T>
vector<T> ary(const T *a,int l,int r){
	return vector<T>{a+l,a+1+r};
}
template<typename T>
void debug(T x){
	cerr<<x<<'\n';
}
template<typename T,typename ...S>
void debug(T x,S ...y){
	cerr<<x<<' ',debug(y...);
}
const int N=2e5+10,K=__lg(N)+2;
int n,m,mod,pw[N];
vector<int>to[N];
struct zj{
	int val,len;
};
zj operator + (const zj &a,const zj &b){
	return {int((1ll*a.val*pw[b.len]+b.val)%mod),a.len+b.len};
}
int anc[K][N],dep[N];
zj f[K][N],g[K][N];
void dfs(int u,int fa=0){
	anc[0][u]=fa,dep[u]=dep[fa]+1;
	for(int v:to[u])if(v^fa){
		dfs(v,u);
	}
}
zj calc(int u,int v){
	zj a={0,0},b={0,0};
	for(int x=dep[u]-dep[v];x>0;x^=x&-x){
		int i=__builtin_ctz(x);
		a=a+f[i][u],u=anc[i][u];
	}
	for(int x=dep[v]-dep[u];x>0;x^=x&-x){
		int i=__builtin_ctz(x);
		b=g[i][v]+b,u=anc[i][v];
	}
	if(u==v){
		// debug(a.val,a.len,b.val,b.len);
		return a+f[0][u]+b;
	}
	for(int i=__lg(dep[u]);~i;i--){
		if(anc[i][u]^anc[i][v]){
			a=a+f[i][u],b=g[i][u]+b;
			u=anc[i][u],v=anc[i][v];
		}
	}
	return a+f[1][u]+g[0][v]+b;
}
int main(){
	scanf("%d%d%d",&n,&mod,&m);
	for(int i=1,u,v;i<n;i++){
		scanf("%d%d",&u,&v);
		to[u].push_back(v),to[v].push_back(u);
	}
	for(int i=pw[0]=1;i<=n;i++)pw[i]=pw[i-1]*10ll%mod;
	dfs(1);
	for(int i=1,x;i<=n;i++){
		scanf("%d",&x);
		f[0][i]=g[0][i]={x%mod,1};
	}
	for(int i=1;(1<<i)<=n;i++){
		for(int u=1;u<=n;u++){
			int t=anc[i-1][u];
			anc[i][u]=anc[i-1][t];
			f[i][u]=f[i-1][u]+f[i-1][t];
			g[i][u]=g[i-1][t]+g[i-1][u];
		}
	}
	for(int u,v;m--;){
		scanf("%d%d",&u,&v);
		printf("%d\n",calc(u,v).val);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 16180kb

input:

2 1 4
1 2
1
3
1 2
2 1
1 1
2 2

output:

0
0
0
0

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 18936kb

input:

3 10 5
1 2
2 3
0
0
0
1 3
3 1
2 2
2 3
2 1

output:

0
0
0
0
0

result:

ok 5 lines

Test #3:

score: -100
Wrong Answer
time: 0ms
memory: 53052kb

input:

2000 2 2000
937 1471
1672 937
356 1672
976 356
1257 356
1503 1257
783 1503
1783 937
1284 976
1955 1503
802 1257
583 1471
526 356
701 1783
393 1955
307 1955
386 1955
1070 937
1724 802
1397 1724
1140 937
422 526
1941 1955
1638 937
1469 526
1800 526
1035 1800
1009 1140
1195 1800
142 1471
1225 1469
1524...

output:

0
1
1
1
0
1
1
0
0
1
1
1
0
1
0
1
0
1
1
1
1
1
1
0
1
0
1
0
1
0
1
1
1
1
0
1
1
1
0
0
1
1
0
1
1
1
0
1
1
1
1
0
0
1
1
1
0
1
1
0
0
0
0
1
0
1
1
0
0
0
1
1
1
1
1
1
0
0
1
1
1
1
1
1
0
0
0
1
1
1
1
0
0
1
1
0
1
1
0
1
0
0
0
1
1
0
1
0
1
0
1
0
0
1
0
0
0
1
0
1
1
0
1
1
0
0
1
1
1
1
0
0
1
1
1
1
0
1
1
0
1
1
0
0
1
1
1
0
0
1
...

result:

wrong answer 6th lines differ - expected: '0', found: '1'