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

  1. 4
  2. 3 1
  3. 2 5
  4. 2 6
  5. 4 3

Sample Output

  1. 57

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

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

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

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

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<algorithm>
  5. using namespace std;
  6. const int N=;
  7. struct node
  8. {
  9. int d,v;
  10. bool operator <(const node &a)const
  11. //从小到大排序,使得当前获得的v一定是出现过最大的。
  12. {
  13. return v<a.v;
  14. }
  15. }moo[N+];
  16. int c[][N+];
  17. int lowbit(int x)
  18. {
  19. return x&(-x);
  20. }
  21. void update(int i,int d,int v)
  22. {
  23. while(d<=N)
  24. {
  25. c[i][d]+=v;
  26. d+=lowbit(d);
  27. }
  28. }
  29. int get_sum(int i,int d)
  30. {
  31. int res=;
  32. while(d)
  33. {
  34. res+=c[i][d];
  35. d-=lowbit(d);
  36. }
  37. return res;
  38. }
  39. int main()
  40. {
  41. int n;
  42. while(~scanf("%d",&n))
  43. {
  44. memset(c,,sizeof(c));
  45. for(int i=;i<=n;i++)
  46. scanf("%d%d",&moo[i].v,&moo[i].d);
  47. sort(moo+,moo++n);
  48. int sum=;//记录所有坐标之和
  49. long long int ans=;
  50. for(int i=;i<=n;i++)
  51. {
  52. int d=moo[i].d;
  53. sum+=d;
  54. update(,d,);//c[1]记录牛数量
  55. update(,d,d);//c[2]记录牛坐标之和
  56. int n1=get_sum(,d);//在i牛及他前面有多少头
  57. int n2=get_sum(,d);//在i牛及他前面的牛坐标和为多少
  58. int tmp1=n1*d-n2;//i左边的坐标差
  59. int tmp2=sum-n2-d*(i-n1);//i右边的坐标差
  60. ans+=(long long int)(tmp1+tmp2)*moo[i].v;
  61. //不用longlong会溢出
  62. }
  63. printf("%lld\n",ans);
  64. }
  65. return ;
  66. }

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. 聚类算法:K-means 算法(k均值算法)

    k-means算法:      第一步:选$K$个初始聚类中心,$z_1(1),z_2(1),\cdots,z_k(1)$,其中括号内的序号为寻找聚类中心的迭代运算的次序号. 聚类中心的向量值可任意设 ...

  2. 登陆验证前对用户名和密码加密之后传输数据---base64加密

    以下这种方法是加密传输的简单实现 1,base64.js /** * * Base64 encode / decode * * */ function Base64() { // private pr ...

  3. mac 无法ssh localhost

    mac 无法ssh localhost,错误提示:bash: /usr/local/bin/ssh_session: Permission denied在网上找了很久也没有找到解决方案,最后根据提示自 ...

  4. iOS8 针对开发者所拥有的新特性汇总如下

    iOS8 针对开发者所拥有的新特性汇总如下 1.支持第三方键盘 2.自带网页翻译功能(即在线翻译) 3.指纹识别功能开放:第三方软件可以调用 4.Safari浏览器可直接添加新的插件. 5.可以把一个 ...

  5. Servlet作业--实现注册和登录

    1.注册页面  zhuce.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...

  6. elasticsearch插件之一:bigdesk

    bigdesk是elasticsearch的一个集群监控工具,可以通过它来查看es集群的各种状态,如:cpu.内存使用情况,索引数据.搜索情况,http连接数等. 可用项目git地址:https:// ...

  7. Jmeter简单测试elasticsearch服务器

    入门知识: Jmeter是一个非常好用的压力测试工具.  Jmeter用来做轻量级的压力测试,非常合适,只需要十几分钟,就能把压力测试需要的脚本写好. 顾名思义:压力测试,就是  被测试的系统,在一定 ...

  8. 创建单例的DbContext

    /// <summary> /// 说明: /// 创建日期:2016/9/30 14:49:48 /// 创建人:曹永承 /// </summary> public clas ...

  9. uiwebview 清缓存。,mark

    //清除cookies NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCook ...

  10. [开发笔记]-jQuery获取checkbox选中项等操作及注意事项

    今天在做一个项目功能时需要显示checkbox选项来让用户进行选择,由于前端不是很熟练,所以做了一个简单的Demo,其中遇到一些小问题,特记录下来,希望能帮到遇到类似问题的同学们. 1. 获取chec ...