MooFest_二维树状数组
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
【题意】有n头牛,排列成一条直线,给出每头牛在直线上的坐标d。每头牛有一个v,如果牛i和牛j想要沟通的话,它们必须用max(v[i],v[j]),消耗的能量为:max(v[i],v[j]) * 它们之间的距离.
问要使所有的牛之间都能沟通(两两之间),总共需要消耗多少能量。
【思路】现将v从小到大排列,使得每次取到的是当前最大的v。
c[1]记录当前牛的数量c[2]记录当前所有牛的d之和。(二维树状数组)
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int N=;
struct node
{
int d,v;
bool operator <(const node &a)const
//从小到大排序,使得当前获得的v一定是出现过最大的。
{
return v<a.v;
}
}moo[N+];
int c[][N+];
int lowbit(int x)
{
return x&(-x);
}
void update(int i,int d,int v)
{
while(d<=N)
{
c[i][d]+=v;
d+=lowbit(d);
}
}
int get_sum(int i,int d)
{
int res=;
while(d)
{
res+=c[i][d];
d-=lowbit(d);
}
return res;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(c,,sizeof(c));
for(int i=;i<=n;i++)
scanf("%d%d",&moo[i].v,&moo[i].d);
sort(moo+,moo++n);
int sum=;//记录所有坐标之和
long long int ans=;
for(int i=;i<=n;i++)
{
int d=moo[i].d;
sum+=d;
update(,d,);//c[1]记录牛数量
update(,d,d);//c[2]记录牛坐标之和
int n1=get_sum(,d);//在i牛及他前面有多少头
int n2=get_sum(,d);//在i牛及他前面的牛坐标和为多少
int tmp1=n1*d-n2;//i左边的坐标差
int tmp2=sum-n2-d*(i-n1);//i右边的坐标差
ans+=(long long int)(tmp1+tmp2)*moo[i].v;
//不用longlong会溢出
}
printf("%lld\n",ans);
}
return ;
}
MooFest_二维树状数组的更多相关文章
- 二维树状数组 BZOJ 1452 [JSOI2009]Count
题目链接 裸二维树状数组 #include <bits/stdc++.h> const int N = 305; struct BIT_2D { int c[105][N][N], n, ...
- HDU1559 最大子矩阵 (二维树状数组)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559 最大子矩阵 Time Limit: 30000/10000 MS (Java/Others) ...
- POJMatrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22058 Accepted: 8219 Descripti ...
- poj 1195:Mobile phones(二维树状数组,矩阵求和)
Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 14489 Accepted: 6735 De ...
- Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*
D. Iahub and Xors Iahub does not like background stories, so he'll tell you exactly what this prob ...
- POJ 2155 Matrix(二维树状数组+区间更新单点求和)
题意:给你一个n*n的全0矩阵,每次有两个操作: C x1 y1 x2 y2:将(x1,y1)到(x2,y2)的矩阵全部值求反 Q x y:求出(x,y)位置的值 树状数组标准是求单点更新区间求和,但 ...
- [poj2155]Matrix(二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25004 Accepted: 9261 Descripti ...
- POJ 2155 Matrix (二维树状数组)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 17224 Accepted: 6460 Descripti ...
- [POJ2155]Matrix(二维树状数组)
题目:http://poj.org/problem?id=2155 中文题意: 给你一个初始全部为0的n*n矩阵,有如下操作 1.C x1 y1 x2 y2 把矩形(x1,y1,x2,y2)上的数全部 ...
随机推荐
- hdu 1561 The more, The Better (树上背包)
The more, The Better Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- for循环的嵌套——7月24日
练习一:输入一个正整数,用for循环嵌套求阶乘的和 //输入一个正整数,求1!+2!+....+n! 用for循环嵌套 Console.Write("请输入一个正整数:"); ...
- MVP Community Camp 社区大课堂
MVP Community Camp 社区大课堂 微软技术社区大课堂开课啦!!!#MVPComCamp# 全中国微软最有价值专家MVP 在 3月21日周五全天齐聚北京国际会议中心为您呈 ...
- 源代码解读Cas实现单点登出(single sign out)功能实现原理
关于Cas实现单点登入(single sing on)功能的文章在网上介绍的比较多,想必大家多多少少都已经有所了解,在此就不再做具体介绍.如果不清楚的,那只能等我把single sign on这块整理 ...
- FZU 2092 收集水晶 bfs+记忆化搜索 or 暴力
题目链接:收集水晶 一眼看过去,觉得是普通的bfs,初始位置有两个.仔细想了想...好像如果这样的话..........[不知道怎么说...T_T] dp[12][12][12][12][210] 中 ...
- java面向对象编程—— 第三章 程序流程控制
3.1流程控制 三种基本技术可以改变程序的控制流程: ① 调用方法:调用方法将导致控制流程离开当前方法,转移到被调用的方法: ② 选择:java中有两种做出选择的机制:if/else语句和sw ...
- [Js]缓冲运动
一.运动框架 1.在开始运动时,关闭已有定时器(否则会不断有新的定时器执行) 2.把运动和停止隔开(if/else) 二.缓冲运动 逐渐变慢,最后停止(距离越远速度越大) 速度=(目标值-当前值)/缩 ...
- iOS 消息推送原理及实现Demo
一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图1-1: 1.Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Pr ...
- Json数据,日期的转换
using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE)) { con.Open(); using (SQLi ...
- 让webapi只接受ajax请求
为了测试先做一个简单的webapi,直接用新建项目时默认的就可以了. 在浏览器中测试request get,得到结果 然后再项目中新建一个AjaxOnly的类 AjaxOnly继承Acti ...