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
我觉得我需要收回当初说树状数组比线段树简单这句话。。太坑了,一题比一题坑。。完全按题解写的
题意:给v【i】,x【i】要求所以的牛的音量和即x【i】-x【j】*max(v【i】,v【j】)之和
题解:两个树状数组数组一起使用,一个求x之前的比x坐标小的数(a),一个求x之前的比x坐标小的坐标和(b);
那么比x小的坐标和x的坐标的总坐标差是a*(e[i].x)-b;比x大的坐标和x的坐标的总坐标差是总坐标-b-(i-1-a)*e[i].x
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1 using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f3f; int s[][N];
struct edge{
ll v,x;
}e[N]; bool comp(const edge &a,const edge &b)
{
return a.v<b.v;
}
void add(int i,ll x,int d)
{
while(i<=N){
s[d][i]+=x;
i+=i&(-i);
}
}
ll sum(int i,int d)
{
ll ans=;
while(i>){
ans+=s[d][i];
i-=i&(-i);
}
return ans;
} int main()
{
ios::sync_with_stdio(false);
cin.tie();
// cout<<setiosflags(ios::fixed)<<setprecision(2);
int n;
while(cin>>n){
memset(s,,sizeof s);
for(int i=;i<=n;i++)cin>>e[i].v>>e[i].x;
sort(e+,e++n,comp);
ll ans=;
for(int i=;i<=n;i++)
{
ll a=sum(e[i].x,),b=sum(e[i].x,);
// cout<<sum(N,1)-b-(i-1-a)*e[i].x<<endl;
ans+=(a*e[i].x-b+sum(N,)-b-(i--a)*e[i].x)*e[i].v;
add(e[i].x,,);//0是比x小的牛的个数
add(e[i].x,e[i].x,);//1是比x小的牛的距离和
}
cout<<ans<<endl;
}
return ;
}

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. 用js控制css属性

    在用js控制css属性时,行内css属性可以任意控制,但若是在<style></style>中写的css属性,均不能用alert读取,但是赋值却有几种现象, 第一种:无法读取, ...

  2. Sublime Text前端开发环境配置

    Sublime Text是前端开发不可不说的编辑器,本文以Sublime Text 3为例讲解一下如何搭建前端的开发环境. 下载Sublime Text 3 下载地址 #==> Sublime ...

  3. Zkdash安装

    zkdash是一个zookeeper的管理界面,也可以作为任何基于zookeeper的配置管理工具,比如:Qconf 1.拉取代码 #git clone https://github.com/irea ...

  4. C++ 网络爬虫实现

    最近有个概念吵得很火,网络爬虫,但是基本都是用什么python或者JAVA写,貌似很少看到用c++写的,我在网上找了一个,看到其实还是很简单的算法 算法讲解:1.遍历资源网站 2.获取html信息   ...

  5. 在 WPF 中使用 Path 路径

    在 WPF 中总会修改 Button 的 Style,比如一个自定义的 Close 按钮.刚入门的可能会用一张 PNG 格式的图片来做这个按钮的 Icon,但这个是不优雅的.而且你要改的时候还得去操作 ...

  6. 我是如何处理大并发量订单处理的 KafKa部署总结

    今天要介绍的是消息中间件KafKa,应该说是一个很牛的中间件吧,背靠Apache 与很多有名的中间件搭配起来用效果更好哦 ,为什么不用RabbitMQ,因为公司需要它. 网上已经有很多怎么用和用到哪的 ...

  7. 老李推荐:第8章2节《MonkeyRunner源码剖析》MonkeyRunner启动运行过程-解析处理命令行参数 2

    我们这一节会先去分析下monkeyrunner是如何对参数进行处理的,我们跳转到MonkeyRunnerOptions这个类里面的processOptions这个方法: 93   public sta ...

  8. CSS基础布局--居中对齐,左侧定宽右侧自适应

    CSS页面布局是web前端开发的最基本的技能,本文将介绍一些常见的布局方法,涉及到盒子布局,column布局,flex布局等内容.本文中,你可以看到一些水平垂直居中的方法,左侧固定宽度,右侧自适应的一 ...

  9. JAVA 发送邮件代码---发送文本内容: 内容使用\n 进行换行

    依赖包:mail.jar JAR链接地址: http://pan.baidu.com/s/1o8LNl0Y 密码: ja52 package mail; import java.util.Proper ...

  10. Android™ 1.5 android.R.drawable Icon Resources

    图标一览表: http://www.darshancomputing.com/android/1.5-drawables.html 官  方  API: http://developer.androi ...