QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#104645#6322. ForestryzhuyifanRE 3539ms174316kbC++146.6kb2023-05-11 15:21:132023-05-11 15:21: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-11 15:21:14]
  • 评测
  • 测评结果:RE
  • 用时:3539ms
  • 内存:174316kb
  • [2023-05-11 15:21:13]
  • 提交

answer

#include<bits/stdc++.h>
#define LL long long
#define SZ(x) ((LL)(x.size()))
using namespace std;
long long read(){
  long long q=0,w=1;
  char ch=getchar();
  while(ch>'9' || ch<'0'){if(ch=='-')w=-1;ch=getchar();}
  while(ch>='0'&&ch<='9'){q=q*10+(ch-'0');ch=getchar();}
  return q*w;
}
void write(LL x){
  if(x<0){putchar('-');x=(-x);}
  if(x>9)write(x/10);
  putchar('0'+x%10);
}
void writeln(LL x){write(x);puts("");}
void writecs(LL x){write(x);putchar(' ');}
LL qpow(LL a,LL b,LL p){
  LL ans=1;
  while(b){
    if(b&1)ans=(ans*a)%p;
    b>>=1;a=(a*a)%p;
  }
  return ans;
}
const long long mod = 998244353;
void addmod(LL &x,LL y){x+=y;if(x>=mod)x-=mod;}
void submod(LL &x,LL y){x-=y;if(x<0)x+=mod;}
LL domod(LL x){
  if(x<0)x+=mod;
  if(x>=mod)x-=mod;
  return x;
}
LL inv(LL x){return qpow(x,mod-2,mod);}
const long long inv2 = inv(2);
struct mat{LL a[3][3];};
mat operator+(mat x,mat y){
  mat z;
  for(LL i=0;i<3;i++)
    for(LL j=0;j<3;j++)z.a[i][j]=0;
  for(LL i=0;i<3;i++)
    for(LL j=0;j<3;j++){
      addmod(z.a[i][j],x.a[i][0]*y.a[0][j]%mod);
      addmod(z.a[i][j],x.a[i][1]*(y.a[0][j]+y.a[1][j])%mod*inv2%mod);
      addmod(z.a[i][j],x.a[i][2]*(y.a[1][j]%mod*inv2%mod+y.a[2][j])%mod);
    }
  return z;
}
const long long N = 300000+95;
long long n,a[N];
struct Edge{
  LL to,nxt;
}e[N<<1];LL head[N],tot;
void add_e(LL u,LL v){
  e[++tot].to=v;e[tot].nxt=head[u];
  head[u]=tot;return ;
}
long long fa[N],dep[N],sz[N],son[N];
void dfs(LL x){
  dep[x]=dep[fa[x]]+1;sz[x]=1;
  for(LL i=head[x];i;i=e[i].nxt){
    if(e[i].to==fa[x])continue;
    fa[e[i].to]=x;dfs(e[i].to);sz[x]+=sz[e[i].to];
    if(sz[son[x]]<sz[e[i].to])son[x]=e[i].to;
  }
  return ;
}
long long dfn[N],low[N],ed[N],tim,seq[N],pf[N];
void dfs2(LL x,LL ac){
  dfn[x]=++tim;seq[tim]=x;ed[ac]=tim;pf[x]=ac;
  if(son[x])dfs2(son[x],ac);
  for(LL i=head[x];i;i=e[i].nxt){
    if(e[i].to==fa[x]||e[i].to==son[x])continue;
    dfs2(e[i].to,e[i].to);
  }
  low[x]=tim;return ;
}
LL ord[N];
bool cmp(LL x,LL y){return a[x]>a[y];}
LL dp[N][3];bool vis[N];
namespace SGT{
  struct node{LL l,r,d1,d2;}s[N<<2];
  void pushup(LL p){
    s[p].d1=s[s[p].l].d1*s[s[p].r].d1%mod;
    s[p].d2=domod(s[s[p].l].d2+s[s[p].r].d2);
    return ;
  }
  void build(LL &p,LL l,LL r){
    p=++tot;s[p].d1=1;s[p].d2=0;
    if(l>=r)return ;
    LL mid=(l+r)>>1;
    build(s[p].l,l,mid);
    build(s[p].r,mid+1,r);
    pushup(p);return ;
  }
  void update(LL p,LL x,LL d1,LL d2,LL l,LL r){
    if(l==r){s[p].d1=d1;s[p].d2=d2;return ;}
    LL mid=(l+r)>>1;
    if(x<=mid)update(s[p].l,x,d1,d2,l,mid);
    else update(s[p].r,x,d1,d2,mid+1,r);
    pushup(p);return ;
  }
  LL query_d1(LL p){return s[p].d1;}
  LL query_d2(LL p){return s[p].d2;}
}
LL rt[N],lim[N],id[N];
void DFS(LL x){
  //  cout<<"> DFS: x = "<<x<<endl;
  if(!vis[x]){dp[x][0]=1;dp[x][1]=0;dp[x][2]=0;}
  else {dp[x][0]=1;dp[x][1]=1;dp[x][2]=0;}
  for(LL i=head[x];i;i=e[i].nxt){
    if(e[i].to==fa[x])continue;
    DFS(e[i].to);LL y=e[i].to;
    LL dp0=dp[x][0],dp1=dp[x][1],dp2=dp[x][2];
    dp[x][0]=(dp0*dp[y][0]%mod);
    dp[x][1]=(dp1*(dp[y][0]+dp[y][1])%mod*inv2%mod);
    dp[x][2]=(dp2*dp[y][0]%mod+dp0*dp[y][1]%mod*inv2%mod+dp0*dp[y][2]%mod)%mod;
    if(y!=son[x]){id[y]=++lim[x];}
  }
  assert(dp[x][0]==1);
  //  cout<<" x = "<<x<<" lim[x] = "<<lim[x]<<endl;
  SGT::build(rt[x],1,lim[x]);
  for(LL i=head[x];i;i=e[i].nxt){
    if(e[i].to==fa[x]||e[i].to==son[x])continue;
    LL y=e[i].to;
    SGT::update(rt[x],id[y],(1+dp[y][1])*inv2%mod,(dp[y][2]+dp[y][1]*inv2)%mod,1,lim[x]);
  }
  return ;
}
mat make(LL x){
  LL dp1=SGT::query_d1(rt[x]),dp2=SGT::query_d2(rt[x]);
  if(!vis[x])dp1=0ll;
  //  cout<<"> x = "<<x<<" dp0 = "<<1<<" dp1 = "<<dp1<<" dp2 = "<<dp2<<endl;
  return (mat){{{1,0,0},{0,dp1,0},{dp2,0,1}}};
}
namespace seg{
  struct node{LL l,r;mat d;}s[N<<2];
  void pushup(LL p){s[p].d=s[p*2].d+s[p*2+1].d;}
  void build(LL p,LL l,LL r){
    s[p].l=l;s[p].r=r;
    if(l==r){s[p].d=make(seq[l]);return ;}
    LL mid=(l+r)>>1;
    build(p*2,l,mid);
    build(p*2+1,mid+1,r);
    pushup(p);return ;
  }
  void update(LL p,LL x){
    if(s[p].l==s[p].r){s[p].d=make(seq[s[p].l]);return ;}
    LL mid=(s[p].l+s[p].r)>>1;
    if(x<=mid)update(p*2,x);
    else update(p*2+1,x);
    pushup(p);return ;
  }
  mat query(LL p,LL l,LL r){
    if(l<=s[p].l&&s[p].r<=r)return s[p].d;
    LL mid=(s[p].l+s[p].r)>>1;
    if(r<=mid)return query(p*2,l,r);
    if(mid<l)return query(p*2+1,l,r);
    return (query(p*2,l,r)+query(p*2+1,l,r));
  }
}
void update(LL x){
  while(x){
    seg::update(1,dfn[x]);
    if(fa[pf[x]]){
      mat vl=seg::query(1,dfn[pf[x]],ed[pf[x]]);
      SGT::update(rt[fa[pf[x]]],id[pf[x]],(1+(vl.a[1][0]+vl.a[1][1]))*inv2%mod,((vl.a[2][0]+vl.a[2][1])+(vl.a[1][0]+vl.a[1][1])*inv2)%mod,1,lim[fa[pf[x]]]);
    }
    x=fa[pf[x]];
  }
  return ;
}
LL query(){
  //  mat vl=seg::query(1,dfn[1]+1,ed[1]);
  /*  cout<<" vl.n = "<<vl.n<<" vl.m = "<<vl.m<<endl;
  for(LL i=0;i<vl.n;i++){
    for(LL j=0;j<vl.m;j++)
      cout<<vl.a[i][j]<<" ";
    cout<<endl;
  }*/
  mat v=seg::query(1,dfn[1],ed[1]);
  /*  cout<<"> query: "<<endl;
  for(LL i=0;i<3;i++){
    for(LL j=0;j<3;j++)
      cout<<v.a[i][j]<<" ";
    cout<<endl;
  }*/
  return domod((v.a[1][0]+v.a[1][1])%mod+(v.a[2][0]+v.a[2][1])%mod);
}
int main(){
  n=read();
  for(LL i=1;i<=n;i++)a[i]=read();
  for(LL i=1;i<n;i++){
    LL u=read(),v=read();
    add_e(u,v);add_e(v,u);
  }
  dfs(1);dfs2(1,1);
  DFS(1);seg::build(1,1,n);
  for(LL i=1;i<=n;i++)ord[i]=i;
  sort(ord+1,ord+n+1,cmp);
  LL ans=0;
  //  cout<<" query() = "<<query()<<endl;
  LL base=1;
  for(LL i=1;i<n;i++)base=(base*2ll)%mod;
  for(LL l=1,r=0;l<=n;l=r+1){
    r=l;while(r<n&&a[ord[r+1]]==a[ord[l]])r++;
    for(LL i=l;i<=r;i++){vis[ord[i]]=true;update(ord[i]);}
    LL SUM=query()*base%mod;
    addmod(ans,SUM*(a[ord[r]]-a[ord[r+1]])%mod);
    /*    cout<<" SUM = "<<SUM<<" l = "<<l<<" r = "<<r<<" a[ord[l]] = "<<a[ord[l]]<<endl;
    for(LL i=1;i<=n;i++)
      cout<<vis[i]<<" ";
    cout<<endl;
    for(LL i=1;i<=n;i++)
      cout<<" i = "<<i<<" dp[i][0] = "<<dp[i][0]<<" dp[i][1] = "<<dp[i][1]<<" dp[i][2] = "<<dp[i][2]<<" son[i] = "<<son[i]<<endl;
    cout<<endl;
    for(LL x=1;x<=n;x++){
      cout<<" x = "<<x<<" seq[x] = "<<seq[x]<<endl;
      mat v=seg::query(1,x,x);
      for(LL i=0;i<3;i++){
	for(LL j=0;j<3;j++)
	  cout<<v.a[i][j]<<" ";
	cout<<endl;
      }
      cout<<endl;
    }
    cout<<endl;*/
  }
  writeln(ans);
  return 0;
}
/*
my hack data:
input:
3
2 1 1 
1 2
2 3
output:
10
*/

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 25972kb

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: 27892kb

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: 3269ms
memory: 164848kb

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: 3430ms
memory: 160608kb

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: 3539ms
memory: 163732kb

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

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: 1122ms
memory: 165556kb

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: 1100ms
memory: 174316kb

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: 1182ms
memory: 170256kb

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: 1214ms
memory: 169148kb

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: -100
Runtime Error

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:


result: