AcWing 422. 校门外的树
原题链接
简单
作者:
LingYunX
,
2021-02-23 13:25:58
,
所有人可见
,
阅读 217
算法1
C++ 代码
// 暴力 o(ml) or 区间合并 o(nlogn)
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N = 110;
int n, m;
struct Segment{
int l, r;
bool operator< (const Segment &t) const
{
return l < t.l;
}
}seg[N];
int main(){
cin >> n >> m;
for (int i = 0; i < m; i ++ ){
cin >> seg[i].l >> seg[i].r;
}
sort(seg, seg + m);
int L = seg[0].l, R = seg[0].r;
int sum = 0;
for (int i = 1; i < m; i ++ ){
if (seg[i].l <= R){
R = max(R, seg[i].r);
}
else{
sum += (R - L + 1);
L = seg[i].l, R = seg[i].r;
}
}
sum += (R - L + 1);
cout << n + 1 - sum << endl;
return 0;
}