Loading [MathJax]/jax/input/TeX/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >一道有趣的树状数组题

一道有趣的树状数组题

作者头像
ACM算法日常
发布于 2020-02-16 12:01:22
发布于 2020-02-16 12:01:22
49600
代码可运行
举报
文章被收录于专栏:ACM算法日常ACM算法日常
运行总次数:0
代码可运行

有趣的树状数组题目

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
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
4
3 1
2 5
2 6
4 3
  • Sample Output
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
57

题意:给一些牛,每头牛有位置和音量两个变量。两头牛之间通讯的花费为max(ai,aj)*dis。两牛的音量最高值 * 两牛距离,求所有牛之间的交流的花费。

暴力的话就是O(n^2)。题目数据这样子搞肯定超时。那么我们可以想想数据结构来优化。可以想到,我们对每对牛进行处理的时候,我们优先考虑的是音量大的那头牛。那么我们从音量小的牛开始算起。先按音量排个序。

拿第i头牛,计算的时候,我们要考虑到在他前面的比它音量小的牛和在它后面的比它音量小的牛。就有vol(i)*(sumfront+sumlast)。前面的牛的距离总和sumfront为当前牛的位置 * 在前面的牛的个数(音量比当前牛小)减去到当前牛累计的位置之和。sumlast的计算很巧妙,用已经遍历过的前缀和(音量比当前牛小,代码中用total表示)减去当前牛前面的所有牛的位置之和再减去当前位置 * 右边的牛的个数(这里包括它本身)`。

代码中sumfront和sumlast分别用sum1和sum2表示。注意的是结果比较大要用longlong表示。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=20000+100;
struct node
{
	int v,pos;
	bool operator<(const node &a)const {
		return v<a.v;
	}
}no[maxn];
int a[2][maxn];
int n;
void add(int i,int x,int val)
{
	while(x<maxn)
	{
		a[i][x]+=val;
		x+=x&(-x);
	}
}
int sum(int i,int x)
{
	int ans=0;
	while(x>0)
	{
		ans+=a[i][x];
		x-=x&(-x);
	}
	return ans;
}//以上是树状数组模板~
int main()
{
	cin>>n;
	memset(a,0,sizeof a);
	for(int i=0;i<n;i++)
	{
		scanf("%d%d",&no[i].v,&no[i].pos);
	}
	sort(no,no+n);
	ll ans=0;
	int total=0;
	for(int i=0;i<n;i++)
	{
		int x=no[i].pos;
		total+=x;
		add(0,x,1);
		add(1,x,x);
		int num1=sum(0,x),num2=sum(1,x);//计算frontsum和lastsum
		int t1=num1*x-num2;//计算音量小于当前且位置小于当前的位置总和
		int t2=total-num2-x*(i+1-num1);//计算音量小于当前且位置大于当前的位置总和
		ans+=(long long)(t1+t2)*no[i].v;
	}
	cout<<ans<<endl;
}

温馨提示

如果你喜欢本文,请分享到朋友圈,想要获得更多信息,请关注ACM算法日常点赞的时候,请宠溺一点

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-01-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 ACM算法日常 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
hdu------(4302)Holedox Eating(树状数组+二分)
Holedox Eating Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3362    Accepted Submission(s): 1145 Problem Description Holedox is a small animal which can be considered as one point. It lives in
Gxjun
2018/03/26
6260
洛谷P2345 奶牛集会
题目背景 MooFest, 2004 Open 题目描述 约翰的N 头奶牛每年都会参加“哞哞大会”。哞哞大会是奶牛界的盛事。集会上的活动很 多,比如堆干草,跨栅栏,摸牛仔的屁股等等。它们参加活动时会聚在一起,第i 头奶牛的坐标为Xi,没有两头奶牛的坐标是相同的。奶牛们的叫声很大,第i 头和第j 头奶牛交流,会发出max{Vi; Vj}×|Xi − Xj | 的音量,其中Vi 和Vj 分别是第i 头和第j 头奶牛的听力。假设每对奶牛之间同时都在说话,请计算所有奶牛产生的音量之和是多少。 输入输出格式 输入格式
attack
2018/04/11
7820
洛谷P2345 奶牛集会
HDUOJ-----1541 Stars
Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3680    Accepted Submission(s): 1449 Problem Description Astronomers often examine star maps where stars are represented by points on a plane
Gxjun
2018/03/21
6720
HDUOJ-----1541   Stars
poj 2182 Lost Cows(树状数组)
N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood 'watering hole' and drank a few too many beers before dinner. When it was time to line up for their evening meal, they did not line up in the required ascending numerical order of their brands.
xindoo
2021/01/22
5240
hdu 2838 Cow Sorting(树状数组)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2239 Accepted Submission(s): 711
全栈程序员站长
2022/07/07
2400
HDUOJ-----2838Cow Sorting(组合树状数组)
Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2163    Accepted Submission(s): 671 Problem Description Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening. Ea
Gxjun
2018/03/22
6850
洛谷P2891 [USACO07OPEN]吃饭Dining
题目描述 Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, and she will consume no others. Farmer John has cooked fabulous meals for his cows, but he forgot to check his menu against their preferences. Although he might not
attack
2018/04/11
7890
洛谷P2891 [USACO07OPEN]吃饭Dining
一道带有一点思维的树状数组题目
A magician has a stack of n cards labeled 1 through n, in random order. Her trick involves discarding all of the cards in numerical order (first the card labeled 1, then the card labeled 2, etc.). Unfortunately, she can only discard the card on the top of her stack and the only way she can change the card on the top of her stack is by moving the bottom card on the stack to the top, or moving the top card on the stack to the bottom. The cost of moving any card from the top to the bottom or vice versa is simply the value of the label on the card. There is no cost to discard the top card of the stack. Help the magician calculate the minimum cost for completing her trick.
ACM算法日常
2020/04/22
5620
POJ-3275:奶牛排序Ranking the Cows(Floyd、bitset)
Each of Farmer John's N cows (1 ≤ N ≤ 1,000) produces milk at a different positive rate, and FJ would like to order his cows according to these rates from the fastest milk producer to the slowest.
ACM算法日常
2018/08/07
7110
POJ3622 Gourmet Grazers(FHQ Treap)
Description Like so many others, the cows have developed very haughty tastes and will no longer graze on just any grass. Instead, Farmer John must purchase gourmet organic grass at the Green Grass Grocers store for each of his N (1 ≤ N ≤ 100,000) cows. Ea
attack
2018/04/11
6380
poj 1990 MooFest 树状数组
题意就是有N头牛,每头牛都有一个坐标和声调值(x, v),两头牛之间通讯要花费的能量是他们的距离乘以最大的一个音调值,现在要任意两头牛之间都相互通讯一次,求总共需要花费多少能量?
xindoo
2021/01/21
2750
T4701 【卜卜】树状数组模板
题目描述 在二维平面内给定n个点: 0 x y v表示给(x,y)的权值减去v 1 x y v表示给(x,y)的权值加上v 然后有m个操作 0 x y v , 1 x y v 意义如上 2 a b c d表示询问左上角为(a,b) , 右下角为(c,d)的矩阵权值和 输入输出格式 输入格式: 第一行一个数n 后n行 每行三个数 type x y 意义见上 然后一个数m表示操作数 后m行 第一个数为type 若type=2 则接四个数 a,b,c,d 意义见上 否则接三个数 x,y,v 意义见上 输出格式:
attack
2018/04/12
6360
详解树状数组(C/C++)
树状数组(Binary Indexed Tree,简称BIT或Fenwick Tree)是一种用于高效处理数据序列的算法数据结构。它能够支持两个主要操作:单点更新和区间求和,这两个操作的时间复杂度都能达到O(log n),其中 n 是数据序列的长度。树状数组非常适合处理那些需要频繁更新和查询区间和的问题。
摆烂小白敲代码
2024/09/23
2420
详解树状数组(C/C++)
洛谷P2866 [USACO06NOV]糟糕的一天Bad Hair Day(单调栈)
题目描述 Some of Farmer John's N cows (1 ≤ N ≤ 80,000) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows' heads. Each cow i has a specified heigh
attack
2018/05/30
5260
1657: [Usaco2006 Mar]Mooo 奶牛的歌声
1657: [Usaco2006 Mar]Mooo 奶牛的歌声 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 526  Solved: 365 [Submit][Status] Description Farmer John's N (1 <= N <= 50,000) cows are standing in a very straight row and mooing. Each cow has a unique height h in the rang
HansBug
2018/04/10
4630
P2880 [USACO07JAN]平衡的阵容Balanced Lineup
题目背景 题目描述: 每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q <= 180,000) 个可能的牛的选择和所有牛的身高 (1 <= 身高 <= 1,000,000). 他想知道每一组里面最高和最低的牛的身高差别. 输入: 第1行:N,Q 第2到N+1行:每头牛的身高 第N+2到N+Q
attack
2018/04/11
6240
10:Challenge 3(树状数组直接修改)
总时间限制: 10000ms单个测试点时间限制: 1000ms内存限制: 262144kB描述 给一个长为N的数列,有M次操作,每次操作是以下两种之一: (1)修改数列中的一个数 (2)求数列中某连续一段的和 输入第一行两个正整数N和M。 第二行N个整数表示这个数列。 接下来M行,每行开头是一个字符,若该字符为'M',则表示一个修改操作,接下来两个整数x和y,表示把x位置的值修改为y;若该字符为'Q',则表示一个询问操作,接下来两个整数x和y,表示求[x,y]这段区间的和。输出对每一个询问操作单独输出一行,
attack
2018/04/12
1.2K0
POJ3250「Bad Hair Day」
Some of Farmer John’s cows ( ) are having a bad hair day! Since each cow is self-conscious about her messy hairstyle, FJ wants to count the number of other cows that can see the top of other cows’ heads.
hotarugali
2022/03/01
3450
HDU4031(树状数组)详解
#include<cstdio> //类是差分的思想区间更新 #include<cstring> //思路:用每个点总的攻击次数-无效次数 #include<algorithm> using namespace std; int n,q,t; int s[20005]; int l[20005],r[20005],last[20005],useless[20005]; int lowbit(int x){ return x&(-x); } void insert(int i,int
知识浅谈
2020/03/25
2750
poj-1989 The Cow Lineup
The Cow Lineup Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5587 Accepted: 3311 Description Farmer John’s N cows (1 <= N <= 100,000) are lined up in a row.Each cow is labeled with a number in the range 1…K (1 <= K <=10,000) ide
ShenduCC
2018/04/25
6120
相关推荐
hdu------(4302)Holedox Eating(树状数组+二分)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验