Educational Codeforces Round 23D
给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的更多相关文章
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- [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 ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- Educational Codeforces Round 6 C. Pearls in a Row
Educational Codeforces Round 6 C. Pearls in a Row 题意:一个3e5范围的序列:要你分成最多数量的子序列,其中子序列必须是只有两个数相同, 其余的数只能 ...
- Educational Codeforces Round 9
Educational Codeforces Round 9 Longest Subsequence 题目描述:给出一个序列,从中抽出若干个数,使它们的公倍数小于等于\(m\),问最多能抽出多少个数, ...
- Educational Codeforces Round 37
Educational Codeforces Round 37 这场有点炸,题目比较水,但只做了3题QAQ.还是实力不够啊! 写下题解算了--(写的比较粗糙,细节或者bug可以私聊2333) A. W ...
- 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 ...
随机推荐
- Chrome 默认样式 (user agent stylesheetbody) 优先级变高的问题
解决方法:只需要在页面的<HTML>标签前添加声明即可. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional ...
- Linux入门之运维(1) 系统监控 vmstat top
vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.这个命令是我查看Linux/Unix最 ...
- 二.数据库游标对象cursor与实例
1.数据库游标对象cursor 2.select实例 代码展示: import pymysql conn=pymysql.connect( host='192.168.199.249', port=3 ...
- Linux(7)- Nginx.conf主配置文件、Nginx虚拟主机/访问日志/限制访问IP/错误页面优化、Nginx反向代理、Nginx负载均衡
一.Nginx.conf主配置文件 Nginx主配置文件conf/nginx.conf是一个纯文本类型的文件,整个配置文件是以区块的形式组织的.一般,每个区块以一对大括号{}来表示开始与结束. 核心模 ...
- Mysql学习笔记—索引
一.什么是索引 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,所以查询语句的优化显然是重中之重. 在数据 ...
- Django——认证系统(Day72)
阅读目录 COOKIE 与 SESSION 用户认证 COOKIE 与 SESSION 概念 cookie不属于http协议范围,由于http协议无法保持状态,但实际情况,我们却又需要“保持状态”,因 ...
- Django-model基础(Day69)
阅读目录 ORM 创建表(建立模型) 添加表记录 查询表记录 F查询与Q查询 修改表记录 删除表记录 数据库回顾:http://www.cnblogs.com/yuanchenqi/articles/ ...
- 《Spring Boot 实战》随记
第一部分 Spring 4.x 1. Spring基础 略过 2. Spring常用配置 2.1 Bean的scope 使用@Scope注解配置scope.默认signleton,原型模式protot ...
- Python Tornado框架(ioloop对象分析)
网上都说nginx和lighthttpd是高性能web服务器,而tornado也是著名的高抗负载应用,它们间有什么相似处呢?上节提到的ioloop对象是如何循环的呢?往下看. 首先关于TCP服务器的开 ...
- IO 复习笔记
输入流,从源到流中:输出流,从流到目的地. 1. 操作文件: 1).写入:FileOutputStream或者FileWriter 2).读取:FileInputStream或者Fil ...