D - AtCoder Express 2(dp,类似区间求线段个数)

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 Li to City Ri (it is possible that Li=Ri). Takahashi the king is interested in the following Q matters:

The number of the trains that runs strictly within the section from City pi to City qi, that is, the number of trains j such that pi≤Lj and Rj≤qi.
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≤Li≤Ri≤N (1≤i≤M)
1≤pi≤qi≤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 pi to City qi.

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个【l,r】线段,q个询问【l,r】中有几条线段。
题解:dp

#include <bits/stdc++.h>

using namespace std;

int a[510][510],dp[510][510];
int main()
{
    int n,m,q;
    while(scanf("%d%d%d",&n,&m,&q)==3){
        memset(a,0,sizeof(a));
        memset(dp,0,sizeof(dp));
        int l,r;
        for(int i=0;i<m;i++){
            scanf("%d%d",&l,&r);
            a[l][r]++;
        }
        for(int i=n;i>0;i--){
            for(int j=1;j<=n;j++){
                dp[i][j]=a[i][j]+dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1];
            }
        }
        while(q--){
            scanf("%d%d",&l,&r);
            printf("%d\n",dp[l][r]);
        }
    }
    return 0;

保持起点不变,终点后的点都加1,[L, R..n]++
sum=[L..R, R]

#include <bits/stdc++.h>

using namespace std;


int main()
{
    int n,m,q;
    while(scanf("%d%d%d",&n,&m,&q)==3){
        int sum[510][510];
        int l,r;
        while(m--){
            scanf("%d%d",&l,&r);
            for(int i=r;i<=n;i++){
                sum[l][i]++;
            }
        }
        while(q--){
            scanf("%d%d",&l,&r);
            int ans=0;
            for(int i=l;i<=r;i++){
                ans+=sum[i][r];
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值