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. iOS多线程——同步异步串行并行

    串行并行异步同步的概念很容易让人混淆,关于这几个概念我在第一篇GCD中有解释,但是还不够清晰,所以这里重写一篇博客专门对这几个概念进行区分: 先说一下队列和任务: (1)队列分为串行和并行,任务的执行 ...

  2. iOS网络编程笔记——XML文档解析

    今天利用多余时间研究了一下XML文档解析,虽然现在移动端使用的数据格式基本为JSON格式,但是XML格式毕竟多年来一直在各种计算机语言之间使用,是一种老牌的经典的灵活的数据交换格式.所以我认为还是很有 ...

  3. vpn的实现原理

    由于公共IP的短缺,我们在组建局域网时,通常使用保留地址作为内部IP,(比如最常用的C类保留地址:192.168.0.0-192.168.255.255)这些地址是不会被互联网分配的,因此它们在互联网 ...

  4. cuda事件的使用

    cudaEvent_t start,stop; cudaEventCreate(&start);//创建事件 cudaEventCreate(&stop); cudaEventReco ...

  5. 开源 & 在线免费使用:升讯威 周报系统

    这个周报系统大约写于2015年,缘起当时所带的开发团队需要逐步建立或完善一些项目管理方法. 在调研了网上的诸多项目管理或周报/日报管理系统之后,并没有找到符合当时情况的系统,这里最大的问题不是网上既有 ...

  6. unity插件开发——MenuItem

    有unity中的菜单栏是我们经常使用到的地方,如下图: MenuItem的作用就是增加一个自己的菜单 使用方法: 在工程中Assets目录下任意一个Editor目录(以后简称Editor目录,如果不存 ...

  7. 关于JDEV的连接问题

    在JDev中有两个连接数据哭库的地方,双击项目名称,里面的Business Components里面的Connection里面的链接,这个链接是Run页面时候的链接 第二个链接在Oracle Appl ...

  8. rsync+inotify实现文件同步更新(配置)

    linux下为了数据安全或者网站同步镜像,不得不考虑一些实时备份的问题,这篇linux下通过rsync+inotify 实现数据实时备份配置过程记录下来,防止遗忘配置过程记录下来,防止遗忘!如有建议技 ...

  9. 手机自动化测试:appium源码分析之bootstrap五

    手机自动化测试:appium源码分析之bootstrap五   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.poptest测试 ...

  10. MCDownloader(iOS下载器)说明书

    示例 前言 很多iOS应用中都需要下载数据,并对这些下载的过程和结果进行管理,因此我才有了写这个MCDownloader的想法.在IOS 文件下载器-MCDownloadManager这篇文章中,我使 ...