poj 2528 Mayor's posters

Mayor’s posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 59553 Accepted: 17255
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
Source

Alberta Collegiate Programming Contest 2003.10.18
這題大致來說就是線段樹+區間離散化。
但是離散化的時候有許多細節需要注意。
下面說一說Discuss裏面一個大神關於離散化需要注意的一些事情:
1.我建的是段樹,也就是:
|_|_|__|
1 2 3
這樣子的。
2,3,4那組數據中比較有爭議的一組是:
3
5 6
4 5
6 8
如果不離散化直接算覆蓋的話,是這樣的:(暫且忽略前面的1-4..)
… |_|_|__|
6 7 8
|_|_|
4 5
|_|_|
5 6
顯然答案爲2啦。(從頂往底看,注意輸入順序)
然後離散化之後,
如果不注意數值相同,就會變成這樣:
3 4
1 2
5 6
數值相同後離散就是這樣:
2 3
1 2
3 4
|_|_|
3 4
|_|_|
1 2
|_|_|
2 3
這就對了。

但是,離散化得再注意到下面這個問題:
1 10
1 3
6 10
這組數據,不離散化是這樣的:
|_|_|_|_|__|
6 7 8 9 10
|_|_|__|
1 2 3
|_|_|_|_|_|_|_|_|_|_|
1 2 3 4 5 6 7 8 9 10
顯然答案爲3.
如果我們使用上面的“注意到數值相同的離散化”,結果是這樣的:
1 4
1 2
3 4
畫出來是這樣的:
|_|_|
3 4
|_|_|
1 2
|_|_|_|_|
1 2 3 4
然後坑就出現了..
我的解決方案是,

|_|_|
↓ 3 4
|_|_|
1 2
|_|_|_|_|
1 2 3 4

在離散化時出現了海報右端序號在前,左端序號在後,且序號不等(也就是離散後會相鄰的情況):

|_|_|
↓ 3 4
|_|_|
1 2
這兩個就屬於離散化後相鄰的情況
我加上1:
變成這樣:
1 5
1 2
4 5
|_|_|
4 5
|_|_|
1 2
|_|_|_|_|__|
1 2 3 4 5
可能會有人問,要是是這樣的呢?
3
1 4
1 2
3 4
本來就相鄰的情況呢?
這樣的話,提取原來的序號,比較原本是否就相鄰即可。
可以去他的博客裏看看http://blog.moe.cn.com/1351.htm
下面說一下我的離散化過程(也是一位學長教的)
例如區間[2,5]我們在處理時可以寫成[2,6),之後用2 6,這兩個數進行離散化操作,變成[1,2),因爲線段樹是對閉區間進行操作,所以在更新數據時改成[1,1]就好了,相當於把左區間整體右移了一位,所以上述第二種情況就可以避免了,這樣操作的話需要注意顏色下放時退出遞歸的條件。
最後一個坑,n有可能等於0;

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<set>
using namespace std;
#define F(x,a,b) for (int x=a;x<=b;x++)
#define MAXN 20005
#define me(x) memset(x,0,sizeof(x))
struct p{int l,r;}re[20*MAXN],rel[20*MAXN];
struct pp{int l,r,val;}tree[20*MAXN];
set<int>a;
int b[20*MAXN];
int color[20*MAXN];
int ans;
void creatree(int i,int l,int r)
{
  tree[i].r=r;tree[i].l=l;tree[i].val=0;
  if (l==r) return;
  int mid=(l+r)>>1;
  creatree(i<<1,l,mid);
  creatree((i<<1)+1,mid+1,r);
}
void up(int i,int l,int r,int c)
{
   if (l==tree[i].l&&r==tree[i].r)
   {
      tree[i].val=c;
      return;
   }
   if (tree[i].val!=0)
   {
     tree[i<<1].val=tree[i].val;
     tree[(i<<1)+1].val=tree[i].val;
     tree[i].val=0;
   }
   int mid=(tree[i].l+tree[i].r)>>1;
   if (r<=mid) up(i<<1,l,r,c);
   if (l>mid) up((i<<1)+1,l,r,c);
   if (l<=mid&&r>mid) {up(i<<1,l,mid,c);up((i<<1)+1,mid+1,r,c);}
}
void _find(int i)
{
  if (tree[i].val!=0)
  {
    if (!color[tree[i].val])
    {
      ans++;
      color[tree[i].val]++;
    }
    return;
  }
  if (tree[i].l==tree[i].r) return;
  _find(i<<1);
  _find((i<<1)+1);
}
int _s(int p[],int len,int x)
{
  int l=0;int r=len;
  while (r-l>1)
  {
     int mid=(l+r)>>1;
     if (p[mid]>=x)
      r=mid;
     else l=mid;
  }
  return r;
}
int main()
{
  int T;
  scanf("%d",&T);
  while (T--)
  {
    int n; scanf("%d",&n);
    if (n==0) {printf("0\n");continue;}
    int cnt=0;
    a.clear();
    me(b);
    F(i,1,n)
    {
      scanf("%d%d",&re[i].l,&re[i].r);
      a.insert(re[i].l);a.insert(re[i].r+1);
    }
    set<int>::iterator it;cnt=0;
    for (it=a.begin();it!=a.end();it++)
    {
        b[++cnt]=*it;
    }
    F(i,1,n)
    {
      int p=_s(b,cnt,re[i].l);
      int q=_s(b,cnt,re[i].r+1);
      rel[i].l=p;
      rel[i].r=q-1;
    }
    me(color);
    ans=0;
    creatree(1,1,cnt-1);
    F(i,1,n)
    {
      up(1,rel[i].l,rel[i].r,i);
    }
    _find(1);
    printf("%d\n",ans);
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章