给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. Scala 常用语法

    Clojure首先是FP, 但是由于基于JVM, 所以不得已需要做出一些妥协, 包含一些OO的编程方式 Scala首先是OO, Java语法过于冗余, 一种比较平庸的语言, Scala首先做的是简化, ...

  2. IO流入门-第九章-BufferedReader_BufferedWriter复制

    利用BufferedReader和BufferedWriter进行复制粘贴 import java.io.*; public class BufferedReader_BufferedWriterCo ...

  3. JS eval()函数

    js  eval()函数   这个函数可以把一个字符串当作一个JavaScript表达式一样去执行它.   举个小例子:    //执行表达式  var the_unevaled_answer = & ...

  4. django--mysql设置

    mysql基本配置 'default': { 'ENGINE': 'django.db.backends.mysql', 'HOST': '127.0.0.1', 'PORT': 3306, 'USE ...

  5. 【我的Android进阶之旅】如何去除ListView中Header View、Footer View中的分割线

    最近的项目中给ListView 加入了一个Header View之后,发现Header View的下方也有了分割线,很难看,UI要求将Header View的分割器去掉,好吧.现在就来说一说如何如何去 ...

  6. Java中的字符串不变性

    原文链接:http://www.programcreek.com/2009/02/diagram-to-show-java-strings-immutability/ (图片出处和内容参照) 1.声明 ...

  7. filebeat 简介安装

    Filebeat is a lightweight shipper for forwarding and centralizing log data. Installed as an agent on ...

  8. golang的极简流式编程实现

    传统的过程编码方式带来的弊端是显而易见,我们经常有这样的经验,一段时间不维护的代码或者别人的代码,突然拉回来看需要花费较长的时间,理解原来的思路,如果此时有个文档或者注释写的很好的话,可能花的时间会短 ...

  9. PScc

      photoshop cc 2018破解版是Adobe公司最新版专业化CC图像处理软件,新功能超乎你的想象!photoshop cc 2018破解版新功能给力来袭,访问Lightroom照片,分享作 ...

  10. Linux主从同步监测和利用sendMail来发邮件

    首先介绍下sendMail About SendEmailSendEmail is a lightweight, command line SMTP email client. If you have ...