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
    我觉得我需要收回当初说树状数组比线段树简单这句话。。太坑了,一题比一题坑。。完全按题解写的
    题意:给vi】,xi】要求所以的牛的音量和即xi】-xj】*maxvi】,vj】)之和
    题解:两个树状数组数组一起使用,一个求x之前的比x坐标小的数(a),一个求x之前的比x坐标小的坐标和(b);
    那么比x小的坐标和x的坐标的总坐标差是a*(e[i].x)-b;比x大的坐标和x的坐标的总坐标差是总坐标-b-(i-1-a)*e[i].x
  1. #include<map>
  2. #include<set>
  3. #include<cmath>
  4. #include<queue>
  5. #include<stack>
  6. #include<vector>
  7. #include<cstdio>
  8. #include<iomanip>
  9. #include<cstdlib>
  10. #include<cstring>
  11. #include<iostream>
  12. #include<algorithm>
  13. #define pi acos(-1)
  14. #define ll long long
  15. #define mod 1000000007
  16. #define ls l,m,rt<<1
  17. #define rs m+1,r,rt<<1|1
  18.  
  19. using namespace std;
  20.  
  21. const double g=10.0,eps=1e-;
  22. const int N=+,maxn=+,inf=0x3f3f3f3f;
  23.  
  24. int s[][N];
  25. struct edge{
  26. ll v,x;
  27. }e[N];
  28.  
  29. bool comp(const edge &a,const edge &b)
  30. {
  31. return a.v<b.v;
  32. }
  33. void add(int i,ll x,int d)
  34. {
  35. while(i<=N){
  36. s[d][i]+=x;
  37. i+=i&(-i);
  38. }
  39. }
  40. ll sum(int i,int d)
  41. {
  42. ll ans=;
  43. while(i>){
  44. ans+=s[d][i];
  45. i-=i&(-i);
  46. }
  47. return ans;
  48. }
  49.  
  50. int main()
  51. {
  52. ios::sync_with_stdio(false);
  53. cin.tie();
  54. // cout<<setiosflags(ios::fixed)<<setprecision(2);
  55. int n;
  56. while(cin>>n){
  57. memset(s,,sizeof s);
  58. for(int i=;i<=n;i++)cin>>e[i].v>>e[i].x;
  59. sort(e+,e++n,comp);
  60. ll ans=;
  61. for(int i=;i<=n;i++)
  62. {
  63. ll a=sum(e[i].x,),b=sum(e[i].x,);
  64. // cout<<sum(N,1)-b-(i-1-a)*e[i].x<<endl;
  65. ans+=(a*e[i].x-b+sum(N,)-b-(i--a)*e[i].x)*e[i].v;
  66. add(e[i].x,,);//0是比x小的牛的个数
  67. add(e[i].x,e[i].x,);//1是比x小的牛的距离和
  68. }
  69. cout<<ans<<endl;
  70. }
  71. return ;
  72. }

poj1990树状数组的更多相关文章

  1. hdu3015,poj1990树状数组

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3015 题意:给定n组数,每组数有值x和值h,求n组数两两的val的总和.将所有x和所有h分别离散化(不 ...

  2. POJ-1990 MooFest---两个树状数组

    题目链接: https://vjudge.net/problem/POJ-1990 题目大意: 一群牛参加完牛的节日后都有了不同程度的耳聋,第i头牛听见别人的讲话,别人的音量必须大于v[i],当两头牛 ...

  3. hdu3015树状数组 poj1990的离散化版本

    都是一类题目,推导调试比较烦,想出来还是不难的 /* 给定n个点对,按一维升序排序一次,每个点的序号为Di,按二维升序排序一次,每个点的序号为Hi 求sum{w(i,j)} w(i,j)=abs(Di ...

  4. poj1990两个树状数组

    垃圾poj交不上去 /* 按权值从小到大排序, 两个树状数组维护权值小于等于并且在i左边的点的个数和权值 */ #include<iostream> #include<cstring ...

  5. POJ_1990 MooFest 【树状数组】

    一.题面 POJ1990 二.分析 一个简单的树状数组运用.首先要把样例分析清楚,凑出57,理解一下.然后可以发现,如果每次取最大的v就可以肆无忌惮的直接去乘以坐标差值就可以了,写代码的时候是反着来的 ...

  6. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...

  7. bzoj1878--离线+树状数组

    这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...

  8. codeforces 597C C. Subsequences(dp+树状数组)

    题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...

  9. BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

随机推荐

  1. "the hypervisor is not running" 故障

    在我们日常服务器管理中,常常会遇到创建虚拟机,如果在一台新部署的 Hyper-V 上新建一个 Virtual Machine 时,出现错误信息:"The virtual machine co ...

  2. C# .NET 逻辑层的框架设计

    前述:在我的了解中,一个大项目的逻辑层是不可捉摸的,对于不同项目或场景都是不同的逻辑.先说明,我的想法是对逻辑层类结构,以及如何操作逻辑的方法进行抽象的封装.并且考虑将不同类,或者不同程序集中的逻辑方 ...

  3. js代码实现放大镜效果

    每当打开淘宝,天猫等pc端时,看到心仪的物品时,点击图片时,便呈现出放大的效果.在没有去理解分析它的原理时,感觉非常的神奇,当真正地去接触,也是非常好理解.如下图展示所见: 很是常见,在此记载一下,毕 ...

  4. centos登录时一闪而过,显示module is unknown

    原因是修改了在/etc/pam.d/login中加入了: session required /lib/security/pam_limits.so session required pam_limit ...

  5. Nagios监控远程主机

    p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; line-height: 150%; fon ...

  6. Object-C定时器,封装GCD定时器的必要性!!! (二)

    上一篇:Object-C定时器,封装GCD定时器的必要性!!! (一) 上一篇认识了Object-C中的几种定时器,这一篇将Dispatch定时器(GCD定时器)封装起来. p.p1 { margin ...

  7. web中关于垃圾回收的一些观点

    感觉dom大神的解惑 关于引用计数法,注意引用的方向性就行.  A.addEventListner(B.func), 那么是增加了A对B的引用.如果A是不可回收的对象,比如全局的Stage,或者单例. ...

  8. Visual Studio 2017创建.net standard类库编译出错原因

    正式版上个月已经Release了,从那时到现在经常会收到更新提示,估计问题还不少吧!其中最吸引我的当然是.net standard与.net core. 刚好最近接触.net standard项目,新 ...

  9. 老李分享:https协议

    老李分享:https协议   最近我们看到很多站点使用 HTTPS 协议提供网页服务.通常情况下我们都是在一些包含机密信息的站点像银行看到 HTTPS 协议. 如果你访问 google,查看一下地址栏 ...

  10. POPTEST联合创始人李爱然的“IT培训创业的随想"

    POPTEST联合创始人李爱然的“IT培训创业的随想" IT教育行业最大的问题是缺少像互联网行业一样的产品经理. 大多数IT教育机构在早期依靠个人或者一套课程开创了一定的局面,随着机构的壮大 ...