测试

线段树
#include <cstdio>
#include 
<cstring>
#include 
<algorithm>
using namespace std;
#define LC(x) ((x) << 1)
#define RC(x) ((x) << 1 | 1)

struct node {
    
int left,right;
    
int color;
    
int mid() {return (left + right) >> 1;}
};

struct poster {
    
int left,right;
}p[
10010];

node t[
100000];

int hash_color[10010];
int pos[20020];
int cnt;
int m;

void build(int l,int r,int idx){
    t[idx].left 
= l;
    t[idx].right 
= r;
    t[idx].color 
= -1;
    
if (l == r) return;
    
int mid = t[idx].mid();
    build(l,mid,LC(idx));
    build(mid
+1,r,RC(idx));
    
return;
}

void update(int l,int r,int c,int idx){
    
if (l <= t[idx].left && t[idx].right <= r){
        t[idx].color 
= c;
        
return;
    }
    
if (t[idx].color != -1){
        t[LC(idx)].color 
= t[RC(idx)].color = t[idx].color;
        t[idx].color 
= -1;
    }
    
int mid = t[idx].mid();
    
if (l <= mid) update(l,r,c,LC(idx));
    
if (r > mid) update(l,r,c,RC(idx));
    
return;
}

void query(int l,int r,int idx){
    
if (t[idx].color != -1){
        
if (!hash_color[t[idx].color]){
            cnt
++;
            hash_color[t[idx].color] 
= true;
        }
        
return;
    }
    
int mid = t[idx].mid();
    
if (r <= mid){
        query(l,r,LC(idx));
    } 
else if (l > mid){
        query(l,r,RC(idx));
    } 
else {
        query(l,mid,LC(idx));
        query(mid
+1,r,RC(idx));
    }
    
return;
}

int search(int x){
    
int low = 0, high = m - 1;
    
while (low <= high){
        
int mid = (low + high) >> 1;
        
if (pos[mid] == x)
            
return mid;
        
else if (pos[mid] < x)
            low 
= mid + 1;
        
else
            high 
= mid - 1;
    }
    
return -1;
}


int main(){
    
int __case;
    scanf(
"%d",&__case);
    
while (__case--){
        
int n,i;
        scanf(
"%d",&n);
        
for (i=0;i<n;i++){
            scanf(
"%d%d",&p[i].left,&p[i].right);
            pos[LC(i)] 
= p[i].left;
            pos[RC(i)] 
= p[i].right;
        }
        sort(pos,pos
+2*n);
        
for (m=0,i=1;i<2*n;i++)
            
if (pos[i] != pos[m])
                pos[
++m] = pos[i];
        
++m;
        build(
0,m-1,1);
        
for (i=0;i<n;i++){
            update(search(p[i].left),search(p[i].right),i,
1);
        }
        cnt 
= 0;
        memset(hash_color,
0,sizeof(hash_color));
        query(
0,m-1,1);
        printf(
"%d\n",cnt);
    }
    
return 0;
}

 

posted @ 2010-05-10 18:34  ziroy  Views(117)  Comments(0)    收藏  举报