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)上的数全部 ...
随机推荐
- jsp基础知识(基本的语法及原理)
jsp 语法分为三种不同的类型: (1) 编译器指令: 类如: <%@ page import="java.io.*"%> (2) 脚本语法: 指定的是java代码: ...
- hdu----(4308)Saving Princess claire_(搜索)
Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/ ...
- 5. Longest Palindromic Substring -- 最长回文字串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- DOM中元素节点、属性节点、文本节点的理解
DOM中元素节点.属性节点.文本节点的理解 节点信息 每个节点都拥有包含着关于节点某些信息的属性.这些属性是:nodeName(节点名称) nodeValue(节点值) nodeType(节点类型) ...
- 转载:Clear Float
众所周知,平时在写HTML代码时,难免少不了使用Float样式,这样一来,假使您没有清除浮动,那么有浮动元素的父元素容器将元素将无法自动撑 开.换句简单好理解的话来说,假如你在写CODE时,其中div ...
- Java--常用类summary(二)
/* 1:StringBuffer(掌握) (1)用字符串做拼接,比较耗时并且也耗内存,而这种拼接操作又是比较常见的,为了解决这个问题,Java就提供了 一个字符串缓冲区类.StringBuffer供 ...
- C语言内存分配机制
内存分配方式有三种: (1)从静态存储区域分配.内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量,static变量. (2)在栈上创建.在执行函数时,函数内局部变量的 ...
- lucene底层数据结构——底层filter bitset原理,时间序列数据压缩将同一时间数据压缩为一行
如何联合索引查询? 所以给定查询过滤条件 age=18 的过程就是先从term index找到18在term dictionary的大概位置,然后再从term dictionary里精确地找到18这个 ...
- ORACLE 日期比较
oracle sql日期比较:在今天之前: select * from up_date where update < to_date('2007-09-07 00:00:00','yyyy-mm ...
- 学习记录014-ssh批量分发
一.ssh服务介绍 1.ssh安全的加密协议用于远程连接服务器 2.默认端口是22,安全协议版本ssh2,它能同时支持RSA和DSA秘钥,SSH1只支持RSA 3.服务端主要包含两个服务功能ssh远程 ...