POJ 1990 MooFest(树状数组)
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 7077 | Accepted: 3181 |
Description
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
* 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
Sample Input
4
3 1
2 5
2 6
4 3
Sample Output
57
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 2e4+;
const int M = ;
const int mod=1e9+;
ll tree[][N];
int n,m;
struct man{
int x,v;
bool operator< (const man &it)const{
return v<it.v;
}
}a[N];
void add(int k,int num){
while(k<=){
tree[][k]+=num;
tree[][k]+=;
k+=k&(-k);
}
}
ll Sum(int k,int d){
ll sum=;
while(k>){
sum+=tree[d][k];
k-=k&(-k);
}
return sum;
}
int main() {
scanf("%d",&n);
int v,x;
for(int i=;i<=n;i++){
scanf("%d%d",&a[i].v,&a[i].x);
}
sort(a+,a++n);
ll ans=;
for(int i=;i<=n;i++){
ll sump=Sum(a[i].x,);
ll sumx=Sum(a[i].x,);
ans+=a[i].v*(sump*a[i].x-sumx)+a[i].v*(Sum(,)-sumx-(i--sump)*a[i].x);
add(a[i].x,a[i].x);
}
printf("%lld\n",ans);
return ;
}
POJ 1990 MooFest(树状数组)的更多相关文章
- 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 ...
- poj 2229 Ultra-QuickSort(树状数组求逆序数)
题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- poj 2155 Matrix (树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16797 Accepted: 6312 Descripti ...
- poj 3067 - Japan(树状数组)
先按第一个数从大到小排序,相等的情况下,第二个数按照从大到小排序..... 预处理后,照着树状数组写就行了... 注意:k的最大值应取1000*1000 代码如下: include <cstdi ...
- poj 2481 - Cows(树状数组)
看的人家的思路,没有理解清楚,,, 结果一直改一直交,,wa了4次才交上,,, 注意: 为了使用树状数组,我们要按照e从大到小排序.但s要从小到大.(我开始的时候错在这里了) 代码如下: #inclu ...
- POJ 3468(树状数组的威力)
之前说过这是线段树的裸题,但是当看了http://kenby.iteye.com/blog/962159 这篇题解后我简直震惊了,竟然能如此巧妙地转化为用树状数组来处理,附上部分截图(最好还是进入原网 ...
- POJ 2352 【树状数组】
题意: 给了很多星星的坐标,星星的特征值是不比他自己本身高而且不在它右边的星星数. 给定的输入数据是按照y升序排序的,y相同的情况下按照x排列,x和y都是介于0和32000之间的整数.每个坐标最多有一 ...
- POJ 2182【树状数组】
题意: 每头牛有编号,他们乱序排成一排,每头牛只知道前边比自己序号小的有几位. 思路: 递推,最后一只牛的编号是确定的,然后不断进行区间更新,直到找到某个空位前方恰好有n个空位. 这题跟某道排队的题思 ...
- POJ 2309 BST 树状数组基本操作
Description Consider an infinite full binary search tree (see the figure below), the numbers in the ...
随机推荐
- python成长之路【第五篇】:python字符编码
在2.7环境中我们要写上这一行#-*- coding:utf-8 -*- 为什么我们要加这一行呢?这一样的意思是置顶编码类型为utf-8编码! 首先在看这个问题之前,咱们是否曾想过一个问题? 为什么我 ...
- Request获取URL地址相应方法
以项目为BBS为例,以下代码置于modify.jsp: 1.request.getLocalName(): akiradunn 2.request.getServerName(): localhost ...
- 使用spawn-fcgi管理php-cgi
spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u nginx -g nginx -f php-cgi
- jqury ajax 标准
参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. type String (默认: "GET") 请求方式 ("POST" 或 ...
- 移动端web之像素基础
px:css pixels逻辑像素,浏览器使用的抽象单位 dp,pt :device independent pixels 设备无关像素 dpr:devicePixelRatio 设备像素缩放比 计算 ...
- C#微信公众号接口开发实例-高级接口-申请带参数的二维码
最近公司涉及到微信绑定用户,做了高级接口-申请带参数的二维码,总结了下微信开发接口.微信接口开发都是除了消息用的xml 回复基本上都是用json的形式传递信息(post/get),开发的方法基本都是一 ...
- Masonry使用注意篇
简要 自动布局最重要的是约束:UI元素间关系的数学表达式.约束包括尺寸.由优先级和阈值管理的相对位置.它们是添加剂,可能导致约束冲突 .约束不足造成布局无法确定 .这两种情况都会产生异常. 使用前:A ...
- golang的第一个deadlock
package main import ( "fmt" "math/rand" ) func push(c chan []int) { ...
- Scala深入浅出实战经典---001-Scala开发环境搭建和HelloWorld解析
001-Scala开发环境搭建和HelloWorld解析 Scala 函数式编程和面向对象结合的语言 函数式编程擅长数值计算 面向对象擅长大型工程和项目的组织以及团队的分工协作 Scala会是下一个伟 ...
- ubuntu 13.04 tftp服务器建立
本文参考博文:http://blog.chinaunix.net/uid-20718037-id-3194493.html 用tftp下载就需要要我们的主机上先安装tftp服务器. 1.安装软件 ...