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 ...
随机推荐
- IE浏览器bug罪魁祸首--hasLayout
IE浏览器一直都是前端程序员最头疼的,IE的表现与其他浏览器不同的原因之一就是,显示引擎使用一个称为布局(layout)的内部概念. 认识hasLayout--haslayout是Windows In ...
- 《BI那点儿事》数据流转换——OLE DB 命令转换
OLE DB命令对数据流中的数据行执行一个OLE DB命令.它针对数据表中的每一行进行更新操作,可以事先将要更新的数据存放在表中.或者针对一个有输入参数的存储过程,可以将这些参数存放在一个数据表中,不 ...
- Android 签名工具 shell脚本
signApk.sh #!/bin/bash #$1 signed and unaligned apk #$2 unsigned apk #$3 aligned apk #./signApk.sh t ...
- VPN推荐
最近ZF加强了对谷歌的屏蔽,推荐一些VPN azuressh.com ¥10/m http://www.archsocks.com/ ¥12/年 豆荚VPN,免费800M/月,偶尔连不上,可付费
- Vim入门教程
尽管网上有成打的Vim在线教程,但是要么艰深晦涩,要么太过肤浅.本教程的目标让每个阶段都有斩获,从理解它的哲学(将和你终身相伴)到超越现在编辑技巧,成为其中的牛人. 简单来说,本教程的学习方式将使你终 ...
- Java文件操作与输入输出流
文件操作 package ch15; import java.io.*; /** * Created by Jiqing on 2016/12/28. */ public class FileTest ...
- Head First 设计模式 --7 适配器模式 外观模式
适配器模式:将一个类东街口转换成客户期望的另一个接口.适配器让原本接口不兼容的类可以合作无间. 适配器模式有两种,对象适配器和类的适配器.先看一下对象适配器. 还是看最开始鸭子的例子,如果此时鸭子不够 ...
- laravel 中 与前端的一些事4 之合并压缩静态文件
合并压缩多个静态文件到一个文件里面,可以减少网站的http请求,稍微优化性能,提高网站的用户体验 使用elixir来实现: 敲命令 合并并压缩js和css文件
- windows docker测试
最近测试环节要求比较多,笔记本上虚拟机越来越多,试验一下docker,随笔如下. 一.安装docker 主机windows 10 专业版 网上在windows10上安装docker有两种方法 一个是使 ...
- iOS开发UI篇—iOS开发中三种简单的动画设置
iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView b ...