CodeForces 816B Karen and Coffee(前缀和,大量查询)

CodeForces 816B Karen and Coffee(前缀和,大量查询)

Description

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, nk (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

题意:

给出n个区间,q个查询区间,问每次查询时,该查询区间内有多少个点至少被k个区间覆盖。

样例1:第一行三个整数的意义是:3个区间 至少被2个区间覆盖  查询四次,第二三四行分别是三个区间。剩下四行为查询的区间,比如92 94就是在92到94中(包含92和94),多少个数被上面区间覆盖了2次(即k次)或者2次以上。在92到94的区间内92 93 94都被覆盖两次,故三个,输出3。

思路:

数据量是2*10^5,暴力的方法肯定会超时。

准备两个一维数组,cnt[] 和 sum[] 来处理每一个满足数 i (范围 [1, 200000]  )

cnt[i] 数组:第 i 个数被区间包含的次数

sum[i] 数组: 前 i 个数被 k 个包含的前缀和。

第一组样例就是

所以对于 k 个提问,sum[b] - sum[a-1] 就是答案了(因为sum[b]是0到b中符合条件的总数,sum[a-1](包含两个端点)是0到a中符合条件的总数,两者相减就是a到b中符合条件的总数)。

这里最巧妙之处就是cnt[i] 要怎么统计出来。如果 [li, ri] 范围的数都遍历一次,绝对会超时, 处理两个点其实就可以统计出来了,分别是 cnt[li], cnt[ri+1]。 对于每一次询问,进行:cnt[li]++, cnt[ri+1]-- 处理。然后 q 个问题之后,再统一遍历多一次,利用前一个数 cnt[i-1] 就能统计出当前数 cnt[i] 了。

代码:

#include<stdio.h>
#include<iostream>
using namespace std;
int a[200010];
int c[200010];
int main()
{
    int k,n,q;
    int b,e;
    cin>>k>>n>>q;
    for(int i=0; i<k; i++)//输入查询
    {
        cin>>b>>e;
        a[b]++;
        a[e+1]--;
    }
    for(int i=1; i<200010; i++)//更新cnt数组
    {
        a[i]+=a[i-1];
        if(a[i]>=n)
            c[i]++;
    }
    for(int i=1; i<200010; ++i)//更新sum数组
        c[i]+=c[i-1];
    for(int i=0; i<q; i++)//输出结果
    {
        int l,r;
        scanf("%d%d",&l,&r);
        cout<<c[r]-c[l-1]<<endl;
    }
}

  

时间: 2024-08-15 14:29:49

CodeForces 816B Karen and Coffee(前缀和,大量查询)的相关文章

CodeForces - 816B 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 u

Karen and Coffee CodeForces - 816B (差分数组+预处理前缀和)

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 u

Codeforces Round #419 (Div. 2)B. Karen and Coffee

B. Karen and Coffee 题意:给定n个区间,当一个数在k个区间以内则这个数可以被选中.有q个询问,问在某个区间能有多少个数可以被选中. 1 #include <iostream> 2 #include <stdio.h> 3 using namespace std; 4 const int MAX_N = 2e5+10; 5 int a[MAX_N], c[MAX_N], n, k, q; 6 int main(){ 7 int l, r; 8 scanf(&quo

#419(div2) B. Karen and Coffee

题意:给出n个温度区间,k,Q个询问,每个询问给出一个温度区间x--y.问这之间有多少个温度在给出K的温度区间内. 思路:前缀和小技巧 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N = 3e5+10; 4 5 int n,k,q,x,y,a[N],b[N]; 6 7 int main() { 8 scanf("%d%d%d",&n,&k,&q); 9 for(int i

codeforces 938F(dp+高维前缀和)

题意: 给一个长度为n的字符串,定义$k=\floor{log_2 n}$ 一共k轮操作,第i次操作要删除当前字符串恰好长度为$2^{i-1}$的子串 问最后剩余的字符串字典序最小是多少? 分析: 首先很容易得到一个性质,那就是删除的那些串是可以不交叉的 很容易想到一个很简单的dp dp[i][j]表示考虑原串的前i位,删除状态为j的情况下字典序最小的字符串(注意dp里面保存的是个字符串) 那么很明显就是个O(n^3logn)的dp,无法通过 dp里是一个字符串这个东西是很浪费时间而且很不优美的

Codeforces 490C Hacking Cypher【前缀模+后缀模+暴力】

C. Hacking Cypher time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Polycarpus participates in a competition for hacking into a new secure messenger. He's almost won. Having carefully studied

CodeForces - 816C Karen and Game(简单模拟)

Problem Description On the way to school, Karen became fixated on the puzzle game on her phone! The game is played as follows. In each level, you have a grid with n rows and m columns. Each cell originally contains the number 0. One move consists of

CF816B Karen and Coffee

思路: 有点类似于区间修改点查询的树状数组. 实现: 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 const int MAXN = 200005; 6 int a[MAXN], n, k, q; 7 8 int main() 9 { 10 cin >> n >> k >> q; 11 for (int i = 0; i < n; i++) 12 {

[Codeforces 816A]Karen and Morning

题目大意:给你一个时间(hh:mm),求最少经过多少分钟才能使这个时间变成回文. 解题思路:模拟,先判断0的情况,然后每过1分钟判断一次即可. C++ Code: #include<cstdio> int main(){ int h,m; scanf("%d:%d",&h,&m); if(h==m%10*10+m/10){ puts("0"); return 0; } for(int i=1;;++i){ ++m; if(m==60){