Karen and Coffee (prefixes and, large queries)

Source: Internet
Author: User
Tags time limit

B. Karen and Coffee time limit/test 2.5 seconds memory limit per test megabytes input standard input output Standa RD output

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 is brewed between, Li and RI degrees, inclusive, to achieve the optimal taste.

Karen thinks that's 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 so she wants to prepare coffee with a temperature between A and B, inclusive, can your tell Her how many admissible integer temperatures fall within the range? Input

The "a" 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 is recommended by to is admissible, and the number of questions Karen has, respectively.

The next n lines describe the recipes. Specifically, the i-th line among this contains two integers li and ri (1≤li≤ri≤200000), describing that i-th R Ecipe suggests that the coffee is 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 which she wants to know the number of admissible int Eger temperatures between A and B degrees, inclusive. Output

For each question, output A is single in a line by itself, the number of admissible integer temperatures between a d b degrees, inclusive. Examples Input

3 2 4----
90 100
Output
3
3
0
4
Input
2 1 1
1 1
200000 200000
90 100
Output 0
Train of thought: 1. Create an array of queries, 3 for loops implement a prefix array, so that s[y]-s[x] represents the number of X to Y satisfied.
       2. When reading data, left endpoint L S[L] + + Add the result from L + 1, right endpoint R, s[r+1]--;
       3. After the first cycle, S[i] represents the number of times I have been included, and the second loop S[i] Indicates whether I satisfies the condition, and the third loop s[i] after the function in 1.
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <algorithm >
#include <string>
#include <vector>
#include <iomanip>
#include <map >
#include <set>
#include <bitset>
#include <queue>
using namespace std;

int s[1000050]; Record status

int  main ()
{
int  n,k,q,i,x,y;
     
     
     scanf ("%d%d%d", &n,&k,&q);
     for (i=1;i<=n;i++)
     {
         scanf ("%d%d", &x,&y);
         s[x]++;
         s[y+1]--;
     }
     for (i=1;i<=200000;i++) s[i]+=s[i-1];//overlap
     for (i=1;i<=200000;i++) s[i]= (s[i]>=k);//satisfied with 1
     for (i=1;i<=200000;i++) s[i]+=s[i-1];//prefix and array
     for (i=1;i<=q;i++)
     {
         scanf ("%d%d", &x,&y );
         printf ("%d\n", S[y]-s[x-1]);
     
     return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.