QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#220083#5372. 杀蚂蚁简单版DaiRuiChen00710 66ms23996kbC++141.7kb2023-10-19 21:42:052023-10-19 21:42:05

Judging History

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

  • [2023-10-19 21:42:05]
  • 评测
  • 测评结果:10
  • 用时:66ms
  • 内存:23996kb
  • [2023-10-19 21:42:05]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e5+5,MOD=998244353;
inline int ksm(int a,int b=MOD-2,int p=MOD) {
	int ret=1;
	for(;b;a=1ll*a*a%p,b=b>>1) if(b&1) ret=1ll*ret*a%p;
	return ret;
}
int p[MAXN],tot[MAXN],s1[MAXN],s2[MAXN],s3[MAXN];
int st[MAXN][20],dfn[MAXN],rdfn[MAXN],dcnt,Fa[MAXN];
inline int cmp(int x,int y) { return dfn[x]<dfn[y]?x:y; }
vector <int> G[MAXN];
inline void dfs(int u,int fa) {
	dfn[u]=++dcnt,st[dcnt][0]=Fa[u]=fa;
	for(int v:G[u]) tot[u]=(tot[u]+p[v])%MOD;
	s1[u]=ksm(1ll*p[u]*p[fa]%MOD);
	s2[u]=(s2[fa]+1ll*tot[u]*p[u])%MOD;
	s3[u]=(s3[fa]+1ll*tot[u]*p[u]%MOD*s1[u])%MOD;
	s1[u]=(s1[u]+s1[fa])%MOD;
	for(int v:G[u]) if(v^fa) dfs(v,u);
	rdfn[u]=dcnt;
}
inline int bit(int k) { return 1<<k; }
inline int LCA(int u,int v) {
	if(u==v) return u;
	int l=min(dfn[u],dfn[v])+1,r=max(dfn[u],dfn[v]),k=__lg(r-l+1);
	return cmp(st[l][k],st[r-bit(k)+1][k]);
}
signed main() {
//	freopen("ant.in","r",stdin);
//	freopen("ant.out","w",stdout);
	int n,q;
	scanf("%d",&n);
	for(int i=1;i<=n;++i) scanf("%d",&p[i]);
	for(int i=1,u,v;i<n;++i) scanf("%d%d",&u,&v),G[u].push_back(v),G[v].push_back(u);
	dfs(1,0);
	for(int k=1;k<20;++k) for(int i=1;i+bit(k-1)<=n;++i) st[i][k]=cmp(st[i][k-1],st[i+bit(k-1)][k-1]);
	scanf("%d",&q);
	for(int s,x,y;q--;) {
		scanf("%d%d%d",&s,&x,&y);
		int k=LCA(x,y);
		if(dfn[k]<=dfn[s]&&dfn[s]<=rdfn[k]) {
			if(LCA(s,x)==k) swap(x,y);
			int z=LCA(s,x);
			printf("%lld\n",(1ll*s1[z]*(s2[x]+MOD-s2[z])%MOD+(s3[z]+MOD-s3[Fa[k]])+1ll*s1[k]*(s2[y]+MOD-s2[k])%MOD)%MOD);
		} else {
			int z=LCA(s,k);
			printf("%lld\n",1ll*s1[z]*(s2[x]+s2[y]+MOD*2-s2[k]-s2[Fa[k]])%MOD);
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Subtask #1:

score: 10
Accepted

Test #1:

score: 10
Accepted
time: 1ms
memory: 8180kb

input:

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

output:

4

result:

ok single line: '4'

Test #2:

score: 0
Accepted
time: 1ms
memory: 9104kb

input:

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

output:

5

result:

ok single line: '5'

Test #3:

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

input:

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

output:

5

result:

ok single line: '5'

Test #4:

score: 0
Accepted
time: 1ms
memory: 8288kb

input:

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

output:

5

result:

ok single line: '5'

Test #5:

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

input:

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

output:

6

result:

ok single line: '6'

Test #6:

score: 0
Accepted
time: 1ms
memory: 9228kb

input:

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

output:

6

result:

ok single line: '6'

Test #7:

score: 0
Accepted
time: 1ms
memory: 8612kb

input:

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

output:

3

result:

ok single line: '3'

Test #8:

score: 0
Accepted
time: 1ms
memory: 9400kb

input:

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

output:

2

result:

ok single line: '2'

Subtask #2:

score: 0
Wrong Answer

Dependency #1:

100%
Accepted

Test #9:

score: 0
Wrong Answer
time: 21ms
memory: 7676kb

input:

100
19082214 4807200 18093892 10128508 6278227 3082640 18435935 14945221 17570657 10054626 13020781 18926050 4884255 5459284 10075080 18518993 16590687 4103633 1887923 12545378 14553360 14115988 766212 14352785 1482486 15388118 16751947 3735054 16071111 661078 10093466 972154 5931449 18167107 109818...

output:

584487649
105375689
183540399
836906770
807065977
86021271
888605614
362898916
717572601
171877756
169742897
568399543
337164853
253104356
2092706
800994587
454522326
214671113
261859742
813312795
853226665
927653977
759323832
758339000
478192913
738386348
620107133
860692893
878764850
578900045
521...

result:

wrong answer 41st lines differ - expected: '721800077', found: '-385592884'

Subtask #3:

score: 0
Wrong Answer

Test #18:

score: 0
Wrong Answer
time: 66ms
memory: 23996kb

input:

100000
13643 13546 7538 2233 7731 14619 19601 8438 9556 19888 17313 1060 15168 11207 11183 16074 10758 7469 13444 9658 18326 4735 7542 13836 5863 7903 7212 14714 10416 18506 13435 14502 15271 13205 14887 18074 8353 19807 1767 19148 7343 10823 14211 66 17168 8305 1210 5436 18552 3659 886 18416 19261 ...

output:

470097670
143314895
319664187
-334970487
-599656204
-774936854
490172883
264410099
250850293
913408788
466102803
464951422
764468729
-568840123
475030770
300112112
603892921
-577505407
967440122
1264162
552912646
-300329097
721517270
275076549
191931901
678760307
278036996
360130747
-533392404
-8333...

result:

wrong answer 4th lines differ - expected: '989628182', found: '-334970487'

Subtask #4:

score: 0
Skipped

Dependency #3:

0%

Subtask #5:

score: 0
Skipped

Dependency #2:

0%

Subtask #6:

score: 0
Skipped

Dependency #1:

100%
Accepted

Dependency #2:

0%