题干:

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 Mtrains, 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 piLj and Rjqi.

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≤LiRiN (1≤iM)
  • 1≤piqiN (1≤iQ)

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

解题报告:

这题区间dp乱搞一下就可以了。

AC代码1:(区间dp)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 500 + 5;
int a[MAX][MAX],n,m,q;
ll dp[MAX][MAX];
int main()
{cin>>n>>m>>q;int l,r;while(m--) {scanf("%d%d",&l,&r);a[l][r]++;}for(int len = 1; len<=n; len++) {for(int l = 1; l+len-1<=n; l++) {//每一个起点 int r = l+len-1;if(l == r) dp[l][r]=a[l][r];else if(r-l==1) dp[l][r] = dp[l][l] + dp[r][r] + a[l][r];else dp[l][r] = dp[l][r-1] + dp[l+1][r] - dp[l+1][r-1]+a[l][r];}}while(q--) {ll ans = 0;scanf("%d%d",&l,&r);printf("%lld\n",dp[l][r]);}return 0 ;}

AC代码2:(区间dp)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 500 + 5;
int a[MAX][MAX],n,m,q;
ll dp[MAX][MAX];
int main()
{cin>>n>>m>>q;int l,r;while(m--) {scanf("%d%d",&l,&r);a[l][r]++;}for(int l = n; l>0; l--) {for(int r = 1; r<=n; r++) {dp[l][r] = a[l][r] + dp[l+1][r] + dp[l][r-1] - dp[l+1][r-1];}}while(q--) {ll ans = 0;scanf("%d%d",&l,&r);printf("%lld\n",dp[l][r]);}return 0 ;}

AC代码3:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 500 + 5;
int a[MAX][MAX],n,m,q;
int main()
{cin>>n>>m>>q;int maxx = -1,l,r;while(m--) {scanf("%d%d",&l,&r);maxx = max(maxx,r);a[l][r]++;}for(int i = 1; i<=maxx; i++) {for(int j = 1; j<=maxx; j++) {a[i][j] += a[i][j-1];}}while(q--) {ll ans = 0;scanf("%d%d",&l,&r);for(int i = l; i<=r; i++) {ans += a[i][r];}printf("%lld\n",ans);}return 0 ;}

【AtCoder - 4244 】AtCoder Express 2 (区间dp 或 暴力枚举,思维)相关推荐

  1. Atcoder 4244 AtCoder Express 2 暴力

    文章目录 题意 做法 题意 n 个 点 并 排 , 给 出 m 个 区 间 , q 个 询 问 , 每 次 询 问 m 个 区 间 内 有 多 少 个 区 间 全 部 在 询 问 区 间 内 . n ...

  2. POJ - 2955 Brackets (区间DP)

    题目: 给出一个有括号的字符串,问这个字符串中能匹配的最长的子串的长度. 思路: 区间DP,首先枚举区间长度,然后在每一个长度中通过枚举这个区间的分割点来更新这个区间的最优解.还是做的少. 代码: / ...

  3. CF1399F Yet Another Segments Subset 区间DP

    先来吐槽两句:这篇文章本应该是发在博客园的,但是由于博客园的markdown没用明白,于是就只能继续用CSDN了.这段时间算是以赛代练吧,基本每场能打的CF都去打了,新建的小号分和以前的号差不多了,但 ...

  4. ZROI 2021 10联day8 T1 题(期望+二维区间DP)

    你要在一片菜地里捉兔子. 菜地形如一个一个 N×M 的长方形网格,每个顶点要么是空的,要么有一个兔子洞.在每个洞里有恰好 44 只兔子.在土地的四个角都设置了逮兔陷阱(陷阱在坐标 [0,0],[0,M ...

  5. AtCoder Grand Contest 021 D - Reversed LCS(区间dp)

    D - Reversed LCS 繁凡さん 设 f[l,r,k]f [ l , r , k ]f[l,r,k] 表示区间 [l,r][ l , r ][l,r] 中修改 kkk 次能得到的最长回文子序 ...

  6. 【CodeForces - 608D】Zuma(区间dp)

    题干: Genos recently installed the game Zuma on his phone. In Zuma there exists a line of n gemstones, ...

  7. POJ 2955 Brackets (区间DP)

    题目链接:http://poj.org/problem?id=2955 Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  8. 0x53. 动态规划 - 区间DP(习题详解 × 8)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 文章目录 0x53. 动态规划 - 区间DP Problem A. 最优矩阵链乘 Problem B. ...

  9. UVA1626 括号序列 Brackets sequence(区间DP匹配括号,输出匹配方案)

    整理的算法模板合集: ACM模板 UVA1626 Brackets sequence 我们将正规括号序列定义如下: 空序列是正规括号序列. 如果 SSS 是一个正规括号序列,那么 (S) 和 [S] ...

最新文章

  1. 发现自己竟然有点恐高,郁闷
  2. java 代码块的作用_Java核心(三):代码块的作用
  3. 算法效果AB测试中的PV-UV不对称性
  4. java kafka client_Kafka Java Client基本使用及整合SpringBoot
  5. 如何理解python_如何理解 Python
  6. jzoj2940-生成输入数据【最小生成树,并查集】
  7. 嵌入式电路设计(入门)
  8. 剑指 Offer 58 - I. 翻转单词顺序 (双指针)
  9. python如何在列表中查找元素位置_查找元素在list中的位置以及折半查询
  10. pytorch 入门学习使用逻辑斯蒂做二分类-6
  11. java 圆类 圆锥类_以圆类 Circle 及立体图形类 Solid 为基础设计圆锥类 Cone
  12. idea下载Scala插件(详细)
  13. 2011年河南省国民经济和社会发展统计公报
  14. 黑马程序员-随笔-我与程序员
  15. 使用Python将数据库中的文本生成词云图
  16. Difference between Vienna DL LLS and UL LLS
  17. C# OPCUA 读写结构体
  18. 唯有卡拉特拉瓦,才敢让建筑飞起来!
  19. 最新微信小程序反编译破解过程记录
  20. NILM-利用nilmtk读取iawe数据集电器数据

热门文章

  1. 开始《数据机构与算法之美》之旅
  2. [算法][算法复杂度]常用算法复杂度速查表
  3. [Leedcode][JAVA][第1162题][BFS]
  4. 树状数组的区间修改+查询
  5. CodeForces - 660C Hard Process
  6. python下电影_Python3.6实现根据电影名称(支持电视剧名称),获取下载链接的方法...
  7. 简单说明c语言程序步骤,C语言的入门简介和三个简单的C语言程序详细说明
  8. wince 自带的web server
  9. python词频统计代码_机器学习必备宝典-《统计学习方法》的python代码实现及课件...
  10. linux用户恢复正常,Linux系统用户口令安全恢复方法