QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#218379#2918. Tree Number Generatorucup-team1004WA 4ms9208kbC++141.9kb2023-10-18 09:12:312023-10-18 09:12:31

Judging History

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

  • [2023-10-18 09:12:31]
  • 评测
  • 测评结果:WA
  • 用时:4ms
  • 内存:9208kb
  • [2023-10-18 09:12:31]
  • 提交

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][v]+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;
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 8272kb

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: 8352kb

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: 0
Accepted
time: 4ms
memory: 9208kb

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
0
1
0
0
1
0
1
0
0
1
1
0
0
1
1
1
1
1
0
0
0
1
0
1
1
1
1
1
1
0
1
1
1
0
1
1
1
0
1
1
1
0
1
1
1
1
0
0
0
1
1
0
1
0
0
0
0
0
1
0
1
1
0
0
0
0
1
1
1
1
1
0
0
1
1
1
0
1
1
1
0
0
1
1
1
1
0
0
0
1
0
1
0
0
1
1
1
0
1
1
0
1
0
1
0
0
1
0
1
0
0
1
1
0
1
1
1
1
0
0
0
1
0
0
0
0
1
1
0
1
1
0
1
1
0
1
1
0
0
0
1
1
0
0
1
...

result:

ok 2000 lines

Test #4:

score: -100
Wrong Answer
time: 4ms
memory: 9184kb

input:

2000 1000 2000
1664 1028
763 1664
1541 1028
1544 1664
69 1544
1473 1541
475 1541
1551 1541
579 1473
262 475
1777 475
1916 475
391 763
807 1028
1357 1028
1682 69
345 1544
234 1541
63 391
480 807
1762 1544
306 1916
1436 1777
891 391
1852 306
523 1852
264 475
313 306
1139 391
1792 69
1604 69
398 313
10...

output:

263
299
976
566
31
40
168
28
487
588
273
218
944
664
988
55
709
900
61
311
421
664
632
388
766
282
455
61
430
984
899
436
780
641
69
126
255
88
629
603
666
57
555
843
55
811
144
988
804
290
666
422
929
899
716
466
371
620
252
606
690
500
122
226
495
800
161
580
505
132
423
845
618
622
244
729
637
89...

result:

wrong answer 2nd lines differ - expected: '429', found: '299'