QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#104604 | #6322. Forestry | Random_Code | AC ✓ | 1008ms | 301636kb | C++14 | 2.6kb | 2023-05-11 11:55:08 | 2023-05-11 11:55:11 |
Judging History
answer
#include <bits/stdc++.h>
#define mod 998244353
#define F first
#define S second
#define ll long long
#define N 300010
using namespace std;
ll inv2=(mod+1)/2;
struct SegT{
ll rt[N],tot,val[N*30],sum[N*30],pd[N*30],ch[N*30][2];
void init()
{
tot=0;
return;
}
void newroot(ll x)
{
rt[x]=++tot;
return;
}
void pushdown(ll x)
{
if(ch[x][0])
{
sum[ch[x][0]]=(sum[ch[x][0]]*pd[x])%mod;
val[ch[x][0]]=(val[ch[x][0]]*pd[x])%mod;
pd[ch[x][0]]=(pd[ch[x][0]]*pd[x])%mod;
}
if(ch[x][1])
{
sum[ch[x][1]]=(sum[ch[x][1]]*pd[x])%mod;
val[ch[x][1]]=(val[ch[x][1]]*pd[x])%mod;
pd[ch[x][1]]=(pd[ch[x][1]]*pd[x])%mod;
}
pd[x]=1;
return;
}
void update(ll x,ll l,ll r,ll ql,ll v)
{
if(l==ql&&r==ql)
{
pd[x]=val[x]=1;
sum[x]=v;
return;
}
pushdown(x);
ll mid=(l+r)>>1;
if(!ch[x][mid<ql])
{
ch[x][mid<ql]=++tot;
}
if(mid>=ql)
{
update(ch[x][0],l,mid,ql,v);
}
else
{
update(ch[x][1],mid+1,r,ql,v);
}
sum[x]=(sum[ch[x][0]]+sum[ch[x][1]])%mod;
val[x]=(val[ch[x][0]]+val[ch[x][1]])%mod;
return;
}
ll coef1,coef2;
ll Merge(ll x,ll y,ll l,ll r)
{
if((!x)||(!y))
{
if(x)
{
coef2=(coef2+val[x]*inv2)%mod;
val[x]=(val[x]*coef1)%mod;
sum[x]=(sum[x]*coef1)%mod;
pd[x]=(pd[x]*coef1)%mod;
}
if(y)
{
coef1=(coef1+val[y]*inv2)%mod;
val[y]=(val[y]*coef2)%mod;
sum[y]=(sum[y]*coef2)%mod;
pd[y]=(pd[y]*coef2)%mod;
}
return x|y;
}
pushdown(x);
pushdown(y);
assert(l!=r);
ll mid=(l+r)>>1;
ch[x][1]=Merge(ch[x][1],ch[y][1],mid+1,r);
ch[x][0]=Merge(ch[x][0],ch[y][0],l,mid);
val[x]=(val[ch[x][0]]+val[ch[x][1]])%mod;
sum[x]=(sum[ch[x][0]]+sum[ch[x][1]])%mod;
return x;
}
}segt;
ll n,a[N],id[N],ans=0;
vector<ll> vt[N];
vector<pair<ll,ll> > allv;
void dfs(ll x,ll lst)
{
ll i,j;
segt.newroot(x);
segt.update(segt.rt[x],0,n-1,id[x],a[x]);
for(i=0;i<vt[x].size();i++)
{
if(vt[x][i]!=lst)
{
dfs(vt[x][i],x);
segt.coef1=inv2,segt.coef2=0;
segt.rt[x]=segt.Merge(segt.rt[x],segt.rt[vt[x][i]],0,n-1);
}
}
ll cur=segt.sum[segt.rt[x]];
if(lst!=-1)
{
cur=(cur*inv2)%mod;
}
ans=(ans+cur)%mod;
return;
}
int main(){
ll i,x,y;
scanf("%lld",&n);
for(i=0;i<n;i++)
{
scanf("%lld",&a[i]);
allv.push_back(make_pair(a[i],i));
}
for(i=1;i<n;i++)
{
scanf("%lld%lld",&x,&y);
x--,y--;
vt[x].push_back(y);
vt[y].push_back(x);
}
sort(allv.begin(),allv.end());
for(i=0;i<n;i++)
{
id[allv[i].S]=i;
}
dfs(0,-1);
for(i=0;i<n-1;i++)
{
ans=(ans*2)%mod;
}
printf("%lld\n",ans);
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 7ms
memory: 10624kb
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: 10624kb
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: 816ms
memory: 240040kb
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: 822ms
memory: 247428kb
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: 884ms
memory: 253304kb
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: 1008ms
memory: 292588kb
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: 648ms
memory: 293956kb
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: 640ms
memory: 301636kb
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: 699ms
memory: 290376kb
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: 790ms
memory: 287404kb
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: 862ms
memory: 259996kb
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: 832ms
memory: 253836kb
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: 840ms
memory: 253948kb
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: 778ms
memory: 236088kb
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: 703ms
memory: 261364kb
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"