HDU 3015 Disharmony Trees 【 树状数组 】
题意:给出n棵树,给出横坐标x,还有它们的高度h,先按照横坐标排序,则它们的横坐标记为xx,
再按照它们的高度排序,记为hh
两颗树的差异度为 abs(xx[i] - xx[j]) * min(hh[i],hh[j]),求所有的差异度的和
和上面一道题一样,只不过这题是要Min的值,就将h从大到小排序,保证每一个h都是当前最小的
然后维护比当前x小的坐标的个数,当前区间的总和,前面比x小的坐标的和
#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; typedef long long LL;
const int INF = (<<)-;
const int mod=;
const int maxn=; int n;
struct node{ int x,xx,h,hh,id;}; node a[maxn],b[maxn],q[maxn];
int c[maxn],d[maxn];
int s[maxn]; int cmp1(node n1,node n2){ return n1.x < n2.x; }
int cmp2(node n1,node n2){ return n1.h < n2.h;}
int cmp3(node n1,node n2){ return n1.h > n2.h;} int lowbit(int x){ return x & (-x);} LL sum(int c[],int x){
LL ret=;
while(x>){
ret+=c[x]; x-=lowbit(x);
}
return ret;
} void add(int c[],int x,int d){
while(x < maxn){
c[x]+=d;x+=lowbit(x);
}
} int main(){
while(scanf("%d",&n) != EOF){
memset(c,,sizeof(c));
memset(d,,sizeof(d));
for(int i=;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].h); sort(a+,a+n+,cmp1);a[].xx = ;
for(int i=;i<=n;i++){
if(a[i].x == a[i-].x) a[i].xx = a[i-].xx;
else a[i].xx = i;
}
sort(a+,a+n+,cmp2);a[].hh = ;
for(int i=;i<=n;i++){
if(a[i].h != a[i-].h) a[i].hh =i;
else a[i].hh = a[i-].hh;
}
sort(a+,a+n+,cmp3); //for(int i=1;i<=n;i++)
//printf("a[%d].x = %d a[%d].h = %d\n",i,a[i].xx,i,a[i].hh); LL ans = ;
for(int i=;i<=n;i++){
LL x = sum(c,a[i].xx);
LL totalfront = sum(d,a[i].xx);
LL total = sum(d,maxn);
ans += a[i].hh * (x*a[i].xx - totalfront + total - totalfront - (i-x-)*a[i].xx);
// printf("x= %I64d totalfront=%I64d total=%I64d ans = %I64d\n",x,totalfront,total,ans);
add(c,a[i].xx,);
add(d,a[i].xx,a[i].xx);
}
printf("%I64d\n",ans);
}
return ;
}
HDU 3015 Disharmony Trees 【 树状数组 】的更多相关文章
- Disharmony Trees 树状数组
Disharmony Trees Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Su ...
- hdu 3015 Disharmony Trees (离散化+树状数组)
Disharmony Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3015 Disharmony Trees(树状数组)
题意:给你n棵树,每棵树上有两个权值X H 对于X离散化 :3 7 1 5 3 6 -> 2 6 1 4 2 5,对于H一样 然后F = abs(X1-X2) S=min(H1,H2) 求出 ...
- HDU-3015 Disharmony Trees [数状数组]
Problem Description One day Sophia finds a very big square. There are n trees in the square. They ar ...
- HDU 3333 | Codeforces 703D 树状数组、离散化
HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...
- HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)
题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...
- HDU 3333 Turing Tree (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...
- HDU 4325 Flowers(树状数组+离散化)
http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...
- hdu 5775 Bubble Sort 树状数组
Bubble Sort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...
- HDU - 1541 Stars 【树状数组】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意 求每个等级的星星有多少个 当前这个星星的左下角 有多少个 星星 它的等级就是多少 和它同一 ...
随机推荐
- [转]java多线程并发去调用一个类的静态方法安全性探讨
文章转自:https://blog.csdn.net/weibin_6388/article/details/50750035 这篇文章主要讲多线程对静态方法访问的数据安全性 总结如下: 1,java ...
- js 基本基础知识回顾
js中的一切的变量.函数.操作符等等都是区分大小写的. js的基本的数据类型->包含下面的5种: 1.undefined 2.Null 3.Boolean 4.Number 5.String j ...
- IPv6地址分配
- ajax第二天学习
post方式发送请求 要首先设置请求头(参数设置为ajax.setRequestHeader("content-type","application/x-www-form ...
- bindActionCreators作用
个人总结: 讲一下bindActionCreators这个API, bindActionCreators是要结合mapDispatchToProps来使用的. mapDispatchToProps函数 ...
- python 用法测试
Python 3.5.6 1.js风格的回调测试 def b(): ') def a(fn): if callable(fn): fn() a(b) class Sample: def q(self) ...
- jQuery 简单介绍
jQuery 简单介绍 jQuery的定义 jQuery是一个快速,小巧,功能丰富的JavaScript库.它通过易于使用的API在大量浏览器中运行,使得 HTML文档遍历和操作,事件处理,动画 ...
- StringUtils 的填充方法
注意:两个参数的用空格填充,三个参数的用后面的参数填充 第一个参数要填充的字符串,第二个是需要的长度,第三个是以什么填充. 左侧填充: leftPad(): StringUtils.leftPad(S ...
- 移动端ios兼容问题
IOS系统bug: 1)input无法输入的问题: -webkit-user-select:none;改成-webkit-user-select:auto: 2)滚动不流畅(overflow-y:au ...
- UVALive 2664 One-way traffic
One-way traffic Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Or ...