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

来源:互联网 发布:中天证券交易软件 编辑:程序博客网 时间:2024/04/30 15:13
Mayor's posters
Time Limit: 1000MSMemory Limit: 65536KTotal Submissions: 32016Accepted: 9282

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

151 42 68 103 47 10

Sample Output

4
线段树成段更新的问题,但是区间最大有一千万,空间上不允许直接操作。所以要离散化。考虑到n最大为10000,则离散化后最多有20000个点,既线段树的区间最大只要开到20000*4就可以了。离散化的意思是,考虑样例,输入的点有1 4 2 6 8 10 3 7这8个点,那么只要对他们进行排序,就可以得到用1~8来表示这些区间,既样例可以表示为1 4,2 5,7 8,3 4,6 8.那么线段树的根节点表示的区间就只要为1~8。
为了得到上面的8个点,既去掉重复的,我选择用STL里的set来解决。然而,这样的离散化是有问题的(以下来自HH博客):
(给出下面两个简单的例子应该能体现普通离散化的缺陷:例子一:1-10 1-4 5-10例子二:1-10 1-4 6-10普通离散化后都变成了[1,4][1,2][3,4]线段2覆盖了[1,2],线段3覆盖了[3,4],那么线段1是否被完全覆盖掉了呢?例子一是完全被覆盖掉了,而例子二没有被覆盖 

为了解决这种缺陷,我们可以在排序后的数组上加些处理,比如说[1,2,6,10]如果相邻数字间距大于1的话,在其中加上任意一个数字,比如加成[1,2,3,6,7,10],然后再做线段树就好了.)

这样便完成了离散化。然后就是线段树成段更新的做法了,注意更新区间利用二分查找找出原来的区间对应的离散化值,线段树维护该区间贴的是哪张海报(并且可见),最后统计一下海报的张数即可。

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <set>#define SIZE 10005#define ls l,mid,rt<<1#define rs mid+1,r,rt<<1|1using namespace std;int T,N,idx,ans;int cv[SIZE<<4];int temp[SIZE<<2],Lt[SIZE],Rt[SIZE];bool vis[SIZE];set <int> st;set <int>::iterator it;void pushDown(int rt){    if(cv[rt] != -1)    {        cv[rt<<1] = cv[rt<<1|1] = cv[rt];        cv[rt] = -1;    }}void update(int l,int r,int rt,int L,int R,int w){    if(L <=l && r <= R)    {        cv[rt] = w;        return;    }    pushDown(rt);    int mid = (l + r) >> 1;    if(L <= mid)update(ls,L,R,w);    if(R > mid)update(rs,L,R,w);}int binarySearch(int tar){    int low = 1,high = idx;    while(low <= high)    {        int mid = (low + high) >> 1;        if(temp[mid] < tar)            low = mid + 1;        else if(temp[mid] > tar)            high = mid - 1;        else            return mid;    }    return -1;}void find(int l,int r,int rt){    if(cv[rt] != -1)    {        if(!vis[cv[rt]])        {            ans ++;            vis[cv[rt]] = true;        }        return;    }    if(l == r)        return;    int mid = (l + r) >> 1;    find(ls);    find(rs);}int main(){    scanf("%d",&T);    while(T--)    {        scanf("%d",&N);        idx = 0;        st.clear();        for(int i=1; i<=N; i++)        {            scanf("%d%d",&Lt[i],&Rt[i]);            st.insert(Lt[i]);            st.insert(Rt[i]);        }        for(it = st.begin(); it != st.end(); it ++)            temp[++idx] = (*it);        int t = idx;        for(int i=1; i<t; i++)            if(temp[i+1] > temp[i] + 1)                temp[++idx] = temp[i] + 1;        sort(temp+1,temp+1+idx);        int L,R;        memset(cv,-1,sizeof(cv));        memset(vis,0,sizeof(vis));        ans = 0;        for(int i=1; i<=N; i++)        {            L = binarySearch(Lt[i]);            R = binarySearch(Rt[i]);            update(1,idx,1,L,R,i);        }        find(1,idx,1);        printf("%d\n",ans);    }    return 0;}