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

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 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, 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

1
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4
题意:有n(<=10000)个海报给出每个海报的左边界和右边界(<=1000000000),问最后能看到的海报有多少个
思路:由于给出的边界范围太大,没有办法直接进行存。但是注意到给出的n挺小的,所以可以考虑离散化的方法进行保存。对于这个题,我所学到的离散化就是将原本输入的边界进行排序,然后用不超过2n的数来表示边界。然后用线段树进行染色和储存。注意如果一个节点的被覆盖的话,那么他的子节点也会被覆盖。然后再进行标记求和就行了。
AC代码
  1 #include <iostream>
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <cstring>
  5 using namespace std;
  6 const int maxn=20000+10;
  7 int flag[maxn<<1],mp[maxn<<1][2],ans;
  8 struct ent
  9 {
 10     int point,num;
 11 } a[maxn<<1];
 12 struct note
 13 {
 14     int l,r,ans;
 15 } tree[maxn<<2];
 16 int cmp(const struct ent &a,const struct ent &b)
 17 {
 18     return a.point<b.point;
 19 }
 20 int build(int l,int r,int k)
 21 {
 22     tree[k].l=l;
 23     tree[k].r=r;
 24     tree[k].ans=0;
 25     if(l==r)
 26         return 0;
 27     int mid=(tree[k].l+tree[k].r)/2;
 28     build(l,mid,k*2);
 29     build(mid+1,r,k*2+1);
 30     return 0;
 31 }
 32 int ins(int l,int r,int k,int d)
 33 {
 34     if(l==tree[k].l&&tree[k].r==r)
 35     {
 36         tree[k].ans=d;
 37         return 0;
 38     }
 39     if(tree[k].ans>0)
 40     {
 41         tree[k*2].ans=tree[k*2+1].ans=tree[k].ans;//覆盖子节点
 42         tree[k].ans=0;
 43     }
 44     int mid=(tree[k].l+tree[k].r)/2;
 45     if(tree[k*2].r>=r) ins(l,r,k*2,d);
 46     else if(tree[k*2+1].l<=l) ins(l,r,k*2+1,d);
 47     else
 48     {
 49         ins(l,mid,k*2,d);
 50         ins(mid+1,r,k*2+1,d);
 51     }
 52     return 0;
 53 }
 54 int sovle (int k)
 55 {
 56     if(tree[k].ans>0)
 57     {
 58         if(!flag[tree[k].ans])
 59         {
 60             ans++;
 61             flag[tree[k].ans]=1;
 62         }
 63         return 0;
 64     }
 65     sovle(k*2);
 66     sovle(k*2+1);
 67     return 0;
 68 }
 69 int main()
 70 {
 71     int n,t;
 72     while(~scanf("%d",&t))
 73     {
 74         while(t--)
 75         {
 76             scanf("%d",&n);
 77             for(int i=0; i<n; i++)
 78             {
 79                 scanf("%d%d",&mp[i][0],&mp[i][1]);
 80                 a[i*2].point=mp[i][0];
 81                 a[i*2+1].point =mp[i][1];
 82                 a[i*2].num=-(i+1);
 83                 a[i*2+1].num=i+1;
 84             }
 85             sort(a,a+n*2,cmp);
 86             int  tmp,cnt;
 87             tmp=a[0].point;
 88             cnt=1;
 89             for(int i=0; i<2*n; i++)
 90             {
 91                 if(a[i].point !=tmp)
 92                 {
 93                     cnt++;
 94                     tmp=a[i].point ;
 95                 }
 96                 if(a[i].num<0)
 97                     mp[-a[i].num-1][0]=cnt;
 98                 else
 99                     mp[a[i].num-1][1]=cnt;
100             }
101             build(1,cnt,1);
102             for(int i=0; i<n; i++)
103             {
104                 ins(mp[i][0],mp[i][1],1,i+1);
105             }
106             memset(flag,0,sizeof(flag));
107             ans=0;
108             sovle(1);
109             printf("%d
",ans);
110         }
111     }
112     return 0;
113 }
View Code
原文地址:https://www.cnblogs.com/wang-ya-wei/p/6013978.html