hdu-3015 Disharmony Trees---离散化+两个树状数组
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=3015
题目大意:
有一些树,这些树的高度和位置给出。现在高度和位置都按从小到大排序,对应一个新的rank,任意两棵树的值为min(高度的rank) * abs(位置差的绝对值)。问所有任意两棵树的值的和是多少。
解题思路:
按照题意离散化,然后对H从大到小排序,这样可以保证前面的树高度都比当前的高(或者相等)。在计算的时候就可以使用当前的H。
和POJ-1990类似
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<algorithm>
#include<vector>
#include<sstream>
#define lowbot(i) (i&(-i))
using namespace std;
typedef long long ll;
const int maxn = 1e5 + ;
struct node
{
ll x, h, id;
}a[maxn], b[maxn];
bool cmp1(node a, node b)
{
return a.x < b.x;
}
bool cmp2(node a, node b)
{
return a.h < b.h;
}
bool cmp3(node a, node b)
{
return a.h > b.h;
}
int n;
void add(int x, int d, int tree[])
{
while(x <= n)
tree[x] += d, x += lowbot(x);
}
ll sum(int x, int tree[])
{
ll ans = ;
while(x)
ans += tree[x], x -=lowbot(x);
return ans;
}
int tree[maxn], tree_num[maxn];
int main()
{
while(scanf("%d", &n) != EOF)
{
memset(a, , sizeof(a));
memset(b, , sizeof(b));
memset(tree, , sizeof(tree));
memset(tree_num, , sizeof(tree_num));
for(int i = ; i <= n; i++)
scanf("%lld%lld", &a[i].x, &a[i].h), a[i].id = i;
sort(a + , a + + n, cmp1);
for(int i = ; i <= n; i++)
{
if(a[i].x == a[i - ].x)b[a[i].id].x = b[a[i - ].id].x;
else b[a[i].id].x = i;
}
sort(a + , a + + n, cmp2);
for(int i = ; i <= n; i++)
{
if(a[i].h == a[i - ].h)b[a[i].id].h = b[a[i - ].id].h;
else b[a[i].id].h = i;
}
//for(int i = 1; i <= n; i++)
// cout<<b[i].x<<" "<<b[i].h<<endl;
sort(b + , b + + n, cmp3);
ll ans = , cnt, num;
for(int i = ; i <= n; i++)
{
cnt = sum(b[i].x, tree);
num = sum(b[i].x, tree_num);
ans += (num * b[i].x - cnt) * b[i].h;
cnt = sum(n, tree) - cnt;
num = i - num - ;
ans += (cnt - num * b[i].x) * b[i].h;
add(b[i].x, b[i].x, tree);
add(b[i].x, , tree_num);
}
cout<<ans<<endl;
}
return ;
}
hdu-3015 Disharmony Trees---离散化+两个树状数组的更多相关文章
- hdu 3015 Disharmony Trees (离散化+树状数组)
Disharmony Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)
hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...
- poj1990两个树状数组
垃圾poj交不上去 /* 按权值从小到大排序, 两个树状数组维护权值小于等于并且在i左边的点的个数和权值 */ #include<iostream> #include<cstring ...
- 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
题解:在路边有一行树,给出它们的坐标和高度,先按X坐标排序.记录排名,记为rankx,再按它们的高度排序,记录排名,记为rankh.两颗树i,j的差异度为 fabs(rankx[i]-rankx[j] ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- Hdu 5458 Stability (LCA + 并查集 + 树状数组 + 缩点)
题目链接: Hdu 5458 Stability 题目描述: 给出一个还有环和重边的图G,对图G有两种操作: 1 u v, 删除u与v之间的一天边 (保证这个边一定存在) 2 u v, 查询u到v的路 ...
- HDU 6318 Swaps and Inversions(归并排序 || 树状数组)题解
题意:一个逆序对罚钱x元,现在给你交换的机会,每交换任意相邻两个数花钱y,问你最少付多少钱 思路:最近在补之前还没过的题,发现了这道多校的题.显然,交换相邻两个数逆序对必然会变化+1或者-1,那我们肯 ...
- HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- HDU 1556 Color the ball (一维树状数组,区间更新,单点查询)
中文题,题意就不说了 一开始接触树状数组时,只知道“单点更新,区间求和”的功能,没想到还有“区间更新,单点查询”的作用. 树状数组有两种用途(以一维树状数组举例): 1.单点更新,区间查询(即求和) ...
随机推荐
- spark Failed to get database default, returning NoSuchObjectException
解决方法:1)Copy winutils.exe from here(https://github.com/steveloughran/winutils/tree/master/hadoop-2.6. ...
- Spring注入的反射解释
对于如下配置片段: <bean id="id" class="lee.Aclass"> <!--property配置需要依赖注入的属性-- ...
- goodBye wordPress
2016-07-28,我自己在GoDaddy上面注册了一个自己的域名,www.codetree.tech,同时在老薛主机上面购买了一个主机域名.我搭建了一个属于自己的博客,开心了很久.最近收到了域名续 ...
- Cache一致性协议之MESI
http://blog.csdn.net/muxiqingyang/article/details/6615199 Cache一致性协议之MESI 处理器上有一套完整的协议,来保证Cache一致性.比 ...
- [转]Jquery Mobile dialog的生命周期
本文转自:http://www.cnblogs.com/jackhuclan/archive/2012/04/05/2432972.html JQuery Mobile对htm5的移动开发绝对是个好用 ...
- 使用python将元组转换成列表,并替换其中元素
aa = (1, 2, 3, 4, 5, 6) b = [(x == 5 and 8 or x) for x in aa] z = map(lambda x: 8 if x == 5 else x, ...
- 位于XDB的服务器localhost要求用户名和密码,端口占用
问题现象: 从MyEclipse启动部署在tomcat上的web程序后,出现如下问题: 然后访问tomcat主页(http://localhost:8080/),弹出如下对话框: 问题原因: 机器上安 ...
- linux程序分析工具介绍(二)—-ldd,nm
本文要介绍的ldd和nm是linux下,两个用来分析程序很实用的工具.ldd是用来分析程序运行时需要依赖的动态库的工具:nm是用来查看指定程序中的符号表相关内容的工具.下面通过例子,分别来介绍一下这两 ...
- OAuth2.0和企业内部统一登录,token验证方式,OAuth2.0的 Authorization code grant 和 Implicit grant区别
统一登录是个很多应用系统都要考虑的问题,多个项目的话最好前期进行统一设计,否则后面改造兼容很麻烦: cas认证的方式:新公司都是老项目,用的是cas认证的方式,比较重而且依赖较多,winform的项目 ...
- golang精华资源
转载自:http://blog.csdn.net/songbohr/article/details/13292261 1.Learning Go <学习Go语言> http://www.m ...