思路:本题可简化成已知区间,求区间的重叠长度。已知两个区间[a,b],[c,d],x = min(b,d);y = max(a,c)。if(y-x>0)有重叠区间,区间长度为y-x;else 无重叠区间。类比的可以求矩阵的重叠面积。
#include<iostream>
#include<vector>
#include<map>
using namespace std;
/*
[a,b],[c,d]
x = min(b,d);y = max(a,c)
if(y-x>0)有重叠区间,区间长度为y-x
else 无重叠区间
*/
const int N = 2000;
vector<pair<int,int> > v1(N),v2(N);
int main(){
int n,sum = 0;
cin>>n;
for(int i = 0; i < n; i++){
cin>>v1[i].first>>v1[i].second;
}
for(int i = 0; i < n; i++){
cin>>v2[i].first>>v2[i].second;
}
vector<pair<int,int> > :: iterator it1,it2;
for(it1 = v1.begin(); it1 != v1.end(); it1++){
for(it2 = v2.begin(); it2 != v2.end(); it2++){
int x = max(it1->first, it2->first);
int y = min(it1->second, it2->second);
if(y - x > 0){
sum += (y - x);
}
}
}
cout<<sum;
return 0;
}