给n个数求每个子区间的价值,区间的价值是最大值-最小值

套路题= =,分别算最大值和最小值的贡献,用并查集维护,把相邻点连一条边,然后sort,求最大是按边价值(两个点的最大价值)小的排,求最小是按最大排

类似的题:http://www.cnblogs.com/acjiumeng/p/8320666.html

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pii pair<int,int>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f; struct edge{
int u,v,ma,mi;
}e[N];
int father[N],sz[N],val[N];
int Find(int x)
{
return father[x]==x?x:father[x]=Find(father[x]);
}
bool cmp1(edge a,edge b)
{
return max(val[a.u],val[a.v])<max(val[b.u],val[b.v]);
}
bool cmp2(edge a,edge b)
{
return min(val[a.u],val[a.v])>min(val[b.u],val[b.v]);
}
int main()
{
int n,cnt=;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&val[i]);
if(i>)
{
e[cnt].u=i-,e[cnt].v=i;
cnt++;
}
father[i]=i,sz[i]=;
}
sort(e,e+cnt,cmp1);
// for(int i=0;i<cnt;i++)
// printf("%d %d\n",e[i].u,e[i].v);
ll ans=;
for(int i=;i<cnt;i++)
{
int x=Find(e[i].u),y=Find(e[i].v);
if(x!=y)
{
ans+=(ll)sz[x]*sz[y]*max(val[e[i].u],val[e[i].v]);
father[x]=y,sz[y]+=sz[x];
}
}
for(int i=;i<=n;i++)father[i]=i,sz[i]=;
sort(e,e+cnt,cmp2);
for(int i=;i<cnt;i++)
{
int x=Find(e[i].u),y=Find(e[i].v);
if(x!=y)
{
ans-=(ll)sz[x]*sz[y]*min(val[e[i].u],val[e[i].v]);
father[x]=y,sz[y]+=sz[x];
}
}
printf("%lld\n",ans);
return ;
}
/******************** ********************/

Educational Codeforces Round 23D的更多相关文章

  1. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  2. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  3. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  4. [Educational Codeforces Round 16]B. Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

  5. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  6. Educational Codeforces Round 6 C. Pearls in a Row

    Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...

  7. Educational Codeforces Round 9

    Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...

  8. Educational Codeforces Round 37

    Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...

  9. Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship

    Problem   Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...

随机推荐

  1. wsgi pep333

    转自:https://www.aliyun.com/jiaocheng/444966.html?spm=5176.100033.1.11.559a7C 摘要:wsgi介绍参考:pep333wsgi有两 ...

  2. django_上传文件

    要求:  写一个文件上传:如果文件名字重复,不要覆盖,并且放到项目根路径的media文件夹下 def upload(request): if request.method == "GET&q ...

  3. android studio上传项目到github报错Successfully created project 'Demo' on GitHub, but initial commit failed:

    今天博主正在愉快地学习在AndroidStudio中使用Git,结果报了下面这个错∑(っ°Д°;)っ: Can't finish GitHub sharing process Successfully ...

  4. web应用与web框架(Day65)

    Web应用 对于所有的web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端 import socket def handle_request(client): ...

  5. 学员管理系统(简单的Django设计)

    学员管理系统(简单的Django设计) 学员管理系统 项目规划阶段 项目背景 近年来老男孩教育的入学学员数量稳步快速增长,传统的excel统计管理学员信息的方式已经无法满足日渐增长的业务需求.因此公司 ...

  6. 用css 添加手状样式,鼠标移上去变小手

    用css 添加手状样式,鼠标移上去变小手,变小手 用css 添加手状样式,鼠标移上去变小手,变小手 cursor:pointer; 用JS使鼠标变小手onmouseover(鼠标越过的时候) onmo ...

  7. PL/SQL编程—分页功能(存储过程)

    SQL),bookname ),publish )); Table created SQL> SQL> create or replace procedure sp_add_book(bo ...

  8. Hadoop中RPC协议小例子报错java.lang.reflect.UndeclaredThrowableException解决方法

    最近在学习传智播客吴超老师的Hadoop视频,里面他在讲解RPC通信原理的过程中给了一个RPC的小例子,但是自己编写的过程中遇到一个小错误,整理如下: log4j:WARN No appenders ...

  9. javascript Date对象 之 设置时间

    之前对js的date对象总是感觉熟悉,而不愿细细深究其所以然,所以每当自己真正应用起来的时候,总会糊里糊涂的,今日花费2个小时的时间仔细钻研了一下,感觉 豁然开朗,故,以此记录,一来 供以后查阅,二来 ...

  10. springboot与redis的无缝整合(分布式)

    参考博客 : https://blog.csdn.net/csao204282/article/details/54092997 1 首先得引入依赖 <dependency> <gr ...