分析:将各自时间段分多钟情况匹配即可
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct Time
{
int st;
int ed;
};
bool sort1(Time x, Time y)
{
return x.st<y.st;
}
int main()
{
int n;
Time TH[5000];
Time TW[5000];
cin>>n;
for(int i = 0; i < n; i++)cin>>TH[i].st>>TH[i].ed;
for(int i = 0; i < n; i++)cin>>TW[i].st>>TW[i].ed;
sort(TH,TH+n,sort1);
sort(TW,TW+n,sort1);
///以H为基础审核W时间段
int indexh = 0;
int indexw = 0;
int ans = 0;
while(indexh<n&&indexw<n)
{
/// [W] [H]
if(TH[indexh].st>=TW[indexw].ed)
{
indexw++;
continue;
}
/// [H] [W]
if(TH[indexh].ed<=TW[indexw].st)
{
indexh++;
continue;
}
/// [H [W] H]
if(TH[indexh].st<=TW[indexw].st&&TH[indexh].ed>=TW[indexw].ed)
{
ans+=(TW[indexw].ed-TW[indexw].st);
indexw++;
continue;
}
///[W [H] W]
if(TH[indexh].st>=TW[indexw].st&&TH[indexh].ed<=TW[indexw].ed)
{
ans+=(TH[indexh].ed-TH[indexh].st);
indexh++;
continue;
}
///[W [交集] H]
if(TH[indexh].st<=TW[indexw].ed&&TH[indexh].st>=TW[indexw].st)
{
ans+=(TW[indexw].ed-TH[indexh].st);
indexw++;
continue;
}
///[H [交集] W]
if(TH[indexh].ed>=TW[indexw].st&&TH[indexh].st<=TW[indexw].st)
{
ans+=(TH[indexh].ed-TW[indexw].st);
indexh++;
continue;
}
}
cout<<ans<<endl;
return 0;
}
/*
4
1 3
5 6
9 13
14 15
2 4
5 7
10 11
13 14
3
*/