前后缀和积

昨天打cf的时候遇到B题,一眼看过去感觉复杂度太高就去做C题了,后来发现做出来的人好多啊,以为是自己姿势不对,看了半天没看出来……后来发现是前缀数组和,cf上有人说用Merge Sort Tree也能做出来,网上搜没找到什么有价值的关于这种算法的解释,等放假了看看B树吧,似乎跟B树有关系
前后缀数组的和积定义如下:
假设有一个数组a[]
前缀和:新建一个数组b[],b[0]=a[0];for(int i=1;i < n;i++)b[i]=b[i-1]+a[i];则数组b[i]表示的是a[0]+a[1]+…a[i];

前缀积:新建一个数组b[],b[0]=a[0];for(int i=1;i < n;i++) b[i]=b[i-1] * a[i];则数组b[i]表示的是a[0]a[1] a[2] * ….a[i];

后缀和:新建一个数组b[],b[n-1]=a[n-1];for(int i=n-2;i>=0;i–) b[i]=b[i+1]+a[i];则数组b[i]表示的就是a[i]+a[i+1]+….a[n-1];

后缀积:新建一个数组b[],b[n-1]=a[n-1];for(int i=n-2;i>=0;i–) b[i]=b[i+1] * a[i];则数组b[i]=a[i] * a[i+1] * …*a[n-1];

看起来很正常啊,就是一个数组遍历一遍啊,没什么算法性可言啊……我也是这么想的…..看来还是应该多接触新题,很久以前就听过前缀后缀的东西,前几天看树状数组也没什么感悟,现在刷题的时候才看出来,下面附上昨天晚上的B题:

B. Karen and Coffee

To stay woke and attentive during classes, Karen needs some coffee!
这里写图片描述

Karen, a coffee aficionado, wants to know the optimal temperature for brewing the perfect cup of coffee. Indeed, she has spent some time reading several recipe books, including the universally acclaimed “The Art of the Covfefe”.

She knows n coffee recipes. The i-th recipe suggests that coffee should be brewed between li and ri degrees, inclusive, to achieve the optimal taste.

Karen thinks that a temperature is admissible if at least k recipes recommend it.

Karen has a rather fickle mind, and so she asks q questions. In each question, given that she only wants to prepare coffee with a temperature between a and b, inclusive, can you tell her how many admissible integer temperatures fall within the range?

Input
The first line of input contains three integers, n, k (1 ≤ k ≤ n ≤ 200000), and q (1 ≤ q ≤ 200000), the number of recipes, the minimum number of recipes a certain temperature must be recommended by to be admissible, and the number of questions Karen has, respectively.

The next n lines describe the recipes. Specifically, the i-th line among these contains two integers li and ri (1 ≤ li ≤ ri ≤ 200000), describing that the i-th recipe suggests that the coffee be brewed between li and ri degrees, inclusive.

The next q lines describe the questions. Each of these lines contains a and b, (1 ≤ a ≤ b ≤ 200000), describing that she wants to know the number of admissible integer temperatures between a and b degrees, inclusive.

Output
For each question, output a single integer on a line by itself, the number of admissible integer temperatures between a and b degrees, inclusive.

Examples
input
3 2 4
91 94
92 97
97 99
92 94
93 97
95 96
90 100
output
3
3
0
4

input
2 1 1
1 1
200000 200000
90 100
output
0

Note
In the first test case, Karen knows 3 recipes.

The first one recommends brewing the coffee between 91 and 94 degrees, inclusive.
The second one recommends brewing the coffee between 92 and 97 degrees, inclusive.
The third one recommends brewing the coffee between 97 and 99 degrees, inclusive.
A temperature is admissible if at least 2 recipes recommend it.

She asks 4 questions.

In her first question, she wants to know the number of admissible integer temperatures between 92 and 94 degrees, inclusive. There are 3: 92, 93 and 94 degrees are all admissible.

In her second question, she wants to know the number of admissible integer temperatures between 93 and 97 degrees, inclusive. There are 3: 93, 94 and 97 degrees are all admissible.

In her third question, she wants to know the number of admissible integer temperatures between 95 and 96 degrees, inclusive. There are none.

In her final question, she wants to know the number of admissible integer temperatures between 90 and 100 degrees, inclusive. There are 4: 92, 93, 94 and 97 degrees are all admissible.

In the second test case, Karen knows 2 recipes.

The first one, “wikiHow to make Cold Brew Coffee”, recommends brewing the coffee at exactly 1 degree.
The second one, “What good is coffee that isn’t brewed at at least 36.3306 times the temperature of the surface of the sun?”, recommends brewing the coffee at exactly 200000 degrees.
A temperature is admissible if at least 1 recipe recommends it.

In her first and only question, she wants to know the number of admissible integer temperatures that are actually reasonable. There are none.

我们可以将每本食谱的l和r分别存进数组p中for(int i=0;i< n;i++) cin>>a>>b,p[a]++,p[b+1]- -;
p[a]++表示有温度a加1,然后将温度b+1减1,这样相对于整个数组,a到b的整个数组相当于+1,然后我们计算数组p的前缀和c,for(int i=1;i<200005;i++) p[i]+=p[i-1],c[i]=c[i-1]+(p[i]>=k);
只有当数组中的数大于等于k才会被记入,然后求两个区间的数就好求了
附上AC代码:

#include<bits/stdc++.h>
using namespace std;
int p[200005],c[200005];
int main()
{
    int n,k,q;
    int a,b;
    cin>>n>>k>>q;
    for(int i=0;i<n;i++) cin>>a>>b,p[a]++,p[b+1]--;
    for(int i=1;i<200005;i++) p[i]+=p[i-1],c[i]=c[i-1]+(p[i]>=k);
    for(int i=0;i<q;i++) {cin>>a>>b;cout<<c[b]-c[a-1]<<endl;}
    return 0;
}

前缀和看起来好有用啊,还是多找这类题刷刷吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值