Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
53176 LinkZelda 树上排序 C++ 解答错误 0 911 MS 76672 KB 2924 2022-07-21 22:56:47

Tests(0/10):


#include<cstdio> #include<algorithm> #include<ctype.h> #include<cstring> #include<iostream> #include<vector> #include<queue> #include<ctime> #include<cmath> #include<queue> #include<map> #include<set> #include<unordered_map> #include<stack> #define pr pair<int,int> #define eps 1e-8 #define int long long using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();} while(isdigit(ch)){x=x*10+ch-'0';ch=getchar();} return x*f; } const int N=1000005,Mod=1e9+7; int head[N],to[N],nxt[N],cnt; void add(int u,int v) { to[++cnt]=v; nxt[cnt]=head[u]; head[u]=cnt; } int a[N],b[N],n; int dfsn[N],rk[N],sz[N],tp[N],p[N],son[N],fa[N],dep[N],dfscnt; int ans; int tr[2][N]; void modify(int ty,int x,int k) { for(;x<=n;x+=x&-x) tr[ty][x]+=k; } int query(int ty,int x) { int ret=0; for(;x;x-=x&-x) ret+=tr[ty][x]; return ret; } void dfs1(int now,int fath) { sz[now]=1;dep[now]=dep[fath]+1; fa[now]=fath; for(int i=head[now];i;i=nxt[i]) { int v=to[i]; if(v==fath) continue; dfs1(v,now); sz[now]+=sz[v]; if(sz[v]>sz[son[now]]) son[now]=v; } } void dfs2(int now,int t) { tp[now]=t;dfsn[now]=++dfscnt; rk[dfscnt]=now; if(!son[now]) return; dfs2(son[now],t); for(int i=head[now];i;i=nxt[i]) { int v=to[i]; if(v==son[now]||v==fa[now]) continue; dfs2(v,v); } } void work(int now,int fath) { int qwq=query(1,a[now]); ans=(ans+qwq*sz[now]%Mod*b[a[now]]%Mod)%Mod; // printf("awa:: %lld %lld\n",now,qwq); int cntt=0; for(int i=head[now];i;i=nxt[i]) { int v=to[i]; if(v==fath) continue; ans=(ans+cntt*sz[v]%Mod*b[a[now]]%Mod)%Mod; cntt+=sz[v]; modify(1,a[now],n-sz[v]); work(v,now); modify(1,a[now],-(n-sz[v])); } } int querylink(int x,int y) { if(!x||!y) return 0; int ret=0; while(tp[x]!=tp[y]) { if(dep[tp[x]]<dep[tp[y]]) swap(x,y); ret+=query(0,dfsn[x])-query(0,dfsn[tp[x]]-1); x=fa[tp[x]]; } ret+=dep[x]>dep[y]?query(0,dfsn[x])-query(0,dfsn[y]-1):query(0,dfsn[y])-query(0,dfsn[x]-1); return ret; } signed main() { n=read(); for(int i=1;i<=n;i++) b[i]=a[i]=read(),p[i]=i; sort(b+1,b+n+1); for(int i=1;i<=n;i++) a[i]=lower_bound(b+1,b+n+1,a[i])-b; for(int i=1;i<n;i++) { int u=read(),v=read(); add(u,v);add(v,u); } dfs1(1,0);dfs2(1,1); work(1,0); //printf("??? %lld\n",ans); sort(p+1,p+n+1,[&](int x,int y){return a[x]<a[y];}); for(int i=1;i<=n;i++) { modify(0,dfsn[p[i]],sz[p[i]]); int qwq=query(0,dfsn[p[i]]+sz[p[i]]-1)-query(0,dfsn[p[i]]-1); // printf("ovo :: %lld ",qwq); ans=(ans+b[a[p[i]]]*qwq%Mod*(n-sz[p[i]]+1)%Mod)%Mod; qwq=query(0,n)-qwq-querylink(1,fa[p[i]]); // printf("%lld\n",qwq); ans=(ans+b[a[p[i]]]*qwq%Mod*sz[p[i]]%Mod)%Mod; } printf("%lld\n",ans); return 0; }


测评信息:

输入

20
40280555 306563900 921760000 212451292 17637368 108430616 397345992 931738368 750783678 443402768 183629340 500349190 99393296 416736384 578450400 725861472 910749824 581722568 25203536 194374912

输出

806138735

答案

672181731

系统信息

exit code: 0, checker exit code: 0

输入

400
477930180 344017636 678033260 325078480 636053800 546640384 204567680 483596704 733955216 168809360 579655368 713402302 539905600 849831048 885227708 82909000 513252288 436073948 64141350 8548264...

输出

313307055

答案

419083310

系统信息

exit code: 0, checker exit code: 0

输入

2000
484790606 245307470 159003350 134947640 279638996 632761570 446613130 333641052 356616668 471045981 655780880 801731414 747514300 885913036 621430840 815638024 867974400 247510920 508684128 1734...

输出

412634143

答案

455296473

系统信息

exit code: 0, checker exit code: 0

输入

2000
563730088 618856030 948161576 509953967 417804000 243904320 49583872 678286210 310714490 786857400 989108000 823921685 160313304 66943816 42658304 138137300 779596576 30281220 692345175 28385596...

输出

348247277

答案

752537850

系统信息

exit code: 0, checker exit code: 0

输入

500000
190224823 798321400 284461930 602711180 720391112 151330788 575913950 341098288 816790408 539473616 189982332 597148717 453932156 638452416 424300885 862964864 171837360 279721550 509197376 93...

输出

328510361

答案

665320053

系统信息

exit code: 0, checker exit code: 0

输入

500000
181283840 960312516 984406600 910577630 734025627 378737500 370736065 321093560 422443928 786948080 199110384 294456415 457650089 958922048 958171214 406434880 97417456 606953000 627423800 480...

输出

803766614

答案

431240536

系统信息

exit code: 0, checker exit code: 0

输入

500000
962227552 705012970 352088000 678412906 203537760 124935600 842670912 179949760 351297485 54583528 487746050 35328638 288475392 953968500 379271480 148775700 570925048 448836120 78060160 62515...

输出

173345241

答案

789242887

系统信息

exit code: 0, checker exit code: 0

输入

500000
567050644 793458752 566867690 462483964 469866770 583949384 963551200 661489500 935488864 999799730 645634560 54263008 883868060 113157520 116840148 843303936 415126044 839884660 473095840 629...

输出

905194545

答案

34452672

系统信息

exit code: 0, checker exit code: 0

输入

500000
419043790 791077988 358563872 938460364 262326470 933280256 864872872 487990250 556415184 666340888 634746750 948460334 565879310 539010100 435358142 543961008 656698480 690632270 430794400 55...

输出

130945794

答案

771841407

系统信息

exit code: 0, checker exit code: 0

输入

500000
656801284 969704328 96332600 68540620 107772885 296256328 697443370 715223168 761577893 928006462 788570712 425401936 622897286 573354815 896342016 157079824 477178272 448864816 61058440 34808...

输出

291567078

答案

322840736

系统信息

exit code: 0, checker exit code: 0