#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Fire {
int start;
int end;
};
bool compare(const Fire& f1, const Fire& f2) {
return f1.start < f2.start;
}
int main() {
int n;
cin >> n;
vector<Fire> fires(n);
for (int i = 0; i < n; i++) {
cin >> fires[i].start >> fires[i].end;
}
sort(fires.begin(), fires.end(), compare);
int ans = 0;
int curStart = fires[0].start;
int curEnd = fires[0].end;
for (int i = 1; i < n; i++) {
if (fires[i].start <= curEnd) {
curEnd = max(curEnd, fires[i].end);
} else {
ans += curEnd - curStart;
curStart = fires[i].start;
curEnd = fires[i].end;
}
}
ans += curEnd - curStart;
cout << ans << endl;
return 0;
}
P1496火烧赤壁
最新推荐文章于 2025-05-01 21:36:18 发布