这题的离散化有点不一样。要在第一次离散化之后把每个数字之间差不为一的地方在随便填进去一个数。然后就是线段树的区间更新加查询
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 10005;
int n,num;
int li[maxn];
int ri[maxn];
int ne[maxn<<3];
int clo[maxn<<4];
bool hashs[maxn];
int ans = 0;
void PushDown(int root)
{
clo[root<<1] = clo[root<<1|1] = clo[root];
clo[root] = -1;
}
void Update(int L,int R,int c,int l,int r,int root)
{
if(l>=L&&r<=R)
{
clo[root] = c;
return ;
}
if(clo[root]!=-1) PushDown(root);
int m = (l+r)>>1;
if(m>=L) Update(L,R,c,l,m,root<<1);
if(m<R) Update(L,R,c,m+1,r,root<<1|1);
}
void query(int l,int r,int root)
{
if(l==r){
if(!hashs[clo[root]]){
ans++;
hashs[clo[root]] = true;
}
return ;
}
if(clo[root]!=-1) PushDown(root);
int m = (l+r)>>1;
query(l,m,root<<1);
query(m+1,r,root<<1|1);
}
inline int find(int a)
{
return upper_bound(ne+1,ne+1+num,a)-ne-1;
}
int main()
{
int cases; int tot;
scanf("%d",&cases);
while(cases--)
{
scanf("%d",&n);
tot = 0;
memset(hashs,false,sizeof(hashs));
memset(clo,-1,sizeof(clo));
for(int i=1;i<=n;i++)
{
scanf("%d%d",&li[i],&ri[i]);
ne[++tot] = li[i];
ne[++tot] = ri[i];
}
sort(ne+1,ne+tot+1);
num = 1;
for(int i=2;i<=tot;i++)
{
if(ne[i]!=ne[i-1])
ne[++num] = ne[i];
}
for(int i=num;i>1;i--) if(ne[i]-ne[i-1]>1) ne[++num] = ne[i]-1;
sort(ne+1,ne+num+1);
for(int i=1;i<=n;i++)
{
int ll = find(li[i]);
int rr = find(ri[i]);
Update(ll,rr,i,1,num,1);
}
ans = 0;
query(1,num,1);
printf("%d\n",ans);
}
return 0;
}
/*10
3
1 10
1 4
6 10
*/