Time Limit: 1000MS
Memory Limit: 30000K

Total Submissions: 8141
Accepted: 3674

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

Source

USACO 2004 U S Open

题意:

给定一个序列,序列两点之间的花费为 两点之间的距离*(两点之间价值大者),求所有N(N-1)/2个点对的花费之和

思路:

对某个点来说,其等级可以作为乘数的情况是 它与它左右两边比它的等级小的组成点对

设某个点,坐标为 Xc,它的等级为val,其左边比它小的有n1个,右边比它小的有n2个,左边比比它小的坐标为Xi(1<=i<=n1),右边比它小的坐标为 Xj(1<=j<=n2)

则经过推导,我们可以得到其对答案的贡献为:

(n1-n2) * Xc * val + val ( segma(Xi) – segma(Xj) )

所以我们需要统计两个类型的值:左右两边比它等级小的点的个数,左右两边比它等级小的点的坐标和。

这两个类型的值需要分别求出来,但只要知道了其中一个就可得到另外一个(知道了左边的就可以推导出右边的)

我们利用树状数组来统计这些值,具体做法如下:

将所有点按照等级排序,则某个点现在的标号即为比它小的所有点(包括左和右)的总个数。

我们使用两个树状数组,从左到右一次插入每个节点,一个树状数组统计左边比它等级小的点的个数,另一个统计点的坐标和

第一个树状数组好理解,主要困难在于构造第二个树状数组

只需要每次 update(x[i], x[i])即可

这样的话, x[i] 插入之前,getsum(x[i])就是左边比i点等级小的点的坐标和

代码:

  1 /*
2 * @FileName: D:\代码与算法\2017训练比赛\七月训练四\1013.cpp
3 * @Author: Pic
4 * @Date: 2017-08-04 21:31:01
5 * @Last Modified time: 2017-08-05 20:21:10
6 */
7
8 #include <iostream>
9 #include <algorithm>
10 #include <cstdio>
11 #include <string.h>
12 #include <queue>
13 using namespace std;
14 typedef __int64 ll;
15 const int MAXN=20000+30;
16 //注意,这道题的vol有相同的情况出现。这样的话就不能先按x坐标排序,统计左右比这个点小的个数,然后按照vol排序统计左右比这个点小的点的vol和
17 //而是应该同步地统计这两个量,防止相同的情况
18 struct BIT{
19 void init()
20 {
21 memset(Tree_sum,0,sizeof(Tree_sum));
22 }
23 ll Tree_sum[MAXN];//存储树状数组的数组
24 //int maxn=20000+30; //树状数组的下标最大值
25 ll lowbit(ll x) //lowbit函数, 找到x与与 *最近的一个末位连续0比他多的数* 的距离
26 {
27 return x&(-x);
28 }
29 ll getsum(ll x) //获取0至x区间的和
30 {
31 ll sum=0;
32 for(;x>0;x-=lowbit(x)){
33 sum+=Tree_sum[x];
34 }
35 return sum;
36 }
37 void update(ll x,ll v) //更新某点的值
38 {
39 for(;x<=MAXN;x+=lowbit(x)){
40 Tree_sum[x]+=v;
41 }
42 }
43 };
44 BIT tr,tr2;
45 struct node
46 {
47 ll vol,x,id;
48 }a[MAXN];
49 ll n1[MAXN];
50 bool cmp(node a,node b)
51 {
52 return a.vol<b.vol;
53 }
54 bool cmp1(node a,node b)
55 {
56 return a.x<b.x;
57 }
58 int main(){
59 //freopen("data.in","r",stdin);
60 ll n;
61 while(~scanf("%I64d",&n)){
62 for(int i=0;i<n;i++){
63 //scanf("%d%d",&a[i].vol,&a[i].x);
64 //cin>>a[i].vol>>a[i].x;
65 scanf("%I64d%I64d",&a[i].vol,&a[i].x);
66 a[i].id=i;
67 }
68 tr.init();
69 tr2.init();
70 sort(a,a+n,cmp1);
71 sort(a,a+n,cmp);
72 ll res=0,sum=0,sumn=0;
73 for(int i=0;i<n;i++){
74 sumn=tr.getsum(a[i].x);
75 ll suma=tr2.getsum(a[i].x);
76 res+=((sum-suma-suma)*a[i].vol);
77 res+=((2*sumn-i)*a[i].vol*a[i].x);
78 //cout<<res<<endl;
79 tr2.update(a[i].x,a[i].x);
80 tr.update(a[i].x,1);
81 sum+=a[i].x;
82 }
83 //printf("%lld\n",res);
84 //cout<<res<<endl;
85 printf("%I64d\n",res);
86 }
87 return 0;
88 }

POJ1990--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. .Net面试题三

    1..Net中类和结构的区别? 2.死锁地必要条件?怎么克服? 3.接口是否可以继承接口?抽象类是否可以实现接口?抽象类是否可以继承实体类? 4.构造器COnstructor是否可以被继承?是否可以被 ...

  2. codeforce C. Success Rate

    写完这道题目才发现自己对二分的理解太浅了 这题是典型的利用二分“假定一个问题可行并求最优解” 二分是通过不断缩小区间来缩小解的范围,最终得出解的算法 我们定义一个c(x) 表示判断函数 如果对任意y& ...

  3. Cannot resolve the collation conflict between "SQL_Latin1_General_CP1_CI_AS" and "Chinese_PRC_CI_AI" in the equal to operation.

    Executed as user: NT AUTHORITY\SYSTEM. Cannot resolve the collation conflict between "Chinese_P ...

  4. 无障碍开发(四)之ARIA aria-***状态值

    aria-***状态值

  5. 题解luoguP2054 BZOJ1965【[AHOI2005]洗牌】

    题目链接: https://www.luogu.org/problemnew/show/P2054 https://www.lydsy.com/JudgeOnline/problem.php?id=1 ...

  6. 这周末又参加班里同学生日party,同学父母包场2小时花费大约1000美金左右。

    今天班上Claire的生日,邀请了几个小朋友去pump it up.特别特别开心,因为她父母选的时间特别好晚上6-8点小孩子玩疯了以后吃的特别多.

  7. O040、Migrate Instance 操作详解

    参考https://www.cnblogs.com/CloudMan6/p/5538599.html   Migrate 操作的作用是将instance 从当前的计算节点迁移到其他的计算节点上.   ...

  8. loj 2759「JOI 2014 Final」飞天鼠

    loj 这题有在一棵树上上升或者下降的操作,稍加分析后可以发现上升操作如果不是一定要做(指高度不足以到下一棵树或者是最后到达\(n\))就不做,下降操作也是如果不是一定要做(指到达下一棵树时高度过高) ...

  9. sql的关键字

    整理一下sql的关键字,一直都在用,只是很少去整理,所以今天简单整理一下,主要是整理CRUD的一些关键字. 写在前面:sql 不区分大小写 select 简单查询语句 select columnNam ...

  10. RAD Studio 10.3.2七大新功能介绍

    RAD Studio 10.3.2七大新功能 Delphi支持macOS 64位应用的开发. C++Builder中Windows 64位平台支持C ++ 17特性. C ++ LSP 代码洞察改进. ...