MooFest
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 7077   Accepted: 3181

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
【题意】一群牛参加完牛的节日后都有了不同程度的耳聋,第i头牛听见别人的讲话,别人的音量必须大于v[i],当两头牛i,j交流的时候,交流的最小声音为max{v[i],v[j]}*他们之间的距离。现在有n头牛,求他们之间两两交流最少要的音量和。
【分析】真想不到这题居然用到树状数组。首先将这n头牛按照v值从小到大排序(后面说的排在谁的前面,都是基于这个排序)。这样,排在后面的牛和排在前面的牛讲话,两两之间所用的音量必定为后面的牛的v值,这样一来才有优化的余地。然后,对于某头牛i来说,只要关心跟排在他前面的牛交流就好了。我们必须快速地求出排在他前面的牛和他之间距离的绝对值之和ans,只要快速地求出ans,就大功告成。这里需要两个树状数组。树状数组可以用来快速地求出某个区间内和,利用这个性质,我们可以快速地求出对于牛i,x位置比i小牛的个数,以及这个牛的位置之和。这里就需要两个树状数组,一个记录比x小的牛的个数sump,一个记录比x小的牛的位置之和sumx,然后,我们可以快速地求出牛i和比牛i位置小的牛的所有距离的绝对值为:sump*a[i].x-sumx;也可以方便地求出比牛i位置大的牛到牛i的距离和,即所有距离-sumx-(i-1-sump)*a[i].x;那么此题就差不多了。此题就用奶牛的坐标作为tree数组的下标,比较容易计算距离。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 2e4+;
const int M = ;
const int mod=1e9+;
ll tree[][N];
int n,m;
struct man{
int x,v;
bool operator< (const man &it)const{
return v<it.v;
}
}a[N];
void add(int k,int num){
while(k<=){
tree[][k]+=num;
tree[][k]+=;
k+=k&(-k);
}
}
ll Sum(int k,int d){
ll sum=;
while(k>){
sum+=tree[d][k];
k-=k&(-k);
}
return sum;
}
int main() {
scanf("%d",&n);
int v,x;
for(int i=;i<=n;i++){
scanf("%d%d",&a[i].v,&a[i].x);
}
sort(a+,a++n);
ll ans=;
for(int i=;i<=n;i++){
ll sump=Sum(a[i].x,);
ll sumx=Sum(a[i].x,);
ans+=a[i].v*(sump*a[i].x-sumx)+a[i].v*(Sum(,)-sumx-(i--sump)*a[i].x);
add(a[i].x,a[i].x);
}
printf("%lld\n",ans);
return ;
}

POJ 1990 MooFest(树状数组)的更多相关文章

  1. POJ 1990 MooFest --树状数组

    题意:牛的听力为v,两头牛i,j之间交流,需要max(v[i],v[j])*dist(i,j)的音量.求所有两两头牛交谈时音量总和∑(max(v[i],v[j])*abs(x[j]-x[i])) ,x ...

  2. poj 2229 Ultra-QuickSort(树状数组求逆序数)

    题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...

  3. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  4. poj 2155 Matrix (树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16797   Accepted: 6312 Descripti ...

  5. poj 3067 - Japan(树状数组)

    先按第一个数从大到小排序,相等的情况下,第二个数按照从大到小排序..... 预处理后,照着树状数组写就行了... 注意:k的最大值应取1000*1000 代码如下: include <cstdi ...

  6. poj 2481 - Cows(树状数组)

    看的人家的思路,没有理解清楚,,, 结果一直改一直交,,wa了4次才交上,,, 注意: 为了使用树状数组,我们要按照e从大到小排序.但s要从小到大.(我开始的时候错在这里了) 代码如下: #inclu ...

  7. POJ 3468(树状数组的威力)

    之前说过这是线段树的裸题,但是当看了http://kenby.iteye.com/blog/962159 这篇题解后我简直震惊了,竟然能如此巧妙地转化为用树状数组来处理,附上部分截图(最好还是进入原网 ...

  8. POJ 2352 【树状数组】

    题意: 给了很多星星的坐标,星星的特征值是不比他自己本身高而且不在它右边的星星数. 给定的输入数据是按照y升序排序的,y相同的情况下按照x排列,x和y都是介于0和32000之间的整数.每个坐标最多有一 ...

  9. POJ 2182【树状数组】

    题意: 每头牛有编号,他们乱序排成一排,每头牛只知道前边比自己序号小的有几位. 思路: 递推,最后一只牛的编号是确定的,然后不断进行区间更新,直到找到某个空位前方恰好有n个空位. 这题跟某道排队的题思 ...

  10. POJ 2309 BST 树状数组基本操作

    Description Consider an infinite full binary search tree (see the figure below), the numbers in the ...

随机推荐

  1. [SoapUI] 同一个Resource不同参数时,在两个step里默认打开总是同一个Resource

    当SoapUI里Projects 有两个相同的Resource,只是参数不同时,使用两个Resource创建的step默认打开的总是同一个Resource.我们应当修改method名字为不同,这是So ...

  2. Head First 设计模式 --5 单例模式

    单例模式:确保一个类只有一个实例,并提供一个全局访问点.用到的设计原则:1.封装变化2.组合优于集成3.针对接口变成而不是针对实现4.为交互对象之间的松耦合设计而努力5.类应该对扩展开放,对修改关闭6 ...

  3. AlwaysOn数据同步问题探究

    随着AlwaysOn技术的流行,关于AlwayOn的问题也越来越多,某企业搭建有三副本的AlwaysOn一套,现想修改主节点上某张表的某个数据,看看会出现什么后果,如果结果正常,就同步到其他节点上:如 ...

  4. spring boot初探

    又被领导鄙视了,说让先把程序跑起来,再去研究深层次的东西.. 我又一次没有学会走就要开始跑了..说干就干 eclipse mars下载 新建maven project 加依赖 <dependen ...

  5. Bootstrap结合BootstrapTable的使用,分为两种模试显示列表。 自适应表格

    引用的css: <link href="@Url.Content("~/Css/bootstrap.min.css")" rel="styles ...

  6. Keeplived配置Nginx双机高可用

    一.简介不管是Keepalived还是Heartbeat做高可用,其高可用,都是站在服务器脚本去说的高可用,而不是服务的角度.也就是说,如果服务器DOWN机或者网络出现故障,高可用是可以实现自动切换的 ...

  7. launch文件

    launch在ROS应用中,每个节点通常有许多参数需要设置,为了方便高效操作多个节点,可以编写launch文件,然后用roslaunch命令运行roslaunch: roslaunch [option ...

  8. 快速分析apk工具aapt的使用

    前面walfred已经介绍了使用apktool对apk进行逆向编译,通过apktool我们的确可以反编译已经序列化后的AndroidManifest.xml和资源文件等等,但是有没有一种快速有效的工具 ...

  9. JAVA 泛型与通配符的使用

    泛型的本质是参数化类型.即所操作的数据类型被指定为一个参数. 1.jdk 1.5/1.6 必须显式的写出泛型的类型. 2.jdk 1.7/1.8 不必显式的写出泛型的类型. 一.泛型声明 可以用< ...

  10. sql2008 无法附加数据库

    sql2008 因为数据库正在使用,所以无法获得对数据库的独占访问权---还原或删除数据库的解决方法 数据库还原出现 3154错误 --主备份 --RESTORE DATABASE [NET_CN] ...