POJ 2528

题目链接:http://poj.org/problem?id=2528

Time Limit: 1000MS Memory Limit: 65536K

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

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

Sample Output

4

题意:

现有一面墙,墙上有一些格子,从1~n标号,给出m张海报的左右边界值,表示从l到r的格子上贴了海报。

按顺序海报一层层贴,会有覆盖,求所有海报贴完后,整面墙上露出多少张海报(不一定要露出整张海报,露出一部分也算)。

题解:

首先想到的是,区间修改线段树,但是看到l与r的范围,显然不能直接建立那么大范围的线段树;

看到海报数量不超过10000,便考虑进行离散化;

离散化后,我们给树初始化建树为0,表示没有海报,后续按照海报的编号(1~m)进行区间修改;

最后查询每个格子,只要大于0,就代表这个格子上有海报,进行计数,最后即可得答案;

otherwise,就像discuss里说的那样,例如case:

1
3
1 10
1 3
6 10

如果进行普通的离散化,会出现问题:

原本3到6之间露出会露出海报1,但是普通离散化后,3被离散化为2,6被离散化为3,之间的间隔消失,就无法露出海报1,就会产生错误答案;

故进行离散化时,idx[]数组除了加入l与r值外,再加入l-1与r+1(当然,其实只加入l-1也是可以的),使得即使在离散化后,每个海报间有一定的间隙;

代码:

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<algorithm>
  4 #define MAXN 40000+5
  5 using namespace std;
  6 typedef long long ll;
  7 int m,a[MAXN];
  8 int idx[MAXN];//离散化索引
  9 struct Paint{int l,r;}paint[MAXN];
 10 struct Node{
 11     int l,r;
 12     int val,lazy;
 13     void update(ll x)
 14     {
 15         val=(r-l+1)*x;
 16         lazy=x;
 17     }
 18 }node[4*MAXN];
 19 void pushdown(int root)
 20 {
 21     if(node[root].lazy)
 22     {
 23         node[root*2].update(node[root].lazy);
 24         node[root*2+1].update(node[root].lazy);
 25         node[root].lazy=0;
 26     }
 27 }
 28 void pushup(int root)
 29 {
 30     node[root].val=max(node[root*2].val,node[root*2+1].val);
 31 }
 32 void build(int root,int l,int r)
 33 {
 34     node[root].l=l; node[root].r=r;
 35     node[root].val=0; node[root].lazy=0;
 36     if(l==r) node[root].val=a[l];
 37     else
 38     {
 39         int mid=l+(r-l)/2;
 40         build(root*2,l,mid);
 41         build(root*2+1,mid+1,r);
 42         pushup(root);
 43     }
 44 }
 45 void update(int root,int st,int ed,int val)
 46 {
 47     if(st>node[root].r || ed<node[root].l) return;
 48     if(st<=node[root].l && node[root].r<=ed) node[root].update(val);
 49     else
 50     {
 51         pushdown(root);
 52         update(root*2,st,ed,val);
 53         update(root*2+1,st,ed,val);
 54         pushup(root);
 55     }
 56 }
 57 int query(int root,int st,int ed)
 58 {
 59     if(ed<node[root].l || node[root].r<st) return 0;
 60     if(st<=node[root].l && node[root].r<=ed) return node[root].val;
 61     else
 62     {
 63         pushdown(root);
 64         ll a=query(root*2,st,ed);
 65         ll b=query(root*2+1,st,ed);
 66         pushup(root);
 67         return max(a,b);
 68     }
 69 }
 70 int main()
 71 {
 72     int t;
 73     scanf("%d",&t);
 74     while(t--)
 75     {
 76         scanf("%d",&m);
 77         memset(a,0,sizeof(a));
 78         int _size=0;
 79         for(int i=1;i<=m;i++)
 80         {
 81             scanf("%d%d",&paint[i].l,&paint[i].r);
 82             idx[_size++]=paint[i].l;
 83             idx[_size++]=paint[i].l-1;
 84             idx[_size++]=paint[i].r;
 85             idx[_size++]=paint[i].r+1;
 86         }
 87         sort(idx,idx+_size);
 88         _size=unique(idx,idx+_size)-idx;
 89         build(1,1,_size);//线段树建树
 90         for(int i=1;i<=m;i++)
 91         {
 92             int l=lower_bound(idx,idx+_size,paint[i].l)-idx+1;
 93             int r=lower_bound(idx,idx+_size,paint[i].r)-idx+1;
 94             //得到paint[i].l和r对应的离散化后的值
 95             update(1,l,r,i);
 96         }
 97         int cnt=0;
 98         for(int i=1;i<=_size;i++)//遍历墙上每个格子
 99         {
100             int tmp=query(1,i,i);
101             if(tmp>0) idx[cnt++]=tmp;//如果墙上贴了海报,就进行记录
102         }
103         sort(idx,idx+cnt);//排序后进行去重,去重后的数组的size即答案
104         printf("%d
",unique(idx,idx+cnt)-idx);
105     }
106 }

PS.代码里,idx[]数组在完成了离散化任务后,被我重复利用了一下,存了每个墙格子最后贴了海报几;

原文地址:https://www.cnblogs.com/dilthey/p/7485001.html