QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#104410#6322. ForestryzhouhuanyiAC ✓1403ms85320kbC++114.0kb2023-05-10 17:08:122023-05-10 17:08:14

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-10 17:08:14]
  • 评测
  • 测评结果:AC
  • 用时:1403ms
  • 内存:85320kb
  • [2023-05-10 17:08:12]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#define N 300000
#define mod 998244353
using namespace std;
int read()
{
    char c=0;
    int sum=0;
    while (c<'0'||c>'9') c=getchar();
    while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
    return sum;
}
void Adder(int &x,int d)
{
    x+=d;
    if (x>=mod) x-=mod;
    return;
}
void Adder2(int &x,int d)
{
    x+=d;
    if (x<0) x+=mod;
    return;
}
int MD(int x)
{
    return x>=mod?x-mod:x;
}
int MD2(int x)
{
    return x<0?x+mod:x;
}
int fast_pow(int a,int b)
{
    int res=1,mul=a;
    while (b)
    {
	if (b&1) res=1ll*res*mul%mod;
	mul=1ll*mul*mul%mod,b>>=1;
    }
    return res;
}
struct matrix
{
    int d00,d10,d11,d20,d21,d22;
};
matrix c;
matrix operator * (matrix a,matrix b)
{
    c.d00=1;
    c.d10=(a.d10+1ll*a.d11*b.d10)%mod;
    c.d11=1ll*a.d11*b.d11%mod;
    c.d20=(a.d20+1ll*a.d21*b.d10+b.d20)%mod;
    c.d21=(1ll*a.d21*b.d11+b.d21)%mod;
    c.d22=1;
    return c;
}
int n,leng,inv2=(mod+1)>>1,fa[N+1],hs[N+1],top[N+1],bt[N+1],dfn[N+1],rev[N+1],sz[N+1],a[N+1],p[N+1],dp[N+1],DP[N+1],s[N+1],S[N+1],ans;
bool used[N+1],vis[N+1];
vector<int>E[N+1];
void add(int x,int y)
{
    E[x].push_back(y),E[y].push_back(x);
    return;
}
bool cmp(int x,int y)
{
    return a[x]<a[y];
}
void dfs(int x)
{
    used[x]=sz[x]=dp[x]=1;
    for (int i=0;i<E[x].size();++i)
	if (!used[E[x][i]])
	{
	    fa[E[x][i]]=x,dfs(E[x][i]),dp[x]=1ll*dp[x]*(dp[E[x][i]]+1)%mod*inv2%mod,sz[x]+=sz[E[x][i]],Adder(s[x],s[E[x][i]]);
	    if (sz[E[x][i]]>sz[hs[x]]) hs[x]=E[x][i];
	}
    Adder(s[x],dp[x]);
    return;
}
void dfs2(int x)
{
    dfn[x]=++leng,rev[dfn[x]]=x,DP[x]=1;
    if (hs[x]) top[hs[x]]=top[x],dfs2(hs[x]),bt[x]=bt[hs[x]];
    else bt[x]=x;
    for (int i=0;i<E[x].size();++i)
	if (fa[E[x][i]]==x&&E[x][i]!=hs[x])
	    top[E[x][i]]=E[x][i],dfs2(E[x][i]),DP[x]=1ll*DP[x]*(dp[E[x][i]]+1)%mod*inv2%mod,Adder(S[x],s[E[x][i]]);
    if (hs[x]) DP[x]=1ll*DP[x]*inv2%mod;
    return;
}
struct seg
{
    struct node
    {
	int l,r;
	matrix data;
    };
    node tree[(N<<2)+1];
    void push_up(int k)
    {
	tree[k].data=tree[k<<1|1].data*tree[k<<1].data;
	return;    
    }
    void build(int k,int l,int r)
    {
	tree[k].l=l,tree[k].r=r;
	if (l==r)
	{
	    tree[k].data=(matrix){1,DP[rev[l]],DP[rev[l]],MD(DP[rev[l]]+S[rev[l]]),DP[rev[l]],1};
	    return;
	}
	int mid=(l+r)>>1;
	build(k<<1,l,mid),build(k<<1|1,mid+1,r),push_up(k);
	return; 
    }
    void add(int k,int x,matrix d)
    {
	if (tree[k].l==tree[k].r)
	{
	    tree[k].data=d;
	    return;
	}
	int mid=(tree[k].l+tree[k].r)>>1;
	if (x<=mid) add(k<<1,x,d);
	else add(k<<1|1,x,d);
	push_up(k);
	return;
    }
    matrix query(int k,int l,int r)
    {
	if (tree[k].l==l&&tree[k].r==r) return tree[k].data;
	int mid=(tree[k].l+tree[k].r)>>1;
	if (r<=mid) return query(k<<1,l,r);
	else if (l>=mid+1) return query(k<<1|1,l,r);
	else return query(k<<1|1,mid+1,r)*query(k<<1,l,mid);
    }
};
seg T;
void del(int x)
{
    int y;
    matrix rst;
    vis[x]=1;
    while (x)
    {
	if (!vis[x]) T.add(1,dfn[x],(matrix){1,DP[x],DP[x],MD(DP[x]+S[x]),DP[x],1});
	else T.add(1,dfn[x],(matrix){1,0,0,S[x],0,1});
	y=top[x],rst=T.query(1,dfn[y],dfn[bt[y]]);
	if (fa[y]) DP[fa[y]]=1ll*DP[fa[y]]*fast_pow(MD(dp[y]+1),mod-2)%mod,Adder2(S[fa[y]],-s[y]);
	s[y]=rst.d20,dp[y]=rst.d21;
	if (fa[y]) DP[fa[y]]=1ll*DP[fa[y]]*MD(dp[y]+1)%mod,Adder(S[fa[y]],s[y]);
	x=fa[y];
    }
    return;
}
int main()
{
    int x,y;
    n=read();
    for (int i=1;i<=n;++i) a[i]=read(),p[i]=i;
    sort(p+1,p+n+1,cmp);
    for (int i=1;i<=n-1;++i) x=read(),y=read(),add(x,y);
    dfs(1),top[1]=1,dfs2(1),T.build(1,1,n);
    Adder(ans,1ll*MD(s[1]+dp[1])*inv2%mod*a[p[1]]%mod);
    for (int i=1;i<=n-1;++i) del(p[i]),Adder(ans,1ll*MD(s[1]+dp[1])*inv2%mod*MD2(a[p[i+1]]-a[p[i]])%mod);
    for (int i=1;i<=n-1;++i) ans=2ll*ans%mod;
    printf("%d\n",ans);
    return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 22080kb

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: 5ms
memory: 15824kb

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: 1317ms
memory: 68388kb

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: 1353ms
memory: 69776kb

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: 1403ms
memory: 69864kb

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: 641ms
memory: 83784kb

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: 493ms
memory: 82268kb

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: 480ms
memory: 85320kb

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: 580ms
memory: 81136kb

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: 531ms
memory: 79688kb

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: 558ms
memory: 69372kb

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: 1374ms
memory: 69200kb

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: 1335ms
memory: 70452kb

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: 1251ms
memory: 69084kb

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: 398ms
memory: 70596kb

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"