有时间可以记忆大佬们的模板
算法1
区间合并
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int ,int > PII;
const int N = 1e6+10;
PII seg[N];
int n;
int main ()
{
cin >> n;
for(int i=0 ; i<n ; i++) cin >> seg[i].first >> seg[i].second;
sort(seg , seg+n);
int ans1=0 ,ans2=0;
int st = seg[0].first , ed= seg[0].second;
for(int i = 1 ; i < n ; i++)
{
if(seg[i].first <= ed) ed = max(ed,seg[i].second);
else{
ans1 = max(ans1,ed -st );
ans2 = max(ans2,seg[i].first-ed);
st = seg[i].first , ed = seg[i].second;
}
}
ans1 = max(ans1,ed-st);
cout << ans1 << " " << ans2 << endl;
return 0;
}