记录详情

Accepted


  
# 状态 耗时 内存占用
#1 Accepted 5ms 14.16 MiB
#2 Accepted 386ms 54.797 MiB
#3 Accepted 406ms 53.293 MiB
#4 Accepted 355ms 53.797 MiB
#5 Accepted 350ms 54.922 MiB
#6 Accepted 34ms 15.125 MiB
#7 Accepted 34ms 17.547 MiB
#8 Accepted 457ms 55.047 MiB
#9 Accepted 453ms 54.418 MiB
#10 Accepted 440ms 54.422 MiB

代码

#include<algorithm>
#include<memory.h>
#include<cmath>
#include<cstdio>
#define ll long long
#define mod 1000000007
#define maxn 2000010
#define For(i,x,y)  for(ll i=x;i<=y;++i)
#define FOr(i,x,y)  for(ll i=x;i>=y;--i)
using namespace std;
inline ll read(){   ll x=0,f=1;char ch=getchar();   while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}  while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}  return x*f; }
inline void write(ll x){    if (x<0) putchar('-'),x=-x; if (x>=10) write(x/10);   putchar(x%10+'0');  }
inline void writeln(ll x){  write(x);   puts("");   }
struct data{
	ll v,id;
}a[maxn];
ll v[maxn],fa[maxn],nxt[maxn],head[maxn],vet[maxn],size[maxn];
bool vis[maxn];
ll tot,n,ans,ans1,ans2;
bool cmp1(data a,data b){	return a.v<b.v;	}
bool cmp2(data a,data b){	return a.v>b.v;}
void insert(ll x,ll y){
	nxt[++tot]=head[x];	head[x]=tot;	vet[tot]=y;
}
ll find(ll x){	return x==fa[x]?x:fa[x]=find(fa[x]);	}
void work1(ll x){
	ll sum=0,sum2=0;	size[x]=1;
	for(ll i=head[x];i;i=nxt[i]){
		if (vis[vet[i]]){
			size[x]+=size[find(vet[i])];
			sum=(sum+size[fa[vet[i]]])%mod;
			sum2=(sum2+size[fa[vet[i]]]*size[fa[vet[i]]])%mod;
			fa[fa[vet[i]]]=x;
		}
	}
	ans=((sum*2+sum*sum-sum2)%mod*v[x]+ans)%mod;
	vis[x]=1;
}
void work(){
	ans=0;
	memset(vis,0,sizeof vis);
	For(i,1,n)	fa[i]=i;
	For(i,1,n)	work1(a[i].id);
}
int main(){
	n=read();
	For(i,1,n)	v[i]=a[i].v=read(),a[i].id=i;
	For(i,1,n-1){
		ll x=read(),y=read();
		insert(x,y);	insert(y,x);
	}
	sort(a+1,a+n+1,cmp1);
	work();	ans1=ans;
	For(i,1,n)	a[i].v=a[i].v;
	sort(a+1,a+n+1,cmp2);
	work();	ans2=ans;
	writeln((ans1-ans2+mod)%mod);
}

信息

递交者
类型
递交
题目
梦美题
语言
C++
递交时间
2017-05-05 13:25:18
评测时间
2017-05-10 14:57:29
评测机
分数
100
总耗时
2923ms
峰值内存
55.047 MiB