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 题意 求每个等级的星星有多少个 当前这个星星的左下角 有多少个 星星 它的等级就是多少 和它同一 ...
随机推荐
- 解析RecyclerView(2)——带顶部View和底部View的RecyclerView
在网络层,互联网提供所有应用程序都要使用的两种类型的服务,尽管目前理解这些服务的细节并不重要,但在所有TCP/IP概述中,都不能忽略他们: 无连接分组交付服务(Connectionless Packe ...
- 多线程与MySQL(十)
1.1 多线程 在传统操作系统中,每个进程有一个地址空间,而且默认就有一个控制线程 线程顾名思义,就是一条流水线工作的过程,一条流水线必须属于一个车间,一个车间的工作过程是一个进程 车间负责把资源整合 ...
- python包管理(distutils、easy_install、pip、setup.py/requirements.txt、wheel)
distutils.distutils2 distutils是 python 标准库的一部分,2000年发布.使用它能够进行 python 模块的 安装 和 发布. distutils2 被设计为 d ...
- 页面定制CSS代码初探(四):cnblogs使用Github引用样式
前言 对于用惯了Github的人来说,眼里的引用应该是这样的 "Talk is cheap. Show me the code" -- Linus Torvalds 然而实际上cn ...
- 路飞学城Python-Day24(practise)
本章总结 练习题 什么是C/S架构? C指的是client(客户端软件),S指的是Server(服务端软件)
- 树莓派(Raspberry Pi):完美的家用服务器
出处:http://linux.cn/thread/11884/1/1/ 树莓派(Raspberry Pi):完美的家用服务器 自从树莓派发布后,所有在互联网上的网站为此激动人心的设备提供了很多有趣和 ...
- ResNet入门
CNN入门讲解:什么是残差网络Resnet 变种神经网络的典型代表:深度残差网络 深度残差神经网络ResNet 学习笔记 https://blog.csdn.net/loveliuzz/article ...
- Js原生实现抽奖功能
<div>代码 按钮代码 JS原生代码 完整的代码: <div style="width:365px;height:300px;border:2px solid gree ...
- SpringBoot 消费NSQ消息
使用监听器,来实现实时消费nsq的消息 一.目前spring boot中支持的事件类型如下 ApplicationFailedEvent:该事件为spring boot启动失败时的操作 Applica ...
- 【codeforces 794C】Naming Company
[题目链接]:http://codeforces.com/contest/794/problem/C [题意] 有n个位置; 两个人; 每个人都有n个字符组成的集合s1,s2(可以有重复元素); 然后 ...