Mayor's posters(区间覆盖+点离散化)

来源:互联网 发布:知乎电子书 编辑:程序博客网 时间:2024/06/11 09:11
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 l i 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, l i+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

题意:

要在墙上贴海报,后来的海报可以覆盖同一区域的海报,问贴完所有的海报,在上面一共可以看到几张

思路:线段树区间覆盖,点离散化

点离散化:由于区间范围较大,可以离散化点,使他们更紧凑,节省空间和时间,离散后的区间长度不发生变化,以样例为例:

                  原所有端点 1,2,3,4,6,7,8,10      原区间  【1,4】【2,6】【8,10】【3,4】【7,10】 原建树范围【1,10】

                 离散后端点 1,2,3,4,5,6,7,8         新区间   【1,4】【2,5】【7,8】【3,4】【6,8】    新建树范围【1,8】

离散化实现代码:

//点离散化        int i;        for(i=0;i<n;i++){            //map[i][0]为第i个区间的左端点  map[i][1]为右右端点            scanf("%d%d",&map[i][0],&map[i][1]);            a[i*2].point=map[i][0];            a[i*2+1].point=map[i][1];            a[i*2].num=-(i+1); //如果是左端点记录为负数            a[i*2+1].num=i+1;  //如果是右端点记录为正数        }        sort(a,a+n*2,cmp);//将所有端点按从小到大排序        int tem=a[0].point;        int cnt=1;        for(i=0;i<n*2;i++){            if(tem!=a[i].point){//与前一个点不同                cnt++;                tem=a[i].point;            }            if(a[i].num<0) map[-a[i].num-1][0]=cnt;            else map[a[i].num-1][1]=cnt;        }

全部代码:

#include<stdio.h>#include<string.h>#include<algorithm>#define MAX 10005using namespace std;struct node{    int col;}tree[MAX*4];struct node1{    int point;    int num;}a[MAX*2];int map[MAX][2],flag[MAX*4],ans;int cmp(node1 a,node1 b){    return a.point<b.point;}void build(int root,int left,int right){    tree[root].col=0; //区间没有染过色初始化为0    if(left==right) return;    int mid=(left+right)/2;    build(root*2,left,mid);    build(root*2+1,mid+1,right);}void update(int root,int nleft,int nright,int left,int right,int m){    if(nleft==left&&nright==right){//如果当前区间刚好符合        tree[root].col=m;//将该区间的颜色覆盖,直接返回,下面的子区间是否染色无所谓        return;          //因为已经被覆盖    }    if(tree[root].col){//如果此区间被染色,将颜色传递给左右子区间        tree[root*2].col=tree[root*2+1].col=tree[root].col;        tree[root].col=0;//父区间初始化为0    }    int mid=(nleft+nright)/2;    if(right<=mid) update(root*2,nleft,mid,left,right,m);    else if(left>mid) update(root*2+1,mid+1,nright,left,right,m);    else{        update(root*2,nleft,mid,left,mid,m);        update(root*2+1,mid+1,nright,mid+1,right,m);    }}void countt(int root,int left,int right){//访问每个结点,记录颜色的种类    if(tree[root].col){//如果有颜色,并且没被记录过        if(!flag[tree[root].col]){            ans++;            flag[tree[root].col]=1;        }        return;    }    int mid=(left+right)/2;    countt(root*2,left,mid);    countt(root*2+1,mid+1,right);    return;//访问完所有结点返回}int main(){    int T;    scanf("%d",&T);    while(T--){        memset(flag,0,sizeof(flag));        int n;        scanf("%d",&n);        //点离散化        int i;        for(i=0;i<n;i++){            //map[i][0]为第i个区间的左端点  map[i][1]为右右端点            scanf("%d%d",&map[i][0],&map[i][1]);            a[i*2].point=map[i][0];            a[i*2+1].point=map[i][1];            a[i*2].num=-(i+1); //如果是左端点记录为负数            a[i*2+1].num=i+1;  //如果是右端点记录为正数        }        sort(a,a+n*2,cmp);//将所有端点按从小到大排序        int tem=a[0].point;        int cnt=1;        for(i=0;i<n*2;i++){            if(tem!=a[i].point){//与前一个点不同                cnt++;                tem=a[i].point;            }            if(a[i].num<0) map[-a[i].num-1][0]=cnt;            else map[a[i].num-1][1]=cnt;        }        build(1,1,cnt);        for(i=0;i<n;i++)           update(1,1,cnt,map[i][0],map[i][1],i+1);        ans=0;        countt(1,1,cnt);        printf("%d\n",ans);    }return 0;}


原创粉丝点击