HDU3015 Disharmony Trees

本文介绍了一种使用树状数组解决特定问题的方法,即计算一系列树之间的不和谐值总和。通过定义不和谐值并采用树状数组进行高效计算,实现了对大量数据的有效处理。

Disharmony Trees


Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 702 Accepted Submission(s): 329


Problem 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.


She finds that trees maybe disharmony and the Disharmony Value between two trees is associated with two value called FAR and SHORT.

The FAR is defined as the following:If we rank all these trees according to their X Coordinates in ascending order.The tree with smallest X Coordinate is ranked 1th.The trees with the same X Coordinates are ranked the same. For example,if there are 5 tree with X Coordinates 3,3,1,3,4. Then their ranks may be 2,2,1,2,5. The FAR of two trees with X Coordinate ranks D1 and D2 is defined as F = abs(D1-D2).

The SHORT is defined similar to the FAR. If we rank all these trees according to their heights in ascending order,the tree with shortest height is ranked 1th.The trees with the same heights are ranked the same. For example, if there are 5 tree with heights 4,1,9,7,4. Then their ranks may be 2,1,5,4,2. The SHORT of two trees with height ranks H1 and H2 is defined as S=min(H1,H2).

Two tree’s Disharmony Value is defined as F*S. So from the definition above we can see that, if two trees’s FAR is larger , the Disharmony Value is bigger. And the Disharmony value is also associated with the shorter one of the two trees.

Now give you every tree’s X Coordinate and their height , Please tell Sophia the sum of every two trees’s Disharmony value among all trees.


Input
There are several test cases in the input

For each test case, the first line contain one integer N (2 <= N <= 100,000) N represents the number of trees.

Then following N lines, each line contain two integers : X, H (0 < X,H <=1,000,000,000 ), indicating the tree is located in Coordinates X and its height is H.


Output
For each test case output the sum of every two trees’s Disharmony value among all trees. The answer is within signed 64-bit integer.


Sample Input
2
10 100
20 200
4
10 100
50 500
20 200
20 100


Sample Output
1
13


Source
2009 Multi-University Training Contest 12 - Host by FZU

题意:给n棵树,每个数都有位置和高度,对位置和高度排序,相同位置和高度的序号相同(见题目),F为每两棵树的位置之差,S为每两棵数的高度中的小的那个,求所有树的F*S之和。

分析:我认为这题的关键就是想到对S从小到大进行排序,然后求解。我们可以用两个树状数组,一个记录位置的大小,一个记录这个位置有没有树,首先,把所有的点加入树状数组,然后因为S是从小到大排序的,我们从小的选起,每个最小的S对应的位置的大小记为a,找出a之前位置还存在的个数,然后 个数*a-a之前位置大小的总和,同理再找出比a位置大的个数,a之后位置大小的总和-个数*a,然后两个相加再乘上这个位置的S就可以,算完一个点就把这个点从树状数组中删除。。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL __int64
using namespace std;
const int MAXN=100010;
struct node
{
    int a,b;
    int num1,num2;
}p[MAXN];
LL c[MAXN],d[MAXN];
bool cmpa(node x,node y)
{
    return x.a<y.a;
}
bool cmpb(node x,node y)
{
    return x.b<y.b;
}
bool cmpn(node x,node y)
{
    if(x.num2==y.num2)
        return x.num1<y.num1;
    return x.num2<y.num2;
}
int lowbit(int x)
{
    return x&(-x);
}
void updatec(int x,int val)
{
    while(x<MAXN)
    {
        c[x]+=val;
        x+=lowbit(x);
    }
}
void updated(int x,int val)
{
    while(x<MAXN)
    {
        d[x]+=val;
        x+=lowbit(x);
    }
}
LL getsumc(int x)
{
    LL ans=0;
    while(x>0)
    {
        ans+=c[x];
        x-=lowbit(x);
    }
    return ans;
}
LL getsumd(int x)
{
    LL ans=0;
    while(x>0)
    {
        ans+=d[x];
        x-=lowbit(x);
    }
    return ans;
}
int main()
{
    int n,i,j;
    while(scanf("%d",&n)==1)
    {
        for(i=1;i<=n;i++)
            scanf("%d%d",&p[i].a,&p[i].b);
        sort(p+1,p+1+n,cmpa);
        for(i=1;i<=n;i++)
        {
            if(i!=1&&p[i].a==p[i-1].a)
                p[i].num1=p[i-1].num1;
            else
                p[i].num1=i;
        }
        sort(p+1,p+1+n,cmpb);
        for(i=1;i<=n;i++)
        {
            if(i!=1&&p[i].b==p[i-1].b)
                p[i].num2=p[i-1].num2;
            else
                p[i].num2=i;
        }
        sort(p+1,p+1+n,cmpn);
        memset(d,0,sizeof(d));
        memset(c,0,sizeof(c));
        for(i=1;i<=n;i++)
        {
            updatec(p[i].num1,p[i].num1);
            updated(p[i].num1,1);
        }
        LL ans=0;
        for(i=1;i<=n;i++)
        {
            LL tmp1=p[i].num1*getsumd(p[i].num1-1)-getsumc(p[i].num1-1);
            LL tmp2=getsumc(MAXN)-getsumc(p[i].num1)-p[i].num1*(getsumd(MAXN)-getsumd(p[i].num1));
            ans=ans+(tmp1+tmp2)*p[i].num2;
            updatec(p[i].num1,-p[i].num1);
            updated(p[i].num1,-1);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


内容概要:本文档是一份关于交换路由配置的学习笔记,系统地介绍了网络设备的远程管理、交换机与路由器的核心配置技术。内容涵盖Telnet、SSH、Console三种远程控制方式的配置方法;详细讲解了VLAN划分原理及Access、Trunk、Hybrid端口的工作机制,以及端口镜像、端口汇聚、端口隔离等交换技术;深入解析了STP、MSTP、RSTP生成树协议的作用与配置步骤;在路由部分,涵盖了IP地址配置、DHCP服务部署(接口池与全局池)、NAT转换(静态与动态)、静态路由、RIP与OSPF动态路由协议的配置,并介绍了策略路由和ACL访问控制列表的应用;最后简要说明了华为防火墙的安全区域划分与基本安全策略配置。; 适合人群:具备一定网络基础知识,从事网络工程、运维或相关技术岗位1-3年的技术人员,以及准备参加HCIA/CCNA等认证考试的学习者。; 使用场景及目标:①掌握企业网络中常见的交换与路由配置技能,提升实际操作能力;②理解VLAN、STP、OSPF、NAT、ACL等核心技术原理并能独立完成中小型网络搭建与调试;③通过命令示例熟悉华为设备CLI配置逻辑,为项目实施和故障排查提供参考。; 阅读建议:此笔记以实用配置为主,建议结合模拟器(如eNSP或Packet Tracer)动手实践每一条命令,对照拓扑理解数据流向,重点关注VLAN间通信、路由选择机制、安全策略控制等关键环节,并注意不同设备型号间的命令差异。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值