POJ 2528 Mayor's posters

线段树成段替换+离散化

/* ***********************************************
Author        :Zhou Zhentao
Email         :774388357@qq.com
Created Time  :2015/11/20 17:21:35
File Name     :acm.cpp
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

#define lson l , m , rt << 1    
#define rson m + 1 , r , rt << 1 | 1   
#define root 1 , N , 1   
#define LL long long    
const int maxn = 200000+10;    
LL add[maxn<<2];    
int x,tot,ans;
int q[maxn];
int li[maxn] , ri[maxn];  
int X[maxn*3];  
int flag[maxn];
int cnt; 

int Bin(int key,int n,int X[]) {  
    int l = 0 , r = n - 1;  
    while (l <= r) {  
        int m = (l + r) >> 1;  
        if (X[m] == key) return m;  
        if (X[m] < key) l = m + 1;  
        else r = m - 1;  
    }  
    return -1;  
}  
   
void PushDown(int rt) {    
    if (add[rt]) {    
        add[rt<<1] = add[rt];    
        add[rt<<1|1] = add[rt];    
        add[rt] = 0;    
    }    
}    
  
void update(int L,int R,int c,int l,int r,int rt) {    
    if (L <= l && r <= R) {    
        add[rt] = c;    
        return ;    
    }
    PushDown(rt);    
    int m = (l + r) >> 1; 
    if (L <= m) update(L , R , c , lson);    
    if (m < R) update(L , R , c , rson);    
}   

void Qu(int L,int R,int l,int r,int rt)
{
    if(add[rt]!=0)
    {
        if(!flag[add[rt]])
        {
            ans++;
            flag[add[rt]]=1;
        }
        return;
    }

    if(l==r) return;
//    PushDown(rt);
    int m = (l + r) >> 1;
    Qu(L , R , lson);
    Qu(L , R , rson);
}

int main()
{    
//    freopen("in.txt","r",stdin);
    int N,T;
    scanf("%d",&T);
     while(T--)
     {
        scanf("%d",&N);  
        int nn = 0;  
        for (int i = 0 ; i < N ; i ++) {  
            scanf("%d%d",&li[i] , &ri[i]);  
            X[nn++] = li[i];  
            X[nn++] = ri[i];  
        }  
        sort(X , X + nn);  
        int m = 1;  
        for (int i = 1 ; i < nn; i ++) {  
            if (X[i] != X[i-1]) X[m ++] = X[i];  
        }  
        for (int i = m - 1 ; i > 0 ; i --) {  
            if (X[i] != X[i-1] + 1) X[m ++] = X[i-1] + 1;  
        }  
        sort(X , X + m);  
        memset(add , 0 , sizeof(add));  
        for (int i = 0 ; i < N ; i ++) {  
            int l = Bin(li[i] , m , X);  
            int r = Bin(ri[i] , m , X); 
            l++;r++;
        //    printf("---- %d %d\n",l,r);
            update(l , r , i+1 ,1,m+1,1);  
        }  
        ans = 0;  
        memset(flag, 0 , sizeof(flag));  
        Qu(1,m,1 , m+1 , 1);  
        printf("%d\n",ans);  
    }
    return 0;    
}    

 

posted @ 2015-11-22 19:22  Fighting_Heart  阅读(181)  评论(0编辑  收藏  举报