Polynswin @ 2022-10-18 20:10:18
先把每个区间的左右端点离散化,编号<=2m,再暴力填上去,再搞ans
#include<bits/stdc++.h>
#define rint register int
#define mmt(a,b) memset(a,b,sizeof(a))
#define test(x) cout<<#x<<" = "<<x<<endl
#define file(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout)
#define endl '\n'
#define ls i<<1
#define rs i<<1|1
//#define int long long
using namespace std;
const int inf=0x3f3f3f3f,maxn=1e4+10;
int n,m,cnt,ans;
int L[maxn],R[maxn],used[maxn],a[maxn],id[maxn];
vector<int> num;
inline void read(int &x);
inline void ffout(int x);
inline void print(string name,int a[],int r);
signed main()
{
rint i,j;
read(n);read(m);
for(i=1;i<=m;i++)
{
read(L[i]);
read(R[i]);
num.push_back(L[i]);
num.push_back(R[i]);
}
sort(num.begin(),num.end());
for(i=0;i<num.size();i++)
if(!id[num[i]])id[num[i]]=++cnt;
for(i=1;i<=m;i++)
{
L[i]=id[L[i]];
R[i]=id[R[i]];
}
for(i=1;i<=m;i++)
for(j=L[i];j<=R[i];j++)a[j]=i;
for(i=1;i<=cnt;i++)
if(!used[a[i]])
{
used[a[i]]=1;
ans++;
}
ffout(ans);
return 0;
}
inline void read(int &x)
{
int f=1;x=0;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
x=x*f;
}
inline void ffout(int x)
{
if(x<0){x=-x; putchar('-'); }
short sta[63],top=0;
do{ sta[top++]=x%10; x/=10; }while(x);
while(top) putchar(sta[--top]+'0');
}
inline void print(string name,int a[],int r)
{
cout<<name<<":";
for(rint i=1;i<=r;i++)
cout<<a[i]<<" ";
cout<<endl;
}