You are given two interval collections A and B. Collection A has N intervals [ A1, A2 ], [ A3, A4 ], ..., [ A2N-1, A2N ] and collection B has M intervals [ B1, B2 ], [ B3, B4 ], ..., [ B2M-1, B2M ]. Your task is to calculate the length of A - B.
For example if A = {[2, 5], [4, 10], [14, 18]} and B = {[1, 3], [8, 15]}, the length of A - B ({(3, 8), (15, 18]}) is 8.
InputLine 1: Two integers N and M (1 ≤ N, M ≤ 100000).
Line 2: 2*N integers, A1, A2, ..., A2N (1 ≤ Ai ≤ 100000000).
Line 3: 2*M integers, B1, B2, ..., B2M (1 ≤= Bi ≤ 100000000).
OutputThe length of A - B.
3 2 2 5 4 10 14 18 1 3 8 15Sample Output
8
滔滔说是sui题 但是我真的看了题解才会写
感觉他代码写的好巧妙 【捂脸
求一个区间有没有被覆盖就是看左端点数有没有多于右端点数
#include<stdio.h>
#include<string>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<limits>
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
using namespace std;
int n, m;
const int maxn = 100005;
typedef pair<int, pair<int, int> > point;
vector <point> vec;
int cnt[2];
int main()
{
while(scanf("%d%d",&n,&m) != EOF){
for(int i = 0; i < n; i++){
int a, b;
scanf("%d%d",&a,&b);
point p = make_pair(a, make_pair(0, 1));
point q = make_pair(b, make_pair(0, -1));
vec.push_back(p);
vec.push_back(q);
}
for(int i = 0; i < m; i++){
int a, b;
scanf("%d%d",&a, &b);
point p = make_pair(a, make_pair(1, 1));
point q = make_pair(b, make_pair(1, -1));
vec.push_back(p);
vec.push_back(q);
}
sort(vec.begin(), vec.end());
cnt[0] = cnt[1] = 0;
int ans = 0;
for(int i = 0; i < vec.size() - 1; i++){
cnt[vec[i].second.first] += vec[i].second.second;
if(cnt[0] > 0 && cnt[1] == 0){
ans += vec[i + 1].first - vec[i].first;
}
}
printf("%d
",ans);
}
return 0;
}
真的是水题啊我好菜啊