AtCoder Beginner Contest 106 D

D - AtCoder Express 2


Time Limit: 3 sec / Memory Limit: 1000 MB

Score: 400400 points

Problem Statement

In Takahashi Kingdom, there is a east-west railroad and NN cities along it, numbered 11, 22, 33, ..., NN from west to east. A company called AtCoder Expresspossesses MM trains, and the train ii runs from City LiLi to City RiRi (it is possible that Li=RiLi=Ri). Takahashi the king is interested in the following QQ matters:

  • The number of the trains that runs strictly within the section from City pipi to City qiqi, that is, the number of trains jj such that pi≤Ljpi≤Lj and Rj≤qiRj≤qi.

Although he is genius, this is too much data to process by himself. Find the answer for each of these QQ queries to help him.

Constraints

  • NN is an integer between 11 and 500500 (inclusive).
  • MM is an integer between 11 and 200 000200 000 (inclusive).
  • QQ is an integer between 11 and 100 000100 000 (inclusive).
  • 1≤Li≤Ri≤N1≤Li≤Ri≤N (1≤i≤M)(1≤i≤M)
  • 1≤pi≤qi≤N1≤pi≤qi≤N (1≤i≤Q)(1≤i≤Q)

Input

Input is given from Standard Input in the following format:

NN MM QQ
L1L1 R1R1
L2L2 R2R2
::
LMLM RMRM
p1p1 q1q1
p2p2 q2q2
::
pQpQ qQqQ

Output

Print QQ lines. The ii-th line should contain the number of the trains that runs strictly within the section from City pipi to City qiqi.


Sample Input 1 Copy

Copy

2 3 1
1 1
1 2
2 2
1 2

Sample Output 1 Copy

Copy

3

As all the trains runs within the section from City 11 to City 22, the answer to the only query is 33.


Sample Input 2 Copy

Copy

10 3 2
1 5
2 8
7 10
1 7
3 10

Sample Output 2 Copy

Copy

1
1

The first query is on the section from City 11 to 77. There is only one train that runs strictly within that section: Train 11. The second query is on the section from City 33 to 1010. There is only one train that runs strictly within that section: Train 33.


Sample Input 3 Copy

Copy

10 10 10
1 6
2 9
4 5
4 7
4 7
5 8
6 6
6 7
7 9
10 10
1 8
1 9
1 10
2 8
2 9
2 10
3 8
3 9
3 10
1 10

Sample Output 3 Copy

Copy

7
9
10
6
8
9
6
7
8
10

题目大意:给出一个区间1-n有m辆火车,每辆火车有自己的行驶区间,每次询问有一个l,r问l,r内有多少辆火车。

解题思路:把火车看做成二维平面上的点,每次求一下区间[l,l]-[r,r]内点的个数。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
using namespace std;
#define LL long long
#define N 500
#define lowb(x) x&-x

int c[N][N];
map<pair<int,int>,int>mp;
void add(int x,int y)
{
    for(int i=x;i<=N;i+=lowb(i))
    {
        for(int j=y;j<=N;j+=lowb(j))
        {
            c[i][j]+=1;
        }
    }
}

int sum(int x,int y)
{
    int ans=0;
    for(int i=x;i;i-=lowb(i))
    {
        for(int j=y;j;j-=lowb(j))
        {
            ans+=c[i][j];
        }
    }
    return ans;
}
int main()
{
    int n,m,q;
    int l,r;
    cin>>n>>m>>q;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&l,&r);
        add(l,r);
    }
    for(int i=1;i<=q;i++)
    {
        scanf("%d%d",&l,&r);
        printf("%d\n",sum(r,r)-sum(r,l-1)-sum(l-1,r)+sum(l-1,l-1));
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_40894017/article/details/81836371