QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#104441 | #6322. Forestry | Zesty_Fox | WA | 774ms | 274516kb | C++20 | 3.2kb | 2023-05-10 18:43:53 | 2023-05-10 18:43:58 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using u32=unsigned int;
using i64=long long;
using u64=unsigned long long;
using db=double;
using vi=vector<int>;
using pii=pair<int,int>;
template<typename T>
T read(){
T x=0,f=0;char ch=getchar();
while(!isdigit(ch)) f|=(ch=='-'),ch=getchar();
while(isdigit(ch)) x=x*10+(ch-'0'),ch=getchar();
return f?-x:x;
}
#define rdi read<int>
#define rdi64 read<i64>
#define fi first
#define se second
#define pb push_back
const int N=3e5+10,MOD=998244353;
#define lson (t[nw].ls)
#define rson (t[nw].rs)
#define mid ((l+r)>>1)
struct SGT{
struct Node{
int ls,rs;
i64 sp,sv,s,mu;
}t[N*60];
int tot;
int new_n(){
t[++tot]={0,0,0,0,0,1};
return tot;
}
void apply(int &nw,i64 v){
if(!nw) nw=new_n();
t[nw].sp=t[nw].sp*v%MOD;
t[nw].s=t[nw].s*v%MOD;
t[nw].mu=t[nw].mu*v%MOD;
}
void pushdown(int nw){
if(t[nw].mu!=1){
if(lson) apply(lson,t[nw].mu);
if(rson) apply(rson,t[nw].mu);
t[nw].mu=1;
}
}
void pushup(int nw){
t[nw].sp=(t[lson].sp+t[rson].sp)%MOD;
t[nw].sv=(t[lson].sv+t[rson].sv)%MOD;
t[nw].s=(t[lson].s+t[rson].s)%MOD;
}
void merge(int &nw,int nw1,int l,int r,i64 &s1,i64 &s2){
if(!nw&&!nw1) return;
if(!nw){
nw=nw1,s2=(s2+t[nw].sp)%MOD;
apply(nw,s1);
return;
}
if(!nw1){
s1=(s1+t[nw].sp)%MOD;
apply(nw,1+s2);
return;
}
if(l==r){
i64 p=(t[nw].sp+(s1+t[nw].sp)*(s2+t[nw1].sp)-s1*s2)%MOD;
s1=(s1+t[nw].sp)%MOD,s2=(s2+t[nw1].sp)%MOD;
t[nw].sp=p,t[nw].s=p*t[nw].sv%MOD;
return;
}
pushdown(nw),pushdown(nw1);
merge(rson,t[nw1].rs,mid+1,r,s1,s2);
merge(lson,t[nw1].ls,l,mid,s1,s2);
pushup(nw);
}
void set(int &nw,int l,int r,int x,i64 v){
if(!nw) nw=new_n();
if(l==r){
t[nw].sp=1,t[nw].sv=t[nw].s=v;
return;
}
pushdown(nw);
if(x<=mid) set(lson,l,mid,x,v);
else set(rson,mid+1,r,x,v);
pushup(nw);
}
}t;
#undef lson
#undef rson
#undef mid
int n;
int a[N];
vi e[N];
int raw[N],tot;
void discrete(){
for(int i=1;i<=n;i++)
raw[++tot]=a[i];
sort(raw+1,raw+tot+1),tot=unique(raw+1,raw+tot+1)-raw-1;
for(int i=1;i<=n;i++)
a[i]=lower_bound(raw+1,raw+n+1,a[i])-raw;
}
const int i2=(MOD+1)/2;
int rt[N];
i64 pw,ans;
void dfs(int x,int fa){
t.set(rt[x],1,tot,a[x],raw[a[x]]);
for(auto y:e[x]){
if(y==fa) continue;
dfs(y,x);
i64 s1=0,s2=0;
t.merge(rt[x],rt[y],1,tot,s1,s2);
t.apply(rt[x],i2);
}
ans+=t.t[rt[x]].s*(fa?i2:1)%MOD*pw%MOD;
}
int main(){
n=rdi();
for(int i=1;i<=n;i++)
a[i]=rdi();
for(int i=1;i<n;i++){
int x=rdi(),y=rdi();
e[x].pb(y),e[y].pb(x);
}
pw=1;
for(int i=1;i<n;i++)
pw=pw*2%MOD;
discrete();
dfs(1,0);
ans=(ans%MOD+MOD)%MOD;
cout<<ans<<endl;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 13548kb
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: 2ms
memory: 13520kb
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: 0
Accepted
time: 674ms
memory: 231308kb
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:
434293031
result:
ok 1 number(s): "434293031"
Test #4:
score: 0
Accepted
time: 708ms
memory: 238452kb
input:
287397 510502405 166707542 6543103 963754822 492178400 668790842 47929394 688150962 147460907 412966210 837275339 435773111 138898710 968087621 746522431 948665731 920170470 717898411 411586421 148909638 659985934 484520123 95176393 425866444 512660652 511428439 958021520 150660147 292591135 7320851...
output:
878336347
result:
ok 1 number(s): "878336347"
Test #5:
score: 0
Accepted
time: 762ms
memory: 244224kb
input:
294100 74665954 206916315 273249696 386095808 903673441 622958780 983022845 464633239 86441267 729372644 689111719 644187811 334408092 398907453 597161881 558735093 189557855 477930470 242405983 95068877 16413783 608746253 761602925 18584327 934502624 975194906 309225637 343248456 653012994 23118382...
output:
712256085
result:
ok 1 number(s): "712256085"
Test #6:
score: 0
Accepted
time: 774ms
memory: 273536kb
input:
300000 606908308 171374011 595912591 940182632 431995844 726762958 256169397 447385236 984023043 528258150 35565776 27503639 983714715 313579533 8771592 967838717 206597371 821802152 910792624 238535529 923693575 376608809 519872194 981808980 991017798 212487460 780752406 913659452 464143746 1561438...
output:
847230777
result:
ok 1 number(s): "847230777"
Test #7:
score: -100
Wrong Answer
time: 595ms
memory: 274516kb
input:
299999 782121216 968610368 25009662 478183585 274702257 773444727 687702634 507153995 474335571 202223165 211518854 313706139 936298699 536896304 702242243 590986189 912655919 596226813 737786290 917153157 990146015 268573292 418734072 3093396 94105534 834508206 491041607 939287910 88847044 81241220...
output:
684953902
result:
wrong answer 1st numbers differ - expected: '41386982', found: '684953902'