QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#210927 | #6322. Forestry | ucup-team870 | WA | 883ms | 554148kb | C++17 | 2.4kb | 2023-10-11 21:34:06 | 2023-10-11 21:34:07 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define IOS {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);}
#define rep(i,j,k) for(int i=j;i<=k;++i)
#define per(i,j,k) for(int i=j;i>=k;--i)
#define P pair<int,int>
#define ll long long
#define vi vector<int>
#define mi int mid=(l+r)>>1;
// #define db double
const int N=3e5+5,V=62,mod=998244353,inf=1e9+7;
int ls[N*V],rs[N*V];
ll s1[N*V],s2[N*V],tag[N*V];
int Rt[N],a[N],tot;
ll qp(ll x,ll y){
ll res=1;
while(y){
if(y&1)res=res*x%mod;
x=x*x%mod; y>>=1;
}
return res;
}
const int i2=qp(2,mod-2);
vi tu[N];
void pu(int i){
s1[i]=(s1[ls[i]]+s1[rs[i]])%mod; s2[i]=(s2[ls[i]]+s2[rs[i]])%mod;
}
int crt(int rt,int l,int r,int x){
int now=++tot; tag[now]=1;
if(l==r){
s1[now]=1; s2[now]=l; return now;
}
mi
if(mid>=x)ls[now]=crt(ls[rt],l,mid,x),rs[now]=rs[rt];
else rs[now]=crt(rs[rt],mid+1,r,x),ls[now]=ls[rt];
pu(now);
return now;
}
void ud(int i,ll v){
tag[i]=tag[i]*v%mod; s1[i]=s1[i]*v%mod; s2[i]=s2[i]*v%mod;
}
void pd(int i){
ud(ls[i],tag[i]); ud(rs[i],tag[i]); tag[i]=1;
}
int mg(int rt1,int rt2,int l,int r,ll v1,ll v2){
if(!rt1){
ud(rt2,v1); return rt2;
}
if(!rt2){
ud(rt1,v2); return rt1;
}
if(l==r){
ud(rt1,(v2+s1[rt2])%mod); ud(rt2,v1);
s1[rt1]=(s1[rt1]+s1[rt2])%mod; s2[rt1]=(s2[rt1]+s2[rt2])%mod; return rt1;
}
mi
pd(rt1); pd(rt2);
ls[rt1]=mg(ls[rt1],ls[rt2],l,mid,v1+s1[rs[rt1]],v2+s1[rs[rt2]]);
rs[rt1]=mg(rs[rt1],rs[rt2],mid+1,r,v1,v2);
pu(rt1); return rt1;
}
ll ans;
void dfs1(int rt,int l,int r){
if(!rt)return;
if(l==r){
cout<<l<<' '<<s1[rt]<<' '<<s2[rt]<<'\n';
}
mi
pd(rt);
dfs1(ls[rt],l,mid);
dfs1(rs[rt],mid+1,r);
}
void dfs(int now,int pre){
Rt[now]=crt(0,1,inf,a[now]);
for(int son:tu[now]){
if(son==pre)continue;
dfs(son,now);
ud(Rt[now],i2);
Rt[son]=crt(Rt[son],1,inf,inf);
Rt[now]=mg(Rt[now],Rt[son],1,inf,0,0);
}
ans=(ans+(now==1?1:i2)*s2[Rt[now]])%mod;
// cout<<now<<"####\n";
// dfs1(Rt[now],1,inf);
}
signed main(){
// IOS
int n;cin>>n;
rep(i,1,n)cin>>a[i];
rep(i,2,n){
int u,v;cin>>u>>v;
tu[u].push_back(v); tu[v].push_back(u);
}
dfs(1,0);
cout<<ans*qp(2,n-1)%mod;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 17880kb
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: 0ms
memory: 18004kb
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: 883ms
memory: 554148kb
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:
163535024
result:
wrong answer 1st numbers differ - expected: '434293031', found: '163535024'