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

来源:互联网 发布:软件需求分析阶段 编辑:程序博客网 时间:2024/06/05 10:10
Mayor's posters
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 40506 Accepted: 11781

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
解决方案:通过这题我对线段树又有了新的体会。本题要注意两点:1)由于本题叶子节点可以是一个1bit的区间,所以要用L+1==R判断是否为叶子节点。2)本题的区间太大,会MLE,所以要进行离散化。
离散化:我用的是stl里的Map。首先由于要化为了区间,可以这样处理:l--或r++都行。然后对所有左右区间进行从小到大排序,排好序后对各个位置进行编号,这是离散化关键一步。成功后,对应区间的值取离散化后的编号即可,这样所用的内存会少了许多。
本题大概思路是不断更新线段树,然后查询整个区间,把出现过的海报标记就行了。
code:
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<map>#define MMAX 20003#define MAX 10003using namespace std;vector<int > V;map <int ,int >H;int setv[4*MMAX],cnt[MAX],N,ll[MAX],rr[MAX];void push_down(int rt){    int lc=rt*2,lr=rt*2+1;    if(setv[rt]>=1)    {        setv[lc]=setv[lr]=setv[rt];        setv[rt]=0;    }}void update(int rt,int L,int R,int l,int r,int v){    if(l<=L&&r>=R)    {        setv[rt]=v;    }    else    {        push_down(rt);        int M=(L+R)/2;        if(l<M) update(rt*2,L,M,l,r,v);        if(r>M) update(rt*2+1,M,R,l,r,v);    }}void query(int rt,int L,int R){    if(setv[rt]||L+1==R)///由于线段树最小区间不再是一个点,而是单位区间,L+1==R为边界条件    {        cnt[setv[rt]]=1;    }    else    {        int M=(L+R)/2;       // cout<<rt<<endl;        query(rt*2,L,M);        query(rt*2+1,M,R);    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        memset(setv,0,sizeof(setv));        memset(cnt,0,sizeof(cnt));        scanf("%d",&N);        V.clear(),H.clear();        for(int i=0; i<N; i++)        {            int l,r;            scanf("%d%d",&ll[i],&rr[i]);            rr[i]++;            V.push_back(ll[i]),V.push_back(rr[i]);        }        sort(V.begin(),V.end());///离散化线段树离散之先排序        V.erase(unique(V.begin(),V.end()),V.end());///离散化线段树之删除重复元素        for(int i=0;i<(int)V.size();i++)H[V[i]]=i+1;///对区间进行离散化        ///以上是用map对区间进行离散化        int _MAX=(int)V.size();        for(int i=0;i<N;i++)        update(1,1,_MAX,H[ll[i]],H[rr[i]],i+1);        query(1,1,_MAX);        int res=0;        for(int i=1;i<=N;i++) if(cnt[i]) res++;        printf("%d\n",res);    }    return 0;}

0 0