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

来源:互联网 发布:域名如何更换 编辑:程序博客网 时间:2024/04/29 02:12

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

Source

Alberta Collegiate Programming Contest 2003.10.18


题意:

给每个区间贴纸,后面贴的会覆盖前面贴的,问最后能看到的纸有几张;
思路:
一道线段树的查询与更新的题,由于每个数据组的l,r都是在10000000以内,不加处理用线段树肯定会超时;
我们在线段树之前先将数据离散化处理;
对于每个测试组给定的数据,最多为2*n个数,我们可以先将这些数用结构体记录数和区间的位置,然后从小到大排序,我们可以得到每个数在所有数中对应的第几大,并用二维map数组记录;这样就将数据离散化处理了;
代码:
#include<cstdio>#include<algorithm>#include<cstring>using namespace std;const int N=100005;int map[2*N][2],vis[2*N],sum;struct no{    int point,num;}a[4*N];struct node{    int l,r,w;}str[3*N];bool cmp(no x,no y){    return x.point<y.point;}void build(int l,int r,int n){    str[n].l=l;    str[n].r=r;    str[n].w=0;    if(l==r)        return;    int temp=(l+r)/2;    build(l,temp,2*n);    build(temp+1,r,2*n+1);}void inser(int l,int r,int n,int w){    if(str[n].l==l&&str[n].r==r)    {        str[n].w=w;        return;    }    if(str[n].w)  //延迟标记    {        str[2*n].w=str[2*n+1].w=str[n].w;        str[n].w=0;    }    int temp=(str[n].l+str[n].r)/2;    if(r<=temp)        inser(l,r,2*n,w);    else if(l>temp)        inser(l,r,2*n+1,w);    else    {        inser(l,temp,2*n,w);        inser(temp+1,r,2*n+1,w);    }}void query(int n){    if(str[n].w)    {        if(!vis[str[n].w])  //如果此颜色被计数了,就不要再重复了        {            sum++;            vis[str[n].w]=1;        }        return;    }    query(2*n);    query(2*n+1);    return;}int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n;        scanf("%d",&n);        for(int i=0;i<n;i++)        {            scanf("%d%d",&map[i][0],&map[i][1]);            a[2*i].point=map[i][0];    //a记录每个数和它对应的区间位置,左区间记为负;            a[2*i+1].point=map[i][1];            a[2*i].num=-(i+1);            a[2*i+1].num=i+1;        }        sort(a,a+2*n,cmp);        int temp=a[0].point;        int cnt=1;        for(int i=0;i<2*n;i++)        {            if(a[i].point!=temp)  //去重            {                temp=a[i].point;                cnt++;            }            if(a[i].num<0)   //map记录每个数在数据中的大小                map[-a[i].num-1][0]=cnt;            else                map[a[i].num-1][1]=cnt;        }        build(1,cnt,1);        for(int i=0;i<n;i++)        {            inser(map[i][0],map[i][1],1,i+1);        }        memset(vis,0,sizeof(vis));        sum=0;        query(1);        printf("%d\n",sum);    }}


0 0