记录详情

Wrong Answer

foo.cpp: In function 'int main()':
foo.cpp:53:41: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long long int' [-Wformat=]
  printf("%d\n", ((ans1-ans)%mod+mod)%mod);
                                         ^
# 状态 耗时 内存占用
#1 Wrong Answer 15ms 25.797 MiB
#2 Wrong Answer 859ms 25.797 MiB
#3 Wrong Answer 812ms 25.797 MiB
#4 Wrong Answer 656ms 25.797 MiB
#5 Wrong Answer 687ms 25.797 MiB
#6 Wrong Answer 78ms 25.793 MiB
#7 Wrong Answer 62ms 25.793 MiB
#8 Wrong Answer 843ms 25.797 MiB
#9 Wrong Answer 781ms 25.801 MiB
#10 Wrong Answer 781ms 25.793 MiB

代码

#include <bits/stdc++.h>
#define N 500020
#define mod 1000000007
#define ll long long
using namespace std;
inline int read(){
	int x=0,f=0;char ch=getchar();
	while(ch>'9'||ch<'0'){if(ch=='-')f=1;ch=getchar();}
	while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
	return f?-x:x;
}
struct node{
	int to, nxt;
}e[N<<1];
struct edge{
	int val, id;
}a[N];
int head[N], cnt;
ll sz[N], vis[N], ans, val[N];
void ins(int x, int y){
	e[++cnt] = (node){y, head[x]}; head[x] = cnt;
	e[++cnt] = (node){x, head[y]}; head[y] = cnt;
}
bool cmp1(edge a, edge b){
	return a.val < b.val;
}
bool cmp2(edge a, edge b){
	return a.val > b.val;
}
void dfs(int x, int v){
	sz[x] = 0; ll sum = 0;
	for(int i = head[x]; i; i = e[i].nxt){
		if(!vis[e[i].to]) continue;
		int y = e[i].to;
		sz[x] += sz[y];
		sum = (sum+sz[y]*sz[y])%mod;
	}
	ans = (ans+(sz[x]*2+sz[x]*sz[x]-sum)%mod*v)%mod;
	sz[x]++; vis[x] = 1;
}
int main(){
	int n = read();
	for(int i = 1; i <= n; i++) a[a[i].id = i].val = read();
	for(int i = 1; i < n; i++) ins(read(), read());
	sort(a+1, a+n+1, cmp1);
	for(int i = 1; i <= n; i++)
		dfs(a[i].id, a[i].val);
	ll ans1 = ans; ans = 0;
	memset(vis, 0, sizeof vis);
	sort(a+1, a+n+1, cmp2);
	for(int i = 1; i <= n; i++)
		dfs(a[i].id, a[i].val);
	printf("%d\n", ((ans1-ans)%mod+mod)%mod);
}

信息

递交者
类型
递交
题目
梦美题
语言
C++
递交时间
2017-05-10 15:32:51
评测时间
2017-05-10 15:32:51
评测机
分数
0
总耗时
5574ms
峰值内存
25.801 MiB