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.单点更新,区间查询(即求和) ...
随机推荐
- iOS如何实时查看App运行日志
Linux下管理挂载IOS设备——libimobiledevicehttps://www.jianshu.com/p/6423610d3293https://blog.csdn.net/fengzei ...
- 29-----BBS论坛
BBS论坛(二十九) 29.帖子详情页布局 (1)front/hooks.py @bp.errorhandler def page_not_found(): return render_templat ...
- 搭建python开发环境
1.下载python3.6并安装 .网址:https://www.python.org/downloads/windows/ 安装完在cmd看输入“python"是否能够出现python版本 ...
- java——快排、冒泡、希尔、归并
直接贴代码 快排: public class Test { private static void sort(int[] nums){ if(nums == null || nums.length = ...
- sudo 命令问题详解(一)
普通用户不能使用sudo命令的解决办法 https://www.cnblogs.com/fasthorse/p/5949946.html 解决sudo: sorry, you must have a ...
- CEF和JS交互
CefClient提供所有浏览器事件处理的接口,重写CefClient类中的方法处理浏览器事件:包括Browser的生命周期,右键菜单,对话框,状态通知显示,下载事件,拖曳事件,焦点事件,键盘事件,离 ...
- jsp中判断if 和 else
<c:choose> <c:when test="${date==datetime}"> ...
- PCB的版本控制
http://club.szlcsc.com/article/details_1783_1.html 转载自:http://www.amobbs.com/thread-5606014-1-1.html ...
- [转]JS对JSON的操作总结
本文转自:http://www.cnblogs.com/csj222/archive/2013/04/11/3013667.html 对于前端完全是菜鸟,迫于无奈,工作中要用到JS,尤其对JSON的处 ...
- MATLAB顺序结构程序和switch实现选择结构
数据操作 (1)数据输入: A=input(提示信息,选项) (2)数据输出: disp(输出项) (3)程序暂停 pause(延迟秒数)若无内容,则需用户按任意键继续 3.2if语句 整非零为真 矩 ...