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)
求出每一对F*S的总和
可以看到一边是求每个数与其他数的最小值,一边是求每个数与其他数的差距。因此我们可以排序一边,处理另一边。
我们排序H,因为这样对于固定一个Xi Hi,从小到大每次都是Hi去乘以Xi与剩下的所有X的差的总和。
这样我们就可以使用树状数组维护两个值:每个位置值的个数,每个位置值的总大小,接着细心点处理就好了
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<vector>
#include<string>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define eps 1E-8
/*注意可能会有输出-0.000*/
#define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
#define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
#define mul(a,b) (a<<b)
#define dir(a,b) (a>>b)
typedef long long ll;
typedef unsigned long long ull;
const int Inf=<<;
const double Pi=acos(-1.0);
const int Mod=1e9+;
const int Max=;
struct node
{
int sub,minx;
} tree[Max];
int n;
bool cmpt(struct node p1,struct node p2)
{
return p1.sub<p2.sub;
}
bool cmp(struct node p1,struct node p2)
{
return p1.minx<p2.minx;//按照最值排序才能固定一个值
}
int lowbit(int x)
{
return x&(-x);
}
void Add(ll *bit,int x,ll y)
{
while(x<=n)
{
bit[x]+=y;
x+=lowbit(x);
}
return;
}
ll Sum(ll *bit,int x)
{
ll sum=0ll;
while(x)
{
sum+=bit[x];
x-=lowbit(x);
}
return sum;
}
ll bit[Max],bit2[Max];//存个数 存大小
ll Solve()
{
ll ans=0ll;
memset(bit,0ll,sizeof(bit));
memset(bit2,0ll,sizeof(bit2));
for(int i=;i<n;++i)
{
Add(bit2,tree[i].sub,(ll)tree[i].sub);
Add(bit,tree[i].sub,1ll);
}
for(int i=;i<n;++i)
{
Add(bit2,tree[i].sub,(ll)-tree[i].sub);
Add(bit,tree[i].sub,-1ll);
// printf("%d\n",tree[i].sub);
ans+=(ll)tree[i].minx*(
Sum(bit2,n)-Sum(bit2,tree[i].sub)-(ll)tree[i].sub*(Sum(bit,n)-Sum(bit,tree[i].sub))+
(ll)tree[i].sub*Sum(bit,tree[i].sub)-Sum(bit2,tree[i].sub));//关键
//printf("%I64d %I64d\n",ans,Sum(bit2,n)-Sum(bit,n)*tree[i].sub);
}
return ans;
}
int main()
{
int tmp;
while(~scanf("%d",&n))
{
for(int i=; i<n; ++i)
scanf("%d %d",&tree[i].sub,&tree[i].minx);
sort(tree,tree+n,cmpt);//离散化
tree[n].sub=tree[n].minx=-;
tmp=;
for(int i=; i<n; ++i)
{
if(tree[i].sub!=tree[i+].sub)
{
tree[i].sub=tmp;
tmp=i+;
}
else
{
tree[i].sub=tmp;
}
}
sort(tree,tree+n,cmp);
tmp=;
for(int i=; i<n; ++i)
{
if(tree[i].minx!=tree[i+].minx)
{
tree[i].minx=tmp;
tmp=i+;
}
else
{
tree[i].minx=tmp;
}
}
// for(int i=0;i<n;++i)
// printf("%d %d\n",tree[i].sub,tree[i].minx);
printf("%I64d\n",Solve());
}
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 [数状数组]
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 题意 求每个等级的星星有多少个 当前这个星星的左下角 有多少个 星星 它的等级就是多少 和它同一 ...
随机推荐
- mysql5.7导入csv文件
环境: Windows10企业版X64 mysql5.7免安装版(从5.6版本开始,官方不再提供64位的msi版本) 运行mysqld.exe启动mysql进程. 用root登录mysql: mysq ...
- C#内存管理与垃圾回收
垃圾回收还得从根说起,就像生儿育女一样. 根:根是一个位置,存放一个指针,该指针指向托管堆中的一个对象,或是一个空指针不指向任何对象,即为null.根存在线程栈或托管堆中,大部分的跟都在线程栈上,因为 ...
- Filter体现职责链模式
1. 前言 Filter—Filter 技术是servlet2.3 新增加的功能.完成的流程:对用户请求进行预处理,接着将请求交给Servlet进行处理并生成响应,最后Filter再对服务器响应进行后 ...
- linux用命令删除重复行
文本处理时,经常要删除重复行,下面是三种方法 第一,用sort+uniq,注意,单纯uniq是不行的. sort -n test.txt | uniq 第二,用sort+awk命令,注意,单纯awk同 ...
- Computer Vision: OpenCV, Feature Tracking, and Beyond--From <<Make Things See>> by Greg
In the 1960s, the legendary Stanford artificial intelligence pioneer, John McCarthy, famously gave a ...
- [BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居
[BZOJ1604][Usaco2008 Open]Cow Neighborhoods 奶牛的邻居 试题描述 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发 ...
- edghasdz
匹配2016年10月8日19时51分50秒匹配 可以使用
- iOS 多个异步网络请求全部返回后再执行具体逻辑的方法
对于dispatch多个异步操作后的同步方法,以前只看过dispatch_group_async,看看这个方法的说明: * @discussion * Submits a block to a dis ...
- Memcache 内存分配策略和性能(使用)状态检查
前言: 一直在使用Memcache,但是对其内部的问题,如它内存是怎么样被使用的,使用一段时间后想看看一些状态怎么样?一直都不清楚,查了又忘记,现在整理出该篇文章,方便自己查阅.本文不涉及安装.操作. ...
- poj1988_Cube Stacking
Cube Stacking Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 24130 Accepted: 8468 Ca ...