AtCoder Beginner Contest 106 D题题解
AtCoder Beginner Contest 106 D
题目
Problem Statement
In Takahashi Kingdom, there is a east-west railroad and N cities along it, numbered 1, 2, 3, ..., N from west to east. A company called AtCoder Express possesses M trains, and the train i runs from City L**i to City R**i (it is possible that L**i=R**i). Takahashi the king is interested in the following Q matters:
- The number of the trains that runs strictly within the section from City p**i to City q**i, that is, the number of trains j such that p**i≤L**j and R**j≤q**i.
Although he is genius, this is too much data to process by himself. Find the answer for each of these Q queries to help him.
Constraints
- N is an integer between 1 and 500 (inclusive).
- M is an integer between 1 and 200 000 (inclusive).
- Q is an integer between 1 and 100 000 (inclusive).
- 1≤L**i≤R**i≤N (1≤i≤M)
- 1≤p**i≤q**i≤N (1≤i≤Q)
Input
Input is given from Standard Input in the following format:
N M Q
L1 R1
L2 R2
:
LM RM
p1 q1
p2 q2
:
pQ qQ
Output
Print Q lines. The i-th line should contain the number of the trains that runs strictly within the section from City p**i to City q**i.
Sample Input 1
2 3 1
1 1
1 2
2 2
1 2
Sample Output 1
3
As all the trains runs within the section from City 1 to City 2, the answer to the only query is 3.
Sample Input 2
10 3 2
1 5
2 8
7 10
1 7
3 10
Sample Output 2
1
1
The first query is on the section from City 1 to 7. There is only one train that runs strictly within that section: Train 1. The second query is on the section from City 3 to 10. There is only one train that runs strictly within that section: Train 3.
Sample Input 3
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
7
9
10
6
8
9
6
7
8
10
题意
N表示总区间长度,M表示区间段数,给出每个区间的左端点和右端点。给出Q次询问,询问在该区间长度内嵌套包含的长度有多少个。比如[2,3]区间嵌套在[1,4]区间内。
思路
最开始的时候,思路搞笑了,上来就想数据结构。思路扭转回来之后,发现维护 前缀和 以及 后缀和 数组似乎可以解决这个问题。但是后来发现问题在于,不能单纯维护出所有情况。可以想到,假设对于[4,6],不属于问询情况的有[3,5],[7,9],[3,9]这三类情况。然而就想到容斥原理解决。但是最后发现列的式子中是不定项的。
后来发现正解,其实思路比较明确。观察到实际上M的值并不是很大,类似于dp的想法寻找递推关系,对于一个右端点值,固定后寻找左端点的值,然后循环遍历。通过此种方式更新得到查询结果。
代码
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;
#define REP(i,n) for(int i=0;i<(n);i++)
const int MAXN = 500+10;
int a[MAXN][MAXN];
int b[MAXN][MAXN];
int ans[MAXN][MAXN];
void init(){
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(ans,0,sizeof(ans));
}
int main() {
int n,m,q;
scanf("%d %d %d",&n,&m,&q);
init();
for(int i=0;i<m;i++){
int l,r;
scanf("%d %d",&l,&r);
a[l][r]++;
}
for(int j=1;j<=n;j++){
b[j][j]=a[j][j];
for(int i=j-1;i>=1;i--){
b[i][j]=b[i+1][j]+a[i][j];
}
}
for(int i=1;i<=n;i++){
ans[i][i]=a[i][i];
for(int j=i+1;j<=n;j++){
ans[i][j]=ans[i][j-1]+b[i][j];
}
}
while(q--){
int li,ri;
scanf("%d %d",&li,&ri);
printf("%d\n",ans[li][ri]);
}
return 0;
}

浙公网安备 33010602011771号