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=1<<28;
const double Pi=acos(-1.0);
const int Mod=1e9+7;
const int Max=100010;
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=0;i<n;++i)
    {
        Add(bit2,tree[i].sub,(ll)tree[i].sub);
        Add(bit,tree[i].sub,1ll);
    }
    for(int i=0;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=0; i<n; ++i)
            scanf("%d %d",&tree[i].sub,&tree[i].minx);
        sort(tree,tree+n,cmpt);//离散化
        tree[n].sub=tree[n].minx=-1;
        tmp=1;
        for(int i=0; i<n; ++i)
        {
            if(tree[i].sub!=tree[i+1].sub)
            {
                tree[i].sub=tmp;
                tmp=i+2;
            }
            else
            {
                tree[i].sub=tmp;
            }
        }
        sort(tree,tree+n,cmp);
        tmp=1;
                for(int i=0; i<n; ++i)
        {
            if(tree[i].minx!=tree[i+1].minx)
            {
                tree[i].minx=tmp;
                tmp=i+2;
            }
            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 0;
}
时间: 2024-07-28 14:48:11

HDU 3015 Disharmony Trees(树状数组)的相关文章

Disharmony Trees 树状数组

Disharmony Trees Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description One day Sophia finds a very big square. There are n trees in the square. They are all so tall. Sophia is very interesting in them.

hdu3015 Disharmony Trees(树状数组+排序)

题目链接:点击打开链接 解题思路: 1.首先对x和高度h分别从小到大排序记录排名 2.然后对高度h按从大到小排序(保证当前要计算的树的高度是所有已经遍历的树中最小高度,便于计算S=min(h1,h2)) 3.循环遍历数组,每次遍历向树状数组C中t[i].rx位置增加t[i].rx,向树状数组C1中t[i].rx位置增加1 解析:C记录排名和,C1记录个数 所以以t[i].rh为最小值的点对的和为 t[i].rh*(sum(n)-sum(t[i].rx)-(LL)t[i].rx*(sum1(n)-

hdu 3015 Disharmony Trees (离散化+树状数组)

Disharmony Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 663    Accepted Submission(s): 307 Problem Description One day Sophia finds a very big square. There are n trees in the square. T

HDU 1541 Stars (树状数组)

Problem Description Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given

HDU 3854 Glorious Array(树状数组)

题意:给一些结点,每个结点是黑色或白色,并有一个权值.定义两个结点之间的距离为两个结点之间结点的最小权值当两个结点异色时,否则距离为无穷大.给出两种操作,一种是将某个结点改变颜色,另一个操作是询问当前距离小于K的结点有多少对,K是一个定值. 思路:先求最初时候小于k的结点有多少对,然后每次改变颜色的时候,统计该点左侧和右侧各有多少同色和异色的结点(这一步使用树状数组),分别处理就行.另外需要预处理离某个结点最近的两个距离小于K的结点的位置. 代码写的略乱. #include<cstdio> #

HDU 3333 Turing Tree 树状数组 离线查询

题意: 给你一个数列,然后有n个查询,问你给定区间中不同数字的和是多少. 思路还是比较难想的,起码对于蒟蒻我来说. 将区间按照先右端点,后左端点从小到大排序之后,对于每个查询,我只要维护每个数字出现的最后一次就可以了(这个结论稍微想一下就可以证明是正确的). 然后就是简单的点更新,区间求和问题了- #include <cstdio> #include <cstring> #include <iostream> #include <map> #include

HDU 2689 Sort it (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2689 Sort it Problem Description You want to processe a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. Then how many times it 

HDU 2492 Ping pong (树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2492 Ping pong Problem Description N(3<=N<=20000) ping pong players live along a west-east street(consider the street as a line segment). Each player has a unique skill rank. To improve their skill rank

HDU 1541 Stars(树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1541 解析: 题意:大概就是计算每颗星星左下边包括了多少颗星星,这个数值就是level.左下边不包括本身,不超过本身的x,y的坐标,可以等于.问每种level有多少颗星星. 这题,一开始想不到怎么用到树状数组,后来看了一下,发现题目给的数据是已经按x,y排好序的,所以我们可以不用管y的值. 注意: 1.每次输入一个坐标对之后,都要计算一下这个它的level. 2.此题的x坐标可以为0,而树状数组是从

HDU 1892 二维树状数组

See you~ Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 3485    Accepted Submission(s): 1103 Problem Description Now I am leaving hust acm. In the past two and half years, I learned so many kno