Language: C++

#include <bits/stdc++.h>
#define Maxn 400007
using namespace std;
int read()
{
	int x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int n,m,L[Maxn],R[Maxn],ans=0;
int last[Maxn],next[Maxn];
bool vis[Maxn];
pair<int,int> pos[Maxn];
void del(int x)
{
	next[last[x]]=next[x];
	last[next[x]]=last[x];
	vis[x]=false;
}
int pre(int x)
{
	if (vis[x]) return x;
	while (x>0)
	{
		x=last[x];
		if (vis[x]) return x;
	}
	return x;
}
int nxt(int x)
{
	if (vis[x]) return x;
	while (x<m+1)
	{
		x=next[x];
		if (vis[x]) return x;
	}
	return x;
}
int main()
{
	n=read(),m=read();
	for (int i=1;i<=n;i++)
		L[i]=read(),R[i]=read();
	for (int i=1;i<=n;i++)
		pos[i]=make_pair(L[i],-R[i]);
	sort(pos+1,pos+n+1);
	for (int i=1;i<=m;i++)
		last[i]=i-1,next[i]=i+1;
	next[0]=1,last[m+1]=m;
	last[0]=0,next[m+1]=m+1;
	memset(vis,true,sizeof(vis));
	for (int i=1;i<=n;i++)
	{
		int x=pre(pos[i].first),y=nxt(-pos[i].second);
		if (x>0) del(x);
		else if (y<m+1) del(y);
		else ++ans;
	}
	printf("%d\n",ans);
	return 0;
}


Compiler output:
a.cc: In function 'void del(int)':
a.cc:17:9: error: reference to 'next' is ambiguous
   17 |         next[last[x]]=next[x];
      |         ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:16: note:                 'int next [400007]'
   12 | int last[Maxn],next[Maxn];
      |                ^~~~
a.cc:17:23: error: reference to 'next' is ambiguous
   17 |         next[last[x]]=next[x];
      |                       ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:16: note:                 'int next [400007]'
   12 | int last[Maxn],next[Maxn];
      |                ^~~~
a.cc:18:14: error: reference to 'next' is ambiguous
   18 |         last[next[x]]=last[x];
      |              ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:16: note:                 'int next [400007]'
   12 | int last[Maxn],next[Maxn];
      |                ^~~~
a.cc: In function 'int nxt(int)':
a.cc:36:19: error: reference to 'next' is ambiguous
   36 |                 x=next[x];
      |                   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:16: note:                 'int next [400007]'
   12 | int last[Maxn],next[Maxn];
      |                ^~~~
a.cc: In function 'int main()':
a.cc:50:29: error: reference to 'next' is ambiguous
   50 |                 last[i]=i-1,next[i]=i+1;
      |                             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:16: note:                 'int next [400007]'
   12 | int last[Maxn],next[Maxn];
      |                ^~~~
a.cc:51:9: error: reference to 'next' is ambiguous
   51 |         next[0]=1,last[m+1]=m;
      |         ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:16: note:                 'int next [400007]'
   12 | int last[Maxn],next[Maxn];
      |                ^~~~
a.cc:52:19: error: reference to 'next' is ambiguous
   52 |         last[0]=0,next[m+1]=m+1;
      |                   ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:12:16: note:                 'int next [400007]'
   12 | int last[Maxn],next[Maxn];
      |                ^~~~