POJ 2528 Mayor posters 线段树 成段更新

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mid int m = (l + r)>>1
#define maxn 10004
int col[maxn<<4];
int l[maxn], r[maxn];
int a[maxn<<4], m, n;
int cnt, vis[maxn<<4];
void push_down(int rt)
{
    if(col[rt] != -1)
    {
        col[rt<<1] = col[rt<<1|1] = col[rt];
        col[rt] = -1;
    }
}
void update(int L, int R, int v, int l, int r, int rt)
{
    if(L <= l && r <= R)
    {
        col[rt] = v;
        return;
    }
    push_down(rt);
    mid; 
    if(L <= m) update(L, R, v, lson);
    if(m < R) update(L, R, v, rson);
}
void query(int l, int r, int rt)
{
    if(col[rt] != -1)
    {
        if(!vis[col[rt]])
            cnt++, vis[col[rt]] = 1;
        return;
    }
    if(l == r) return;
    push_down(rt);
    mid;
    query(lson);
    query(rson);
}
int bin(int key)
{
    int l = 0, r = m-1;
    while(l <= r)
    {
        int mm = (l + r) >> 1;
        if(a[mm] == key) return mm;
        if(a[mm] > key) r = mm-1;
        else l = mm+1;
    }
    return -1;
}
int main()
{
    int i, j, cas;
    scanf("%d", &cas);
    while(cas--)
    {
        scanf("%d", &n);
        memset(col, -1, sizeof(col));
        m = 0;
        for(i = 0; i < n; i++)
        {
            scanf("%d%d", &l[i], &r[i]);
            a[m++] = l[i];
            a[m++] = l[i]+1;
            a[m++] = r[i];
            a[m++] = r[i]+1;
        }
        sort(a, a+m);
        m = unique(a, a+m) - a;
        for(i = 0; i < n; i++)
            update(bin(l[i]), bin(r[i]), i, 0, m-1, 1);
        memset(vis, 0, sizeof(vis));
        cnt = 0;
        query(0, m-1, 1);
        printf("%d\n", cnt);
    }
    return 0;
}

你可能感兴趣的:(POJ 2528 Mayor posters 线段树 成段更新)