QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#220065 | #5372. 杀蚂蚁简单版 | DaiRuiChen007 | 0 | 57ms | 23808kb | C++14 | 1.6kb | 2023-10-19 21:24:55 | 2023-10-19 21:24:56 |
Judging History
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() {
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 p=LCA(s,x);
printf("%lld\n",(1ll*s1[p]*(s2[x]+MOD-s2[p])%MOD+(s3[p]+MOD-s3[Fa[k]])+1ll*s1[k]*(s2[y]+MOD-s2[k])%MOD)%MOD);
} else printf("%lld\n",1ll*s1[k]*(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: 0
Wrong Answer
Test #1:
score: 10
Accepted
time: 0ms
memory: 6272kb
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: -10
Wrong Answer
time: 1ms
memory: 9240kb
input:
5 1 1 1 1 1 1 2 2 3 3 4 4 5 1 2 5 3
output:
10
result:
wrong answer 1st lines differ - expected: '5', found: '10'
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Wrong Answer
Test #18:
score: 0
Wrong Answer
time: 57ms
memory: 23808kb
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:
520607507 48976391 788847470 -166885102 -600961863 -260970688 66774813 299504075 18852514 415474041 901422477 402361776 364997749 -469675023 253670627 56749936 986986650 -715693892 543469745 207740195 615768803 -414177952 336436210 873041072 823498630 844855532 288489357 52177230 -345880956 -4943092...
result:
wrong answer 1st lines differ - expected: '470097670', found: '520607507'
Subtask #4:
score: 0
Skipped
Dependency #3:
0%
Subtask #5:
score: 0
Skipped
Dependency #2:
0%
Subtask #6:
score: 0
Skipped
Dependency #1:
0%