poj2528--Mayor's posters(線段樹+離散化)
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張海報,也就是說最多出現20000個點,所以可以使用離散化,將每個點離散後,重新對給出控制的區間,這樣區間最大就是1到20000.可以直接使用線段樹,成段更新,每次更新一個顏色,最後遍歷所有的段,將還可以遍歷的顏色儲存,統計
將-1 定義為沒有顏色,0代表有多種顏色,1到m代表各自的顏色,線段樹只要在0時向下深入,其他的直接統計顏色。
但是這個題中的離散化有問題,會擠掉中間的顏色,不過poj好像也是這麼做的,沒有考慮中間的顏色。
如 1 ,10 1,4 7,10 這三組數的正確結果應該是還可以看到3種顏色,但是如果直接排列點的話就會擠掉5到6這一種顏色。
真正的這種成段的離散化,一定要將兩個節點中還有值的話,要多加一個節點,代表兩個節點中間的值。
#include<stdio.h>
#include<string.h>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<queue>
#include<deque>
#include<math.h>
#define ll long long
#define ls rt<<1
#define rs rt<<1|1
#define maxn 22000
const int inf=0x3f3f3f3f;
int v[maxn<<2],t,n,k,sum;
int a[11000],b[11000];
int uu[maxn<<1];
using namespace std;
void pushup(int rt)
{
if(v[ls]&&v[rs])
v[rt]=1;
}
void update(int L,int R,int l,int r,int rt)
{
if(v[rt]) return;
if(L<=l&&r<=R)
{
if(!v[rt])
{
v[rt]=1;
k=1;
}
return;
}
int m=(l+r)>>1;
if(L<=m&&!v[ls]) update(L,R,l,m,ls);
if(R>m&&!v[rs]) update(L,R,m+1,r,rs);
pushup(rt);
}
int main()
{
scanf("%d",&t);
while(t--)
{
int ma=1;
scanf("%d",&n);
memset(v,0,sizeof(v));
sum=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i],&b[i]);
uu[i]=a[i];
uu[i+n]=b[i];
uu[i+2*n]=a[i]+1;
uu[i+3*n]=b[i]+1;
}
sort(uu,uu+4*n);
int si=unique(uu,uu+4*n)-uu;
for(int i=0;i<n;i++)
{
a[i]=upper_bound(uu,uu+si,a[i])-uu;
b[i]=upper_bound(uu,uu+si,b[i])-uu;
ma=max(ma,b[i]);
}
for(int i=n-1;i>=0;i--)
{
k=0;
update(a[i],b[i],1,ma,1);
if(k) sum++;
}
printf("%d\n",sum);
}
}