POJ 2528-Mayor's posters(线段树区间更新+离散化)

来源:互联网 发布:淘宝开店怎么进货 编辑:程序博客网 时间:2024/05/21 22:45

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 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

                                                                                  

题意:贴海报~求出最后能够看到多少张海报~

思路:数据离散化,线段树区间更新~

AC的辛酸历程~

一开始使用了错误的离散化方法,RE~错误的方法可能导致离散后的前坐标大于后坐标,以及离散后的数据范围不确定~

TEL:开始使用的记录海报的数量方法是先将每一段的海报求和然后再查找和小于N的数据,记录~所以妥妥的T了>.<.。。应该直接数组记录更新的值即可~

            离散化的时候使用set+map,TEL~ 最后使用的方法是 用一个数组记录,sort排序,二分查找~

WA:右端点不需要加一...

CODE:

#include <iostream>#include <cstdio>#include <algorithm>#include <cmath>#include <string>#include <cstring>#include <queue>#include <stack>#include <vector>#include <set>#include <map>const int inf=0xfffffff;typedef long long ll;using namespace std;const int Max=20010;struct node{    int x,y;    int xx,yy;}poster[Max];int setv[Max<<2],vis[Max],num[Max],nn[Max],ans;void build(int pos,int l,int r){    if(l==r){        setv[pos]=0;    }    else{        int mid=(l+r)/2;        build(pos*2,l,mid);        build(pos*2+1,mid+1,r);    }}void pushdown(int pos,int l,int r){    if(setv[pos]!=0){        setv[pos*2]=setv[pos*2+1]=setv[pos];        setv[pos]=0;    }}void update(int pos,int l,int r,int x,int y,int v){    if(x<=l && y>=r){        setv[pos]=v;    }    else{        pushdown(pos,l,r);        int mid=(l+r)/2;        if(x<=mid) update(pos*2,l,mid,x,y,v);        if(y>mid) update(pos*2+1,mid+1,r,x,y,v);    }}void query(int pos,int l,int r){    if(setv[pos]!=0){        if(vis[setv[pos]]==0){            vis[setv[pos]]=1;            ans++;            return;        }    }    if(l==r) return ;    else{        pushdown(pos,l,r);        int mid=(l+r)/2;        query(pos*2,l,mid);        query(pos*2+1,mid+1,r);    }}int main(){    //freopen("in","r",stdin);    int T,N;    scanf("%d",&T);    while(T--){        scanf("%d",&N);        memset(vis,0,sizeof(vis));        int k=1;        for(int i=1;i<=N;i++){            scanf("%d%d",&poster[i].x,&poster[i].y);            nn[k++]=poster[i].x;            nn[k++]=poster[i].y;        }        sort(nn+1,nn+k);        int j=2;        num[1]=nn[1];        for(int i=2;i<k;i++){            if(nn[i]!=nn[i-1]){                num[j++]=nn[i];            }            else continue;        }        for(int i=1;i<=N;i++){            poster[i].xx=lower_bound(num,num+j,poster[i].x)-num;            poster[i].yy=lower_bound(num,num+j,poster[i].y)-num;        }//        for(int i=1;i<j;i++){//            m[num[i]]=i;//        }        int maxn=j;        build(1,1,maxn);        for(int i=1;i<=N;i++){            //printf("%d %d\n",poster[i].xx,poster[i].yy);            update(1, 1, maxn,                   poster[i].xx, poster[i].yy, i);        }        ans=0;        query(1,1,maxn);        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击