ZOJ - 3961 Let's Chat(区间相交)
题意:给定一个长度为n的序列,A和B两人分别给定一些按递增顺序排列的区间,区间个数分别为x和y,问被A和B同时给定的区间中长度为m的子区间个数。
分析:
1、1 ≤ n ≤ 109,而1 ≤x, y ≤ 100,显然应该枚举区间。
2、具体操作为:
(1)id1和id2分别指向A和B的第一个区间,若两区间相交,则求相交区间中长度为m的子区间个数。
(2)若A指向的区间右边界<=B指向的区间右边界,则id1++,将A的下一个区间与B的该区间比较,依此类推。
3、求两区间的相交区间:分别取两区间左边界的最大值和右边界的最小值即可。
#include<cstdio> #include<cstring> #include<cstdlib> #include<cctype> #include<cmath> #include<iostream> #include<sstream> #include<iterator> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<stack> #include<deque> #include<queue> #include<list> #define lowbit(x) (x & (-x)) const double eps = 1e-8; inline int dcmp(double a, double b){ if(fabs(a - b) < eps) return 0; return a > b ? 1 : -1; } typedef long long LL; typedef unsigned long long ULL; const int INT_INF = 0x3f3f3f3f; const int INT_M_INF = 0x7f7f7f7f; const LL LL_INF = 0x3f3f3f3f3f3f3f3f; const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f; const int dr[] = {0, -1, 0, 1, 0, -1, -1, 1, 1}; const int dc[] = {0, 0, 1, 0, -1, -1, 1, -1, 1}; const int MOD = 1e9 + 7; const double pi = acos(-1.0); const int MAXN = 100 + 10; const int MAXT = 10000 + 10; using namespace std; struct Node{ int l, r; void read(){ scanf("%d%d", &l, &r); } }a[MAXN], b[MAXN]; int main(){ int T; scanf("%d", &T); while(T--){ int n, m, x, y; scanf("%d%d%d%d", &n, &m, &x, &y); for(int i = 0; i < x; ++i){ a[i].read(); } for(int i = 0; i < y; ++i){ b[i].read(); } int id1 = 0, id2 = 0, ans = 0; while(id1 < x && id2 < y){ int ll = max(a[id1].l, b[id2].l); int rr = min(a[id1].r, b[id2].r); if(rr - ll + 1 >= m){ ans += rr - ll - m + 2; } if(a[id1].r <= b[id2].r){ ++id1; } else{ ++id2; } } printf("%d\n", ans); } return 0; }