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
3
1 10
1 3
6 10
画图可以知道我们可以看到三幅海报,输出应该为3。
但是我们来看看离散化的成果:
1->1 3->2 6->3 10->4
可以看到2,3直接相邻,这让原本的不相邻的点相邻了,自然也就覆盖掉了原本应该有的那个区段。我们采取的解决方案是,在所有相邻但不属于同一张海报的两个点直接加上一个虚点。这样就不会出现这个问题了。
但是!!!!!!!!!
本题并没有考虑到这个问题。所以我们如果按照上述的方式就会WA
也就是上面这个例子poj要求的输出是2。也就是说,poj要AC的话,直接把端点离散去重就可以了,不用考虑这种两个端点被遮住但中间有部分没有被遮住的情况。
本题的艺术是:讲错就错
顺便记录一下,vector的去重方式是:
sort(V.begin(),V.end());
V.erase(unique(V.begin(),V.end()),V.end());
AC代码:
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define md(l,r) (l+r)>>1
#define rson(x) x<<1|1
#define lson(x) x<<1
#define endl '\n'
#define sc(x) scanf("%d",&x)
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const int size=1e5+5;
int Ledge[size],Redge[size];
int lid[size],rid[size];
vector<int > V;
typedef long long LL;
struct node{
int l,r;
LL sum;
LL tag;
}tree[size<<2];
void build(int k,int l,int r)
{
tree[k].l=l,tree[k].r=r;
if(l==r)
{
tree[k].sum=0;
return ;
}
int mid(md(l,r));
build(lson(k),l,mid);
build(rson(k),mid+1,r);
}
void change(int k)
{
if(tree[k].l!=tree[k].r)
{
int ls=lson(k),rs=rson(k);
tree[ls].sum=tree[k].tag;
tree[rs].sum=tree[k].tag;
tree[ls].tag=tree[k].tag;
tree[rs].tag=tree[k].tag;
}
tree[k].tag=0;
}
int query(int k,int l,int r)
{
LL ans=inf;
if(tree[k].tag) change(k);
if(tree[k].l==l&&tree[k].r==r)
{
return tree[k].sum;
}
int mid=md(tree[k].l,tree[k].r);
if(r<=mid) ans=query(lson(k),l,r);
else if(l>=mid+1) ans=query(rson(k),l,r);
else ans=min(query(lson(k),l,mid),query(rson(k),mid+1,r));
return ans;
}
void add(int k,int l,int r,LL x)
{
if(tree[k].tag) change(k);
if(tree[k].l==l&&tree[k].r==r)
{
tree[k].sum=x;
tree[k].tag=x;
return ;
}
int mid=md(tree[k].l,tree[k].r);
if(l>=mid+1) add(rson(k),l,r,x);
else if(r<=mid) add(lson(k),l,r,x);
else add(lson(k),l,mid,x),add(rson(k),mid+1,r,x);
tree[k].sum=min(tree[lson(k)].sum,tree[rson(k)].sum);
}
int main()
{
int t;
sc(t);
int ans=0;
while(t--)
{
int n;
ans=0;
V.clear();
memset(tree,0,sizeof(tree));
sc(n);
for(int i=0;i<n;i++)
{
sc(Ledge[i]),sc(Redge[i]);
V.push_back(Ledge[i]),V.push_back(Redge[i]);
}
sort(V.begin(),V.end());
V.erase(unique(V.begin(),V.end()),V.end());
for(int i=0;i<n;i++) lid[i]=lower_bound(V.begin(),V.end(),Ledge[i])-V.begin()+1;
for(int i=0;i<n;i++) rid[i]=lower_bound(V.begin(),V.end(),Redge[i])-V.begin()+1;
build(1,1,V.size()+1);
for(int i=n-1;i>=0;i--)
{
if(query(1,lid[i],rid[i])==0)
{
add(1,lid[i],rid[i],i);
ans++;
}
}
cout<<ans<<endl;
}
return 0;
}