题意:给出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 【 树状数组 】的更多相关文章

  1. Disharmony Trees 树状数组

    Disharmony Trees Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  2. hdu 3015 Disharmony Trees (离散化+树状数组)

    Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. 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) 求出 ...

  4. HDU-3015 Disharmony Trees [数状数组]

    Problem Description One day Sophia finds a very big square. There are n trees in the square. They ar ...

  5. HDU 3333 | Codeforces 703D 树状数组、离散化

    HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...

  6. HDU 3333 - Turing Tree (树状数组+离线处理+哈希+贪心)

    题意:给一个数组,每次查询输出区间内不重复数字的和. 这是3xian教主的题. 用前缀和的思想可以轻易求得区间的和,但是对于重复数字这点很难处理.在线很难下手,考虑离线处理. 将所有查询区间从右端点由 ...

  7. HDU 3333 Turing Tree (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3333 题意就是询问区间不同数字的和. 比较经典的树状数组应用. //#pragma comment(l ...

  8. HDU 4325 Flowers(树状数组+离散化)

    http://acm.hdu.edu.cn/showproblem.php?pid=4325 题意:给出n个区间和m个询问,每个询问为一个x,问有多少个区间包含了x. 思路: 因为数据量比较多,所以需 ...

  9. hdu 5775 Bubble Sort 树状数组

    Bubble Sort 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...

  10. HDU - 1541 Stars 【树状数组】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1541 题意 求每个等级的星星有多少个 当前这个星星的左下角 有多少个 星星 它的等级就是多少 和它同一 ...

随机推荐

  1. windows上上传代码到Github

    Repository name: 仓库名称 Description(可选): 仓库描述介绍 Public, Private : 仓库权限(公开共享,私有或指定合作者) Initialize this ...

  2. ajax返回值

    前端: <html> <head> <meta name="viewport" content="width=device-width&qu ...

  3. GridBagLayout使用案例+获取目录下所有的文件+获取创建时间及最后修改时间

    package vvv; import java.awt.Dimension;import java.awt.GridBagConstraints;import java.awt.GridBagLay ...

  4. Win7下安装Flash低版本

    我把HKEY_LOCAL_MACHINE\SOFTWARE\Macromedia\FlashPlayer\SafeVersions中高于要装的版本的项目都删了,还是不行. 看了这个帖子后发现,原来64 ...

  5. RocketMQ学习笔记(12)----RocketMQ的Consumer API简介

    由于消息的消费方式有两种,所以两种方式也有不同的API: 1. PushConsumer的配置 1. consumerGroup: 默认值为DEFAULT_CONSUMER,Consumer组名,多个 ...

  6. what is udev?

    如果你使用Linux比较长时间了,那你就知道,在对待设备文件这块,Linux改变了几次策略.在Linux早期,设备文件仅仅是是一些带有适当的属性集的普通文件,它由mknod命令创建,文件存放在/dev ...

  7. [置顶] 智能家居开源项目 The open Home Automation Bus (openHAB)

    ================================================================================ 2014-05-19 论文的事情太多, ...

  8. 【安装配置Redis】

    目录 安装 配置 Redis官网:https://redis.io Redis是完全开源免费的,遵守BSD协议. Redis是一个高性能的key-value数据库. @ *** Redis具有以下特点 ...

  9. 倍增算法总结 ( 含RMQ模板)

    部分题目来自<算法竞赛设计进阶> 问题       给定一个长度为n的数列A,有m个询问,每次给定一个整数T,求出最大的k,满足a[1],a[2]……a[k]的和小于等于T(不会打sigm ...

  10. 移动端ios兼容问题

    IOS系统bug: 1)input无法输入的问题: -webkit-user-select:none;改成-webkit-user-select:auto: 2)滚动不流畅(overflow-y:au ...