Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 68924 | Accepted: 19861 |
Description
- 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
Output
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
題目大意:給出一些個區間,在這些區間上貼海報,後貼的海報可能會覆蓋前面的海報,問最後能看見幾個海報;
給出的數據只有1w對,但是每個區間的數字可能會很大,用線段樹維護的話,如果不離散化的話,可能無法維護。
離散化時要注意可能出現
[1,10] [1,5] [8,10]這樣普通離散化的話可能會變成[1,4] [1,2] [3,4]導致丟失中間區間,故對於相鄰的兩個數字的話如果差值大於1,就新插入一個新的元素。
離散化用unique,重複的元素被放在數組尾部。
代碼如下:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N=100005*5;
int l[N];
int r[N];
int num[N];
int tree[N];
int has[N];
int ans=0;
int res[N];
void push(int i)
{
if(tree[i]!=0)
{
tree[i*2+1]=tree[i*2]=tree[i];
tree[i]=0;
}
}
void build(int l,int r,int i)
{
tree[i]=0;
if(l==r)
return ;
int mid=(l+r)/2;
build(l,mid,i*2);
build(mid+1,r,i*2+1);
}
void updata(int l,int r,int L,int R,int i,int val)
{
if(l>=L&&r<=R)
{
tree[i]=val;
return ;
}
push(i);
int mid=(l+r)/2;
if(L<=mid)
updata(l,mid,L,R,i*2,val);
if(R>mid)
updata(mid+1,r,L,R,i*2+1,val);
}
void query(int l,int r,int i)
{
if(l==r)
{
if(has[tree[i]]==0&&tree[i]!=0)
{
ans++;
has[tree[i]]=1;
}
return ;
}
push(i);
int mid=(l+r)/2;
query(l,mid,i*2);
query(mid+1,r,i*2+1);
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
memset(has,0,sizeof(has));
scanf("%d",&n);
int tot=0;
for(int i=1;i<=n;i++)
{
scanf("%d %d",&l[i],&r[i]);
num[++tot]=l[i];
num[++tot]=r[i];
}
sort(num+1,num+tot+1);
tot=unique(num+1,num+tot+1)-num-1;
for(int i=tot;i>=2;i--)
{
if(num[i]-num[i-1]>1)
num[++tot]=num[i]-1;
}
sort(num+1,num+tot+1);
build(1,tot,1);
for(int i=1;i<=tot;i++)
res[i]=i;
for(int i=1;i<=n;i++)
{
int indexl=upper_bound(num+1,num+tot+1,l[i])-num-1;
int indexr=upper_bound( num+1,num+tot+1,r[i])-num-1;
int le=res[indexl];
int ri=res[indexr];
updata(1,tot,le,ri,1,i);
}
ans=0;
query(1,tot,1);
printf("%d\n",ans);
}
return 0;
}