日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【AtCoder - 4244 】AtCoder Express 2 (区间dp 或 暴力枚举,思维) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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?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

解題報告:

? ? ?這題區間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 或 暴力枚举,思维)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。