QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#104653 | #6322. Forestry | yxzy | AC ✓ | 1265ms | 95164kb | C++14 | 4.2kb | 2023-05-11 16:04:58 | 2023-05-11 16:05:00 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const ll inf=1.1e18;
namespace io{
const int S=1<<20;
char ib[S],*s=ib,*t=ib;
inline char gc(){ return (s==t)&&(t=(s=ib)+fread(ib,1,S,stdin)),s==t?-1:*s++; }
inline ll read(){
ll x=0,f=1;char c=gc();
while(c<'0'||c>'9') f=(c=='-')?-1:1,c=gc();
while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c&15),c=gc();
return x*f;
}
char ob[S],*p=ob;
inline void flush(){fwrite(ob,p-ob,1,stdout),p=ob;}
inline void pt(char x){(p-ob>=S)&&(flush(),1);*p++=x;}
inline void write(ll x){
static char t[46];static int ct=0;
if(x<0) pt('-'),x=-x;
do{t[++ct]=x%10+'0';x/=10;}while(x);
while(ct) pt(t[ct]),ct--;
}
}
using io::read;using io::pt;using io::write;
const int N=3e5+10,p=998244353,iv2=(p+1)>>1;
inline void ad(int &x,int y){(x+=y)>=p&&(x-=p);}
inline int qpow(int x,int y){
int s=1;while(y){
if(y&1) s=1ll*s*x%p;
x=1ll*x*x%p;y>>=1;
}return s;
}
int n,a[N],id[N],sz[N],zs[N],d[N],f[N],s[N];vi to[N];
inline void dfs(int x,int fa){
sz[x]=d[x]=1;f[x]=fa;
for(int y:to[x]){
if(y==fa) continue;
dfs(y,x);sz[x]+=sz[y];ad(s[x],s[y]),d[x]=1ll*d[x]*(d[y]+1)%p*iv2%p;
if(sz[y]>sz[zs[x]]) zs[x]=y;
}ad(s[x],d[x]);
}
int dfn[N],w[N],ct,top[N],ed[N],dp[N],S[N];
inline void dfs2(int x){
w[dfn[x]=++ct]=x;dp[x]=1;
if(zs[x]) top[zs[x]]=top[x],dfs2(zs[x]),ed[x]=ed[zs[x]];
else ed[x]=x;
for(int y:to[x]){
if(y==f[x]||y==zs[x]) continue;
top[y]=y;dfs2(y);dp[x]=1ll*dp[x]*(d[y]+1)%p*iv2%p;ad(S[x],s[y]);
}if(zs[x]) dp[x]=1ll*dp[x]*iv2%p;
}
struct DP{
int d[2][2];
inline DP(){d[0][0]=d[0][1]=d[1][0]=d[1][1]=0;}
inline DP(int x,int y){d[0][0]=d[0][1]=d[1][0]=d[1][1]=x,ad(d[1][0],y);}
DP operator * (DP x){
DP y;
y.d[0][0]=(d[0][0]+1ll*d[0][1]*x.d[0][0])%p;
y.d[0][1]=1ll*d[0][1]*x.d[0][1]%p;
y.d[1][0]=(d[1][0]+1ll*d[1][1]*x.d[0][0]+x.d[1][0])%p;
y.d[1][1]=(1ll*d[1][1]*x.d[0][1]+x.d[1][1])%p;
return y;
}
inline void out(){cout<<d[0][0]<<" "<<d[0][1]<<" "<<d[1][0]<<" "<<d[1][1]<<endl;}
}t[N<<2];
inline void pu(int p){ t[p]=t[p<<1|1]*t[p<<1];}
void build(int p,int l,int r){
if(l==r) return t[p]=DP(dp[w[l]],S[w[l]]),void();
int mid=(l+r)>>1;
build(p<<1,l,mid),build(p<<1|1,mid+1,r);pu(p);
}
void add(int p,int l,int r,int x,int y,int z){
// cout<<"add:"<<p<<" "<<l<<" "<<r<<" "<<x<<endl;t[p].out();
if(l==r) return t[p]=DP(y,z),/*cout<<"change:"<<p<<" ",t[p].out(),*/void();
int mid=(l+r)>>1;
x<=mid?add(p<<1,l,mid,x,y,z):add(p<<1|1,mid+1,r,x,y,z);pu(p);
// cout<<"up:"<<p<<" ";t[p].out();
}
DP ask(int p,int l,int r,int pl,int pr){
if(pl<=l&&r<=pr) return t[p];
int mid=(l+r)>>1;
if(mid>=pr) return ask(p<<1,l,mid,pl,pr);
if(mid<pl) return ask(p<<1|1,mid+1,r,pl,pr);
return ask(p<<1|1,mid+1,r,pl,pr)*ask(p<<1,l,mid,pl,pr);
}
int v[N],ans;
inline void del(int x){
// cout<<"del :"<<x<<endl;
int y;DP tp;v[x]=1;
while(x){
add(1,1,n,dfn[x],v[x]?0:dp[x],S[x]);
y=top[x],tp=ask(1,1,n,dfn[y],dfn[ed[y]]);
if(f[y]) dp[f[y]]=1ll*dp[f[y]]*qpow((d[y]+1),p-2)%p,ad(S[f[y]],p-s[y]);
s[y]=tp.d[1][0],d[y]=tp.d[1][1];x=f[y];
if(f[y]) dp[f[y]]=1ll*dp[f[y]]*(d[y]+1)%p,ad(S[f[y]],s[y]);
}
// cout<<"del end:"<<endl;
// for(int i=1;i<=n;i++) cout<<s[i]<<" "<<d[i]<<" "<<S[i]<<" "<<dp[i]<<endl;
}
inline bool cmp(int x,int y){return a[x]^a[y]?a[x]<a[y]:x<y;}
int main(){
// freopen("1.in","r",stdin);
// freopen("1.out","w",stdout);
n=read();
for(int i=1;i<=n;i++) a[i]=read(),id[i]=i;
sort(id+1,id+1+n,cmp);
for(int i=1,x,y;i<n;i++) x=read(),y=read(),to[x].push_back(y),to[y].push_back(x);
dfs(1,0),top[1]=1,dfs2(1);build(1,1,n);
// for(int i=1;i<=n;i++) cout<<s[i]<<" "<<d[i]<<" "<<S[i]<<" "<<dp[i]<<" "<<id[i]<<endl;
ans=1ll*(s[1]+d[1])*iv2%p*a[id[1]]%p;
for(int i=1;i<n;i++) del(id[i]),ad(ans,1ll*(s[1]+d[1])*iv2%p*(a[id[i+1]]-a[id[i]])%p);
printf("%lld\n",1ll*ans*qpow(2,n-1)%p);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 46828kb
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: 1ms
memory: 42536kb
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: 1123ms
memory: 55952kb
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: 1177ms
memory: 56452kb
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: 1242ms
memory: 56780kb
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: 469ms
memory: 86824kb
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: 0
Accepted
time: 405ms
memory: 88532kb
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:
41386982
result:
ok 1 number(s): "41386982"
Test #8:
score: 0
Accepted
time: 413ms
memory: 95164kb
input:
299999 471542727 799095206 509787451 592650193 362215487 588125595 604003616 881525850 755688267 236898505 532366496 67948692 232297132 929458994 431732351 274008699 164112665 690178118 571148044 408207186 453143828 959612302 878551481 584372849 894288979 552607930 610390281 409926978 15831307 61533...
output:
479741390
result:
ok 1 number(s): "479741390"
Test #9:
score: 0
Accepted
time: 489ms
memory: 82656kb
input:
299997 25665195 607081146 2312642 326822363 385524999 209349813 211617900 372403090 573561165 779096327 304738950 525680732 809703693 740379205 642786351 334319127 24588707 570042249 298675391 92525045 882182405 255387261 629708545 662677567 102298185 416640032 800837034 517838306 180083897 65300699...
output:
756252561
result:
ok 1 number(s): "756252561"
Test #10:
score: 0
Accepted
time: 459ms
memory: 79152kb
input:
299997 772601140 677850510 486293752 38381420 713140958 613588331 933478663 892594986 40965372 719820852 371692421 950213365 627891743 923082750 330248475 635152312 968671306 91954505 646645243 266673541 419892861 468231632 676898301 475369890 173394153 206234682 614218712 386714613 691986706 401543...
output:
909256779
result:
ok 1 number(s): "909256779"
Test #11:
score: 0
Accepted
time: 476ms
memory: 57824kb
input:
300000 489134030 28527536 706816680 898196399 298100900 573504624 817242924 202561288 93577170 123268581 618798194 660515267 184003900 797853604 19557769 572066362 795291782 626861802 147727524 877230625 221524908 456496916 339550982 405198767 983242265 794896971 192309413 938450729 890444371 624597...
output:
469866382
result:
ok 1 number(s): "469866382"
Test #12:
score: 0
Accepted
time: 1257ms
memory: 57640kb
input:
294048 940092908 294031597 203789774 965832063 294368312 530860757 29650208 19058325 908258800 103593701 42838422 924175435 729123020 53690401 645412066 388715095 613747643 989874415 525317950 992107991 110671090 174219963 334698216 629961584 722552204 684114784 157441276 972553266 734116607 6480471...
output:
869124246
result:
ok 1 number(s): "869124246"
Test #13:
score: 0
Accepted
time: 1265ms
memory: 56704kb
input:
294169 829764052 27629554 569458841 732262994 789440677 560991159 427506198 382533265 982599986 867072676 292427700 863455778 358352325 895409930 503249204 713052609 50315636 478399531 724940834 394303205 658023052 804043479 566808951 672917322 636437946 905749010 875678660 8659223 349546005 8117721...
output:
603293934
result:
ok 1 number(s): "603293934"
Test #14:
score: 0
Accepted
time: 1243ms
memory: 56336kb
input:
274214 936267909 35123462 801828019 44784239 618602008 669685848 531693333 374691550 641907521 460643008 895242266 178217314 865256487 210105123 791635414 102380409 605115311 319289854 173186400 301848535 470317014 68928307 385895216 80699081 351459001 505538791 208284669 800278979 952086428 4182918...
output:
344857557
result:
ok 1 number(s): "344857557"
Test #15:
score: 0
Accepted
time: 378ms
memory: 56320kb
input:
300000 300000 299999 299998 299997 299996 299995 299994 299993 299992 299991 299990 299989 299988 299987 299986 299985 299984 299983 299982 299981 299980 299979 299978 299977 299976 299975 299974 299973 299972 299971 299970 299969 299968 299967 299966 299965 299964 299963 299962 299961 299960 299959...
output:
8100800
result:
ok 1 number(s): "8100800"