Description

Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a social gathering of cows from around the world. MooFest involves a variety of events including haybale stacking, fence jumping, pin the tail on the farmer, and of course, mooing. When the cows all stand in line for a particular event, they moo so loudly that the roar is practically deafening. After participating in this event year after year, some of the cows have in fact lost a bit of their hearing.

Each cow i has an associated "hearing" threshold v(i) (in the range 1..20,000). If a cow moos to cow i, she must use a volume of at least v(i) times the distance between the two cows in order to be heard by cow i. If two cows i and j wish to converse, they must speak at a volume level equal to the distance between them times max(v(i),v(j)).

Suppose each of the N cows is standing in a straight line (each cow at some unique x coordinate in the range 1..20,000), and every pair of cows is carrying on a conversation using the smallest possible volume.

Compute the sum of all the volumes produced by all N(N-1)/2 pairs of mooing cows.

Input

* Line 1: A single integer, N

* Lines 2..N+1: Two integers: the volume threshold and x coordinate for a cow. Line 2 represents the first cow; line 3 represents the second cow; and so on. No two cows will stand at the same location.

Output

* Line 1: A single line with a single integer that is the sum of all the volumes of the conversing cows. 

Sample Input

4
3 1
2 5
2 6
4 3

Sample Output

57

【题意】有n头牛,排列成一条直线,给出每头牛在直线上的坐标d。每头牛有一个v,如果牛i和牛j想要沟通的话,它们必须用max(v[i],v[j]),消耗的能量为:max(v[i],v[j]) * 它们之间的距离.

问要使所有的牛之间都能沟通(两两之间),总共需要消耗多少能量。

【思路】现将v从小到大排列,使得每次取到的是当前最大的v。

c[1]记录当前牛的数量c[2]记录当前所有牛的d之和。(二维树状数组)

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=;
struct node
{
int d,v;
bool operator <(const node &a)const
//从小到大排序,使得当前获得的v一定是出现过最大的。
{
return v<a.v;
}
}moo[N+];
int c[][N+];
int lowbit(int x)
{
return x&(-x);
}
void update(int i,int d,int v)
{
while(d<=N)
{
c[i][d]+=v;
d+=lowbit(d);
}
}
int get_sum(int i,int d)
{
int res=;
while(d)
{
res+=c[i][d];
d-=lowbit(d);
}
return res;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(c,,sizeof(c));
for(int i=;i<=n;i++)
scanf("%d%d",&moo[i].v,&moo[i].d);
sort(moo+,moo++n);
int sum=;//记录所有坐标之和
long long int ans=;
for(int i=;i<=n;i++)
{
int d=moo[i].d;
sum+=d;
update(,d,);//c[1]记录牛数量
update(,d,d);//c[2]记录牛坐标之和
int n1=get_sum(,d);//在i牛及他前面有多少头
int n2=get_sum(,d);//在i牛及他前面的牛坐标和为多少
int tmp1=n1*d-n2;//i左边的坐标差
int tmp2=sum-n2-d*(i-n1);//i右边的坐标差
ans+=(long long int)(tmp1+tmp2)*moo[i].v;
//不用longlong会溢出
}
printf("%lld\n",ans);
}
return ;
}

MooFest_二维树状数组的更多相关文章

  1. 二维树状数组 BZOJ 1452 [JSOI2009]Count

    题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...

  2. HDU1559 最大子矩阵 (二维树状数组)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others)  ...

  3. POJMatrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 22058   Accepted: 8219 Descripti ...

  4. poj 1195:Mobile phones(二维树状数组,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14489   Accepted: 6735 De ...

  5. Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*

    D. Iahub and Xors   Iahub does not like background stories, so he'll tell you exactly what this prob ...

  6. POJ 2155 Matrix(二维树状数组+区间更新单点求和)

    题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...

  7. [poj2155]Matrix(二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 25004   Accepted: 9261 Descripti ...

  8. POJ 2155 Matrix (二维树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17224   Accepted: 6460 Descripti ...

  9. [POJ2155]Matrix(二维树状数组)

    题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...

随机推荐

  1. SQL Server数据库(作业)

    create datebase zuoye2gouse zuoyegocreate table student --学生表( Sno varchar(20) not null primary key, ...

  2. ODBC错误处理

    ODBC 中的错误处理 ODBC 中的错误是使用来自每个 ODBC 函数调用的返回值和 SQLError 函数或 SQLGetDiagRec 函数的返回值进行报告的.SQLError 函数用于 ODB ...

  3. 使用@Controller注解为什么要配置<mvc:annotation-driven />

    自己看了官方文档,也到网上查了下,目前理解如下: <mvc:annotation-driven/>相当于注册了DefaultAnnotationHandlerMapping和Annotat ...

  4. jQuery EasyUI Combobox无法检索中文输入的问题

    在项目里使用了EasyUI的Combobox,当ComboBox的item是英文时,都能正常检索出对应项,但是如果使用中文输入法输入几个字母然后通过按shift键输入时,奇怪的事情发生了,combob ...

  5. ExecuteNonQuery()返回值注意点

    在使用ExecuteNonQuery(),调用存储过程,语句执行无错误,但是返回结果一直是-1 原因: 当使用储存过程时, 要把SET NOCOUNT ON  这个语句去掉, 这样数据就有反回值了 当 ...

  6. BroadcastReceiver的简介

    BroadcastReceiver本质上属于一个监听器,因此实现BroadcastReceiver的方法只要重写BroadcastReceiver的onReceive(Context  context ...

  7. 开始→运行(cmd)命令大全

    gpedit.msc-----组策略 sndrec32-------录音机 Nslookup-------IP地址侦测器 explorer-------打开资源管理器 logoff---------注 ...

  8. 上传项目的更改 info.plist文件

    info.plistOpen AsSource As 添加<key>NSAppTransportSecurity</key>    <dict>        &l ...

  9. LCA-倍增法(在线)

    原文:http://www.tuicool.com/articles/N7jQV32 1. DFS预处理出所有节点的深度和父节点 inline void dfs(int u) { int i; for ...

  10. JavaScript自定义右键菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...