题意:给出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. Cookie是存储在客户端上的一小段数据

    背景 在HTTP协议的定义中,采用了一种机制来记录客户端和服务器端交互的信息,这种机制被称为cookie,cookie规范定义了服务器和客户端交互信息的格式.生存期.使用范围.安全性. 在JavaSc ...

  2. ServiceStack.Redis之IRedisClient(转载)

    一.属性 IRedisClient的属性如下: 属性 说明 ConnectTimeout  连接超时 Db 当前数据库的ID或下标 DbSize  当前数据库的 key 的数量 HadExceptio ...

  3. Projective Texture Mapping - 投影纹理

    昨天导师让写一个投影纹理,将一个相机渲染的图片的一部分投影到另外一个相机里面,目的是无缝的拼接. 投影纹理就和shadow map一样,都是将片元转换到另外一个相机/光源坐标系下,投影后找到对应的纹素 ...

  4. Python内置数据结构之字典dict

    1. 字典 字典是Python中唯一的内置映射类型,其中的值不按顺序排列,而是存储在键下.键可能是数(整数索引).字符串或元组.字典(日常生活中的字典和Python字典)旨在让你能够轻松地找到特定的单 ...

  5. div纵向居中的方法(转载)

    方法一这个方法把一些 div 的显示方式设置为表格,因此我们可以使用表格的 vertical-align property 属性. <div id="wrapper"> ...

  6. HDU 2079 选课时间(母函数模板题)

    链接:传送门 思路:母函数模板题 /************************************************************************* > Fil ...

  7. [读书笔记] R语言实战 (三) 图形初阶

    创建图形,保存图形,修改特征:标题,坐标轴,标签,颜色,线条,符号,文本标注. 1. 一个简单的例子 #输出到图形到pdf文件 pdf("mygrapg.pdf") attach( ...

  8. zabbix_get 获取agnet端mysql数据失败

    问题 在使用zabbix_get获取agent端的mysql数据时,总是报错,ERROR 2002 (HY000): Can't connect to local MySQL server throu ...

  9. 《你又怎么了我错了行了吧》【Beta】Scrum Meeting 2

    第二天 日期:2019/6/25 前言: 第2次会议在女生宿舍召开 确认编码阶段已经完成,继续测试项目 1.1 今日完成任务情况以及明日任务安排 姓名 当前阶段任务 下一阶段任务 刘 佳 完善了未开发 ...

  10. thinkPHP的Excel插件

    原文地址 http://www.thinkphp.cn/topic/14005.html 总结的注意事项 1实例化第三方类,要在类名前加\ ,不然引用地址不对. 实现步骤:一:在http://phpe ...