题意:https://ac.nowcoder.com/acm/contest/2995/E

给你一棵树,节点有权值,让你求所有路径max-min的和。

思路:

我们计算每个点的贡献,对于一个点,当它为某条路径的最大值是,一定在一个所有值<=它的连通块里。所有我们从小到大添边合并共享(两块的大小之积,不是和)

最小值也同样处理。

 #define IOS ios_base::sync_with_stdio(0); cin.tie(0);
#include <cstdio>//sprintf islower isupper
#include <cstdlib>//malloc exit strcat itoa system("cls")
#include <iostream>//pair
#include <fstream>//freopen("C:\\Users\\13606\\Desktop\\Input.txt","r",stdin);
#include <bitset>
//#include <map>
//#include<unordered_map>
#include <vector>
#include <stack>
#include <set>
#include <string.h>//strstr substr strcat
#include <string>
#include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9;
#include <cmath>
#include <deque>
#include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less
#include <vector>//emplace_back
//#include <math.h>
#include <cassert>
#include <iomanip>
//#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor
#include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare)
using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation
//******************
clock_t __START,__END;
double __TOTALTIME;
void _MS(){__START=clock();}
void _ME(){__END=clock();__TOTALTIME=(double)(__END-__START)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;}
//***********************
#define rint register int
#define fo(a,b,c) for(rint a=b;a<=c;++a)
#define fr(a,b,c) for(rint a=b;a>=c;--a)
#define mem(a,b) memset(a,b,sizeof(a))
#define pr printf
#define sc scanf
#define ls rt<<1
#define rs rt<<1|1
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef unsigned long long ull;
typedef long long ll;
typedef double db;
const double E=2.718281828;
const double PI=acos(-1.0);
const ll INF=(1LL<<);
const int inf=(<<);
const double ESP=1e-;
const int mod=(int)1e9+;
const int N=(int)5e5+; int fa[N];
int SZ[N];
int Find(int x)
{
return (x==fa[x])?x:(fa[x]=Find(fa[x]));
} vector<vector<int> >G(N);
pair<ll,int> a[N];
bool vis[N]; void solve()
{
int n;
sc("%d",&n);
for(int i=;i<=n;++i)fa[i]=i,sc("%lld",&a[i].first),a[i].second=i,G[i].clear();
for(int i=;i<=n-;++i)
{
int u,v;
sc("%d%d",&u,&v);
G[v].push_back(u);
G[u].push_back(v);
}
sort(a+,a++n);
mem(vis,);
for(int i=;i<=n;++i)SZ[i]=;
ll ans=;
for(int i=;i<=n;++i)
{
int now=a[i].second;
vis[now]=;
int sz=G[now].size();
for(int j=;j<sz;++j)
{
int to=G[now][j];
if(vis[to])
{
ans=(ans+a[i].first*SZ[now]%mod*SZ[Find(to)]%mod)%mod;
SZ[now]+=SZ[Find(to)];
fa[Find(to)]=Find(now);
}
}
}
// cout<<ans<<endl;
mem(vis,);
for(int i=;i<=n;++i)SZ[i]=;
for(int i=;i<=n;++i)fa[i]=i;
for(int i=n;i>=;--i)
{
int now=a[i].second;
vis[now]=;
int sz=G[now].size();
for(int j=;j<sz;++j)
{
int to=G[now][j];
if(vis[to])
{
ans=(ans-a[i].first*SZ[now]%mod*SZ[Find(to)]%mod+mod)%mod;
SZ[now]+=SZ[Find(to)];
fa[Find(to)]=Find(now);
}
}
}
pr("%lld\n",ans);
} int main()
{
int T;
sc("%d",&T);
while(T--)solve();
return ;
} /**************************************************************************************/

NIT校赛-- 雷顿女士与平衡树的更多相关文章

  1. NIT校赛-- 雷顿女士与分队

    题意:https://ac.nowcoder.com/acm/contest/2995/D 思路: 和最大子串很像,dp[i]=max(dp[i-1]+a[i],a[i]),要不和前面连一起,要不就是 ...

  2. 第十六届浙江大学宁波理工学院程序设计大赛 E 雷顿女士与平衡树(并查集)

    题意 链接:https://ac.nowcoder.com/acm/contest/2995/E来源:牛客网 卡特莉正在爬树,此时她又在树梢发现了一个谜题,为了不令她分心以至于发生意外,请你帮她解决这 ...

  3. 第十六届浙江大学宁波理工学院程序设计大赛 D 雷顿女士与分队hard version(dp)

    题意 链接:https://ac.nowcoder.com/acm/contest/2995/D来源:牛客网 卡特莉接到来自某程序设计竞赛集训队的邀请,来为他们进行分队规划. 现在集训队共有n名选手, ...

  4. 2016 华南师大ACM校赛 SCNUCPC 非官方题解

    我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...

  5. SCNU省选校赛第二场B题题解

    今晚的校赛又告一段落啦,终于"开斋"了! AC了两题,还算是满意的,英语还是硬伤. 来看题目吧! B. Array time limit per test 2 seconds me ...

  6. 2014上半年acm总结(1)(入门+校赛)

    大一下学期才开始了acm,不得不说有一点迟,但是acm确实使我的生活充实了很多,,不至于像以前一样经常没事干=  = 上学期的颓废使我的c语言学的渣的一笔..靠考前突击才基本掌握了语法 寒假突然醒悟, ...

  7. 2017CUIT校赛-线上赛

    2017Pwnhub杯-CUIT校赛 这是CUIT第十三届校赛啦,也是我参加的第一次校赛. 在被虐到崩溃的过程中也学到了一些东西. 这次比赛是从5.27早上十点打到5.28晚上十点,共36小时,中间睡 ...

  8. HZNU第十二届校赛赛后补题

    愉快的校赛翻皮水! 题解 A 温暖的签到,注意用gets #include <map> #include <set> #include <ctime> #inclu ...

  9. 校赛F

    问题描述 例如对于数列[1 2 3 4 5 6],排序后变为[6 1 5 2 4 3].换句话说,对于一个有序递增的序列a1, a2, a3, ……, an,排序后为an, a1, an-1, a2, ...

随机推荐

  1. java.security.InvalidKeyException: Illegal key size or default parameters

    今天在使用idea打包maven项目时,出现这个错误:java.security.InvalidKeyException: Illegal key size or default parameters ...

  2. include指令与jsp:include动作标识的区别

    include指令: 文件包含指令include是jsp的另一条指令标识.通过该指令可以在一个jsp页面中包含另一个jsp页面.不过该指令是静态包含,也就是说被包含文件中所有内容会被原样包含到jsp页 ...

  3. Sketch教程

    去年夏天开始用Sketch,觉得它放Dock里闪亮亮的很好看,当时笑称是男盆友送我的第一颗小钻石噗哈哈.所以那段时间几乎刷遍了所有关于 Sketch 的网站.文章.教程,之后又在自学设计,因为想和更多 ...

  4. sass/scss 和 less对比

    一. Sass/Scss.Less是什么? Sass (Syntactically Awesome Stylesheets)是一种动态样式语言,Sass语法属于缩排语法,比css比多出好些功能(如变量 ...

  5. impdp导入报错ORA-39070:无法打开日志文件

    >impdp test/123456@orcl directory=expnc_dir dumpfile=TEST.DMP full=y ORA-39002:操作无效 ORA39070:无法打开 ...

  6. guava常用集合交集,差集,并集,补集操作

    <!-- https://mvnrepository.com/artifact/com.google.guava/guava --> <dependency> <grou ...

  7. ExtractIcon function Retrieves a handle to an icon from the specified executable file, DLL, or icon file.

    https://msdn.microsoft.com/zh-cn/vstudio/ms648068(v=vs.90)

  8. spark入门备忘---1

    import org.apache.spark.{SparkContext, SparkConf} import scala.math.random /** * 利用spark进行圆周率的计算 */ ...

  9. Python中elasticsearch插入和更新数据的实现方法

    Python中elasticsearch插入和更新数据的实现方法 这篇文章主要介绍了Python中elasticsearch插入和更新数据的实现方法,需要的朋友可以参考下 首先,我的索引结构是酱紫的. ...

  10. Centos7 系统更改apache默认网站目录(解决You don't have permission to access / on this server问题)

    当我们在Centos7中配置好Apache时,发现apache默认解析目录是在 /var/www/html,也就是说当访问服务器 IP 或者本地 localhost 时, 默认定位到这个目录里的 in ...