学到了离散化~~~~
但是这题数据有问题
比方说
3
1 10
1 4
6 10
正确答案应该是3
但是只有当你得出2的时候才能AC
坑吧。。。。。。
AC代码如下:
但是我认为正确的代码得加上注释那部分
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
struct Node{
int a, b;
};
int X[33000], tot;
Node node[11000];
int N;
int col[160000];
int cnt;
bool hashs[33000];
int binary( int num, int sum, int A[] ){
int l, r, mid;
l = 0;
r = sum - 1;
while( l <= r ){
mid = ( l + r ) / 2;
if( A[mid] == num ) return mid;
else if( A[mid] > num ) r = mid - 1;
else l = mid + 1;
}
return -1;
}
int lc( int root ){
return 2 * root;
}
int rc( int root ){
return 2 * root + 1;
}
void PushDown( int root ){
if( col[root] != -1 ){
col[lc(root)] = col[rc(root)] = col[root];
col[root] = -1;
}
}
void Updata( int color, int l, int r, int L, int R, int root ){
if( l <= L && r >= R ){
col[root] = color;
return;
}
PushDown( root );
int mid = ( L + R ) / 2;
if( l <= mid ) Updata( color, l, r, L, mid, lc( root ) );
if( r > mid ) Updata( color, l, r, mid + 1, R, rc( root ) );
}
void query( int l, int r, int root ){
if( col[root] != -1 ){
if( !hashs[col[root]] ){
hashs[col[root]] = true;
cnt++;
}
return;
}
if( l == r ){
return;
}
int mid = ( l + r ) / 2;
query( l, mid, lc( root ) );
query( mid + 1, r, rc( root ) );
}
int main(){
int T;
scanf( "%d", &T );
while( T-- ){
scanf( "%d", &N );
memset( col, -1, sizeof( col ) );
memset( hashs, false, sizeof( hashs ) );
cnt = 0;
int temp = 0;
for( int i = 0; i < N; i++ ){
scanf( "%d%d", &node[i].a, &node[i].b );
X[temp++] = node[i].a;
X[temp++] = node[i].b;
}
sort( X, X + temp );
tot = 1;
for( int i = 1; i < temp; i++ ){
if( X[i] != X[i-1] ) X[tot++] = X[i];
}
// for( int i = tot - 1; i > 0; i-- ){
// if( X[i] != X[i-1] + 1 ) X[tot++] = X[i-1] + 1;
// }
sort( X, X + tot );
for( int i = 0; i < N; i++ ){
int l = binary( node[i].a, tot, X );
int r = binary( node[i].b, tot, X );
Updata( i, l, r, 0, tot - 1, 1 );
}
query( 0, tot - 1, 1 );
cout << cnt << endl;
}
return 0;
}