码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2528 Mayor's posters(离散化 线段树)

时间:2015-04-18 19:16:11      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:acm   线段树   离散化   

题意  在墙上贴n张海报  输入每张海报的的左右端点坐标  问最后可以看到多少张海报  能看到一点也是能看到

先把线段树初始化为0 输入一张海报  就把那个区间变成这张海报的序号  最后判断墙上有多少个不同的序号就行了

但是海报坐标的端点值高达10000000  直接用线段树会超时   但是注意到海报最多只有10000张  也就是最多有20000个不同的坐标  于是可以利用离散化的知识   把所有坐标排序 注意所有右端点坐标+1也要加入排序(注意1,10 ; 1,3;  7,10这种情况  如果右端点+1没加入排序的话可能使原来不相邻的变为相邻  这样就覆盖了本来没有覆盖的区间)  可以发现  把每个端点的坐标变为该点排序后的序号不会改变整个图形的结构  于是就可以用序号代替坐标了  大大减小了复杂度

#include <cstdio>
#include <cstring>
#include <algorithm>
#define lc p<<1,s,mid
#define rc p<<1|1,mid+1,e
#define mid ((s+e)>>1)
#define CLR(A) memset(A,0,sizeof(A))
using namespace std;

const int N = 20005;
int col[N * 4], setv[N * 4], vis[N];
int le[N], ri[N], c[N * 2], ans;

void pushup(int p)
{
    col[p] = (col[p << 1] == col[p << 1 | 1] ? col[p << 1] : -1);
}

void pushdown(int p, int s, int e)
{
    if(!setv[p] || s == e) return;
    setv[p << 1] = col[p << 1] = setv[p];
    setv[p << 1 | 1] = col[p << 1 | 1] = setv[p];
    setv[p] = 0;
}

void update(int p, int s, int e, int l, int r, int v)
{
    if(s == l && e == r)
    {
        setv[p] = col[p] = v;
        return;
    }
    pushdown(p, s, e);
    if(r <= mid) update(lc, l, r, v);
    else if(l > mid) update(rc, l, r, v);
    else update(lc, l, mid, v), update(rc, mid + 1, r, v);
    pushup(p);
}

void query(int p, int s, int e, int l, int r)
{
    if(e < s) return;
    if(s == l && e == r && col[p] != -1)
    {
        if(!vis[col[p]]) vis[col[p]] = 1, ++ans;
        return;
    }

    pushdown(p, s, e);
    query(lc, l, mid);
    query(rc, mid + 1, r);
}

int compress(int n) //离散化
{
    int k = 0;
    for(int i = 0; i < n; ++i)
    {
        c[k++] = le[i];
        c[k++] = ri[i];
        c[k++] = ri[i] + 1;
    }
    sort(c, c + k);
    return unique(c, c + k) - c - 1;
}

int main()
{
    int cas, m, n, l, r;
    scanf("%d", &cas);
    while(cas--)
    {
        scanf("%d", &m);
        for(int i = 0; i < m; ++i)
            scanf("%d%d", &le[i], &ri[i]);
        n = compress(m);

        ans = 0;
        CLR(col), CLR(vis), CLR(setv);
        for(int i = 0; i < m; ++i)
        {
            l = lower_bound(c, c + n, le[i]) - c + 1;
            r = lower_bound(c, c + n, ri[i]) - c + 1;
            update(1, 1, n, l, r, i + 1);
        }
        vis[0] = 1;
        query(1, 1, n, 1, n);
        printf("%d\n", ans);
    }
    return 0;
}

Mayor‘s posters

Description

The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campaign have been placing their electoral posters at all places at their whim. The city council has finally decided to build an electoral wall for placing the posters and introduce the following rules: 
  • Every candidate can place exactly one poster on the wall. 
  • All posters are of the same height equal to the height of the wall; the width of a poster can be any integer number of bytes (byte is the unit of length in Bytetown). 
  • The wall is divided into segments and the width of each segment is one byte. 
  • Each poster must completely cover a contiguous number of wall segments.

They have built a wall 10000000 bytes long (such that there is enough place for all candidates). When the electoral campaign was restarted, the candidates were placing their posters on the wall and their posters differed widely in width. Moreover, the candidates started placing their posters on wall segments already occupied by other posters. Everyone in Bytetown was curious whose posters will be visible (entirely or in part) on the last day before elections. 
Your task is to find the number of visible posters when all the posters are placed given the information about posters‘ size, their place and order of placement on the electoral wall. 

Input

The first line of input contains a number c giving the number of cases that follow. The first line of data for a single case contains number 1 <= n <= 10000. The subsequent n lines describe the posters in the order in which they were placed. The i-th line among the n lines contains two integer numbers li and ri which are the number of the wall segment occupied by the left end and the right end of the i-th poster, respectively. We know that for each 1 <= i <= n, 1 <= li <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered li, li+1 ,... , ri.

Output

For each input data set print the number of visible posters after all the posters are placed. 

The picture below illustrates the case of the sample input. 
技术分享

Sample Input

1
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4

Source



POJ 2528 Mayor's posters(离散化 线段树)

标签:acm   线段树   离散化   

原文地址:http://blog.csdn.net/acvay/article/details/45115469

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!