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 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 491 9492 9797 9992 9493 9795 9690 100

Output

3304

Input

2 1 11 1200000 20000090 100

Output

0

Note

In the first test case, Karen knows 3 recipes.

  1. The first one recommends brewing the coffee between 91 and 94 degrees, inclusive.
  2. The second one recommends brewing the coffee between 92 and 97 degrees, inclusive.
  3. 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 94degrees 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 97degrees 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 97degrees are all admissible.

In the second test case, Karen knows 2 recipes.

  1. The first one, "wikiHow to make Cold Brew Coffee", recommends brewing the coffee at exactly 1 degree.
  2. 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.

思路:

先用差分数组处理下每一个温度有几个人能适应

然后扫一边差分数组的前缀和数组,值大于等于k的给数组a赋值为1

然后再求一下数组a的前缀和,

每一个咨询就可以O(1)求求出。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), ‘\0‘, sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n,q,k;
int f[maxn];
int sum[maxn];
int a[maxn];
int asum[maxn];
int l,r;
int main()
{
    gg(n);
    gg(k);
    gg(q);
    repd(i,1,n)
    {
        gg(l),gg(r);
        f[l]++;
        f[r+1]--;
    }
    repd(i,1,maxn-5)
    {
        sum[i]=sum[i-1]+f[i];
    }
    repd(i,1,maxn-5)
    {
        if(sum[i]>=k)
        {
            a[i]++;
        }
        asum[i]=asum[i-1]+a[i];
    }
    repd(i,1,q)
    {
        int ans=0;
        gg(l),gg(r);
        ans=asum[r]-asum[l-1];
        printf("%d\n", ans);
    }
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ‘ ‘ || ch == ‘\n‘);
    if (ch == ‘-‘) {
        *p = -(getchar() - ‘0‘);
        while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
            *p = *p * 10 - ch + ‘0‘;
        }
    }
    else {
        *p = ch - ‘0‘;
        while ((ch = getchar()) >= ‘0‘ && ch <= ‘9‘) {
            *p = *p * 10 + ch - ‘0‘;
        }
    }
}

原文地址:https://www.cnblogs.com/qieqiemin/p/10323125.html

时间: 2024-08-30 01:19:38

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

差分数组与前缀和

前缀和 前缀和顾名思义就是前面i个数的总和. 假设有一个序列A,前缀和为S.根据概念很容易知到公式 \(S[i]=\displaystyle \sum_{j=1}^iA[j]\) 如何求区间\([l,r]\)的和呢? \(sum[l,r]=s[r]-s[l-1]\) 那如果要对多个不同区间 \([l,r]\)进行加减操作呢?然后输出某个区间\([l,r]\)的区间和,接下来就要用到差分数组了 差分数组 设原数组为\(A[i]\),差分数组为\(B[i]\),则: \[B[i]=\begin{ca

差分数组,前缀和

1 /************************************************************************* 2 > File Name: a.cpp 3 > Author: QWX 4 > Mail: 5 > Created Time: 2018/11/11 9:43:08 6 ************************************************************************/ 7 8 9

树状数组区间修改,区间更新:差分数组的运用

树状数组最原始的作用就是求前缀和,可以实现单点修改和区间查询. 但是假设现在有: 1.区间修改,单点查询 2.区间修改,区间查询 但是又不想敲线段树怎么办? 就用树状数组喽. 假设现在有一个原数组a(假设a[0] = 0),有一个数组d,d[i] = a[i] - a[i-1],那么 a[i] = d[1] + d[2] + .... + d[i] d数组就是差分数组 所以求a[i]就可以用树状数组维护d[i]的前缀和 区间修改,单点查询: 根据d的定义,对[l,r]区间加上x,那么a[l]和a

P1083 借教室 差分数组

第一行包含两个正整数n,mn,m,表示天数和订单的数量. 第二行包含nn个正整数,其中第ii个数为r_iri?,表示第ii天可用于租借的教室数量. 接下来有mm行,每行包含三个正整数d_j,s_j,t_jdj?,sj?,tj?,表示租借的数量,租借开始.结束分别在第几天. 每行相邻的两个数之间均用一个空格隔开.天数与订单均用从11开始的整数编号. 输出格式: 如果所有订单均可满足,则输出只有一行,包含一个整数00.否则(订单无法完全满足) 输出两行,第一行输出一个负整数-1−1,第二行输出需要修

数据结构之差分数组

2019-06-25 推荐博客阅读:https://www.sohu.com/a/271430685_100201031 一. 适合解决的问题 有n个数.m次操作,每一次操作,给定l,r,del.将l~r区间的所有数增加del:最后有q个询问,给你 l,r ,每一次询问求出l~r的区间和. 注明: 先进行m个修改操作,后进行查询操作. 涉及到的用途有 快速处理区间加减操作:O(1) 询问区间和:O(n)处理O(1)查询. 二. 算法解释 差分数组定义:记录当前位置的数与上一位置的数的差值.  我

Color the ball(差分数组)

N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗? Input每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N). 当N = 0,输入结束.Output每个测

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 ac

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

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