AcWing 422. 校门外的树
原题链接
简单
作者:
hai阿卢
,
2021-02-06 15:49:57
,
所有人可见
,
阅读 231
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int N=10010;
typedef pair<int,int> PII;
vector<PII> a,an;
void merge()
{
int l=-2e9,r=-2e9;
for(auto it=a.begin();it<a.end();it++)
{
if(r<(*it).first)
{
if(l!=-2e9) an.push_back({l,r});
l=(*it).first,r=(*it).second;
}
else r=max(r,(*it).second);
}
if(l!=-2e9) an.push_back({l,r});
}
int main()
{
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
a.push_back({x,y});
}
sort(a.begin(),a.end());
merge();
for(auto it=an.begin();it<an.end();it++)
{
// cout<<(*it).first<<" "<<(*it).second<<endl;
n=n-((*it).second+1-(*it).first);
}
cout<<n+1;
return 0;
}