需要离散化,, 额,,, 用MAP会超时,但是用数组加二分查找就不会
#include <iostream>
#include <string.h>#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <map>
using namespace std;
#define lson l , m, rt<<1
#define rson m + 1, r, rt << 1 | 1
#define MAXN 20010
int col[MAXN << 2], ans, k;
int LL[MAXN], RR[MAXN], num[MAXN<<1];
bool vis[MAXN];
//map<int , int > list;
int list[MAXN<<1];
void pushdown( int rt)
{
if(col[rt])
{
col[rt << 1] = col[rt << 1|1] = col[rt];
col[rt] = 0;
}
}
void update( int L, int R, int val, int l, int r, int rt)
{
if( L <= l && r <= R)
{
col[rt] = val;
return ;
}
pushdown(rt);
int m = ( l + r) >> 1;
if(L <= m) update( L, R, val, lson);
if(m < R) update( L, R, val, rson);
}
void insert( int n)
{
int cnt = 0;
//list.clear();
memset(vis, 0, sizeof(vis));
for( int i = 0; i < n; i ++)
{
scanf("%d %d",&LL[i], &RR[i]);
num[cnt++] = LL[i];
num[cnt++] = RR[i];
}
sort( num, num + cnt);
//list[num[0]] = 1;
list[1] = num[0];
k = 1;
for( int i = 1; i < cnt; i++)
{
if(num[i] != num[i-1])
list[++k] = num[i];
// list[num[i]] = k++;
}
}
int cheak(int aa,int nn)
{
int l=1,r=nn;
while(l<=r)
{
int m=(l+r)>>1;
if(list[m]==aa) return m;
if(list[m]<aa) l=m+1;
else r=m;
}
return -1;
}
void cal( int l, int r, int rt)
{
if(vis[col[rt]]) return ;
if(col[rt])
{
vis[col[rt]] = 1;
ans++;
return ;
}
int m = (l + r) >> 1;
cal(lson);
cal(rson);
}
int main()
{
int t, n, c = 1;
scanf("%d",&t);
while(t--)
{
c = 1;
ans = 0;
memset(col, 0, sizeof(col));
scanf("%d",&n);
insert(n);
//int ll = list.size();
for( int i = 0; i < n; i++)
{
int ll = cheak(LL[i], k);
int rr = cheak(RR[i],k );
update(ll, rr, c++, 1, k, 1);
}
cal(1, k, 1);
printf("%d\n",ans);
}
}