QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#103761 | #6322. Forestry | installb | WA | 338ms | 134564kb | C++20 | 1.8kb | 2023-05-07 14:31:08 | 2023-05-07 14:31:11 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define per(i,r,l) for(int i=(r);i>=(l);--i)
using namespace std;
const int N=3e5+10,P=998244353,M=1e6,i2=(P+1)/2;
int a[N],u,v,n,cnt,s1,s2,ans,rt[N];
vector<int> G[N];
int su[N*40],w[N*40],tg[N*40],ls[N*40],rs[N*40];
inline void up(int o){
su[o]=su[ls[o]]+su[rs[o]];
w[o]=(w[ls[o]]+w[rs[o]])%P;
}
inline void add_tg(int o,int c){
tg[o]=1ll*tg[o]*c%P;
su[o]=1ll*su[o]*c%P;
w[o]=1ll*w[o]*c%P;
}
inline void pd(int o){
if(tg[o]!=1){
if(ls[o]) add_tg(ls[o],tg[o]);
if(rs[o]) add_tg(rs[o],tg[o]);
tg[o]=1;
}
}
void upd(int &o,int l,int r,int x,int y){
if(!o) o=++cnt,tg[o]=1;
if(l==r){su[o]+=y,w[o]=(w[o]+1ll*x*y)%P;return;}
int mid=(l+r)>>1;pd(o);
x>mid?upd(rs[o],mid+1,r,x,y):upd(ls[o],l,mid,x,y);
up(o);
}
int merge(int x,int y,int l,int r){
if(!x){s2=(s2+su[y])%P;add_tg(y,1ll*i2*s1%P);return y;}
if(!y){s1=(s1+su[x])%P;add_tg(x,1ll*i2*s2%P);return x;}
if(l==r){
int p=su[x],q=su[y];
su[x]=(1ll*p*s2+1ll*q*s1+1ll*p*q)%P*i2%P;
w[x]=1ll*su[x]*l%P;
s1=(s1+p)%P,s2=(s2+q)%P;
return x;
}pd(x),pd(y);
int mid=(l+r)>>1;
rs[x]=merge(rs[x],rs[y],mid+1,r);
ls[x]=merge(ls[x],ls[y],l,mid);
up(x);return x;
}
void dfs(int u,int p){
upd(rt[u],1,M,a[u],1);
for(int &v:G[u]) if(v^p)
dfs(v,u),s1=0,s2=1,rt[u]=merge(rt[u],rt[v],1,M);
if(u!=1) ans=(ans+w[rt[u]])%P;
}
int main(){
//freopen("in","r",stdin);
scanf("%d",&n);
rep(i,1,n) scanf("%d",a+i);
rep(i,1,n-1) scanf("%d%d",&u,&v),G[u].push_back(v),G[v].push_back(u);
dfs(1,0);
ans=(ans+2ll*w[rt[1]])%P;
rep(i,1,n-2) ans=2*ans%P;
printf("%d\n",ans);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 18016kb
input:
4 1 2 3 4 1 2 2 4 3 2
output:
44
result:
ok 1 number(s): "44"
Test #2:
score: 0
Accepted
time: 3ms
memory: 18064kb
input:
5 3 5 6 5 1 4 1 2 3 3 5 1 3
output:
154
result:
ok 1 number(s): "154"
Test #3:
score: -100
Wrong Answer
time: 338ms
memory: 134564kb
input:
278989 864394090 384799247 186936242 598547507 962916136 540758021 158527118 688236322 682301622 948660645 881768181 481432818 557201870 794956026 205262301 920739629 141926922 990361777 818811172 150579096 1032726 328924563 638044961 21740781 484574329 737977343 113738003 345289940 363021157 582495...
output:
237822508
result:
wrong answer 1st numbers differ - expected: '434293031', found: '237822508'