NIT校赛-- 雷顿女士与平衡树
题意: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校赛-- 雷顿女士与平衡树的更多相关文章
- NIT校赛-- 雷顿女士与分队
题意:https://ac.nowcoder.com/acm/contest/2995/D 思路: 和最大子串很像,dp[i]=max(dp[i-1]+a[i],a[i]),要不和前面连一起,要不就是 ...
- 第十六届浙江大学宁波理工学院程序设计大赛 E 雷顿女士与平衡树(并查集)
题意 链接:https://ac.nowcoder.com/acm/contest/2995/E来源:牛客网 卡特莉正在爬树,此时她又在树梢发现了一个谜题,为了不令她分心以至于发生意外,请你帮她解决这 ...
- 第十六届浙江大学宁波理工学院程序设计大赛 D 雷顿女士与分队hard version(dp)
题意 链接:https://ac.nowcoder.com/acm/contest/2995/D来源:牛客网 卡特莉接到来自某程序设计竞赛集训队的邀请,来为他们进行分队规划. 现在集训队共有n名选手, ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- SCNU省选校赛第二场B题题解
今晚的校赛又告一段落啦,终于"开斋"了! AC了两题,还算是满意的,英语还是硬伤. 来看题目吧! B. Array time limit per test 2 seconds me ...
- 2014上半年acm总结(1)(入门+校赛)
大一下学期才开始了acm,不得不说有一点迟,但是acm确实使我的生活充实了很多,,不至于像以前一样经常没事干= = 上学期的颓废使我的c语言学的渣的一笔..靠考前突击才基本掌握了语法 寒假突然醒悟, ...
- 2017CUIT校赛-线上赛
2017Pwnhub杯-CUIT校赛 这是CUIT第十三届校赛啦,也是我参加的第一次校赛. 在被虐到崩溃的过程中也学到了一些东西. 这次比赛是从5.27早上十点打到5.28晚上十点,共36小时,中间睡 ...
- HZNU第十二届校赛赛后补题
愉快的校赛翻皮水! 题解 A 温暖的签到,注意用gets #include <map> #include <set> #include <ctime> #inclu ...
- 校赛F
问题描述 例如对于数列[1 2 3 4 5 6],排序后变为[6 1 5 2 4 3].换句话说,对于一个有序递增的序列a1, a2, a3, ……, an,排序后为an, a1, an-1, a2, ...
随机推荐
- 自制操作系统-使用16进制文件显示 hello world
1.下载qemu: https://www.cnblogs.com/sea-stream/p/10849382.html 2.制作软盘镜像 使用010editor,新建文件 图2 另保存为cherry ...
- 进入docker 容器命令行
#!/bin/bash CNAME=$1 CPID=$(docker inspect --format "{{.State.Pid}}" $CNAME) nsenter --tar ...
- node_exporter安装和配置
1.二进制包安装 mkdir -p /opt/exporter 下载地址: wget https://github.com/prometheus/node_exporter/releases/down ...
- loadrunner11安装
今天虚拟机里面装了下lr11,虚拟机版本是vm9.0,先在虚拟机里面装了windows2003,当然lr也是可以装在自己电脑上面的,但是最好是纯净的环境,由于我电脑东西比较多,所以我就装在虚拟机里面了 ...
- JAVA RPC (九) netty服务端解析
源码地址:https://gitee.com/a1234567891/koalas-rpc 企业生产级百亿日PV高可用可拓展的RPC框架.理论上并发数量接近服务器带宽,客户端采用thrift协议,服务 ...
- 用Ajax请求后台数据
我们先不讲ajax的原理,还是先以实战为主,看一下这个东西到底怎么用的? form表单: <%@ page language="java" contentType=" ...
- docker运行puppeteer出现Page crash解决方案
Docker默认文件空间64MB.如果puppeteer运行的时候超过这个内存就出现了.Page crash.可以使用docker run --shm-size=256m指定一个更大的内存即可.
- Flume-自定义 Sink
Sink 不断地轮询 Channel 中的事件且批量地移除它们,并将这些事件批量写入到存储或索引系统.或者被发送到另一个 Flume Agent. Sink 是完全事务性的. 在从 Channel 批 ...
- IDEA 好用的插件
IDEA 好用的插件 非自带的一些自用插件. Alibaba java Coding Guidelines 阿里出的java规范,支持eclipse和Idea,支持实时扫描,规范代码,养成良好习惯.推 ...
- delphi 根据数据库结构生成TreeView
procedure TUIOperate.FillTree(treeview: TTreeView); var findq: TADOQuery; node: TTreeNode; / ...