poj2528解题报告 Mayor's posters


Mayor's posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 33152   Accepted: 9641

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
水题,就是求重叠后的个数,肯定要用线段树来做,要不然肯定要超时,先到的数,用后来的数覆盖,到最后计算,最后一次的,海报是哪一个,加起来,就可以了!当然,还要用到一个小技巧,就是要压缩路径,这题在
例子一:1-10 1-4 5-10
例子二:1-10 1-4 6-10
普通离散化后都变成了[1,4][1,2][3,4]
线段2覆盖了[1,2],线段3覆盖了[3,4],那么线段1是否被完全覆盖掉了呢?
例子一是完全被覆盖掉了,而例子二没有被覆盖

 

为了解决这种缺陷,我们可以在排序后的数组上加些处理,比如说[1,2,6,10] 如果相邻数字间距大于1的话,在其中加上任意一个数字,比如加成[1,2,3,6,7,10],然后再做线段树就好了,还是用了延时标记!

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 11111
int le[N],ri[N],l[N<<4],lr[N<<4],m,cnt;
bool flag[N<<4];
int  fin(int x)//找压缩点
{
      int s=0,e=m-1;
      while(s<=e)//二分查找相应的点
      {
            int mid=(s+e)>>1;
            if(lr[mid]==x)return mid;
            if(lr[mid]<x) s=mid+1;
            else e=mid-1;
      }
      return -1;
}
void update(int num ,int s,int e,int a,int b,int c)
{
      if(a<=s&&b>=e)
      {
            l[num]=c;
            return ;
      }
      if(l[num]!=-1)//延时标记,向下传递
      {
          l[num<<1]=l[num<<1|1]=l[num];
          l[num]=-1;//更新为已更新状态
      }
      int mid=(s+e)>>1;
      if(mid>=a)update(num<<1,s,mid,a,b,c);
      if(mid<b)update(num<<1|1,mid+1,e,a,b,c);

}
void query(int num ,int s,int e)
{

     if(l[num]!=-1)
     {
           if(!flag[l[num]]) cnt++;//找到该段的海报
           flag[l[num]]=true;
           return ;
     }
     if(s==e)
      return ;
     int mid=(s+e)>>1;
     query(num<<1,s,mid);
     query(num<<1|1,mid+1,e);
}
int main()
{
    int t,n,i,nn;
    scanf("%d",&t);
    while(t--)
    {
          scanf("%d",&n);
          for(i=0,nn=0;i<n;i++)
          {
                scanf("%d%d",&le[i],&ri[i]);
                lr[nn++]=le[i];
                lr[nn++]=ri[i];
          }
          sort(lr,lr+nn);
          for(i=0,m=0;i<nn;i++)//压缩
          {
             if(lr[i]!=lr[i-1])lr[m++]=lr[i]; //去掉重点
          }
          for(i=m-1;i>=0;i--)
          {
                if(lr[i]!=(lr[i-1]+1))lr[m++]=lr[i]+1;//间距大于一就加一个结点
          }
          sort(lr,lr+m);
          memset(l,-1,sizeof(l));//建树,初始化为-1
          for(i=0;i<n;i++)//更新结点
          {
                int s=fin(le[i]);//找到该点所对应的压缩点
                int e=fin(ri[i]);
                update(1,0,m,s,e,i);

          }
          memset(flag,false,sizeof(flag));//初始化每个线段为没找状态
          cnt=0;//初始化结点为0
          query(1,0,m);
          printf("%d\n",cnt);

    }
    return 0;
}



原文地址:https://www.cnblogs.com/dyllove98/p/3132847.html