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.单点更新,区间查询(即求和) ...
随机推荐
- jq得到总价
<html><head> <title></title> {load href="/static/js/jquery-1.9.1.min.js ...
- python练习六十一:文件处理,读取文件内容
python练习六十一:文件处理,读取文件内容 假设要读取text.txt文件中内容 写文件(如果有文件,那直接调用就行,我这里自己先创建的文件) list1 = ['python','jave',' ...
- 转 ogg issue
1.http://www.dbdream.com.cn/2013/05/17/ogg-00446%E9%94%99%E8%AF%AF%E8%A7%A3%E5%86%B3/ OGG-00446错误解决 ...
- 可视化开发_AppInventor2似乎被抛弃了
工具 blockly google,mixly,scratch,app inventor2 的分别 可视化编程,青雀,来自 白鹭 没源码 如果想二次开发呢,初版拖拽控件生成,后期维护的时候找程序员加功 ...
- JobService 7.0 定时任务不生效
代码 // 构建JobInfo对象,传递给JobSchedulerService JobInfo.Builder builder = new JobInfo.Builder(JOB_ID,new Co ...
- 2019.03.20 读书笔记 关于Reflect与Emit的datatable转list的效率对比
Reflect public static List<T> ToListByReflect<T>(this DataTable dt) where T : new() { Li ...
- JavaScript操作符(=?,)优先级
JavaScript操作符优先级: 关于最后3个运算符的优先级比较,下面通过一个实例来具体说明: var a,b,c; a = 3,4,5; b = a--,--a,a; c = a ? b++ : ...
- ThreadFactory
在Java中有两类线程:User Thread(用户线程).Daemon Thread(守护线程) 比如,任何一个守护线程都是整个JVM中所有非守护线程的保姆:只要当前JVM实例中尚存在任何一个非守护 ...
- php和java的区别
php和java的区别 前几天有个大学的同学给我来电话,他是在培训java的,然后我们就讨论了一下关于php和java的优劣区别(我们的是初学者,所以下面发表的内容可能不会很精准到位,望体谅): 我们 ...
- 转载【Ubuntu】Ubuntu14.04虚拟机调整窗口大小自适应VMware14窗口
Ubuntu屏幕居中一小块,很不好看 查看-–> 自动调整大小—>自动适应客户机/自动适应窗口 切一下就可以把Ubuntu图的界面大小调的和VMware窗口自适应了 效果: 转载 自h ...