Mayor’s posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 54922 Accepted: 15935
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
题目链接:http://poj.org/problem?id=2528
题意:经典的海报覆盖线段树区间修改,查询题;
思路:
1.从后往前枚举poster,区间有0则ans++,再+1;【本代码思路】
2.从前往后枚举poster,区间覆盖,再query(1,maxn) 的颜色数;
*3.离散化技巧:
a. 1 <= li <= ri <= 10000000.而 1 <= n <= 10000. 即最多20000+的坐标值—–》离散化;
b. 普通离散化的区间问题:
3
1 10
1 4
6 10
普通离散化后id[1]=1,id[4]=2,id[6]=3.id[10]=4;
倒着枚举
但其实答案为3!!;
离散化时应把两相差大于1的坐标中加1个离散化后的点
for(int i=2;i<=tot;i++)
{
if(x[i]==x[i-1]) continue;
else if(x[i]-x[i-1]>1)
{
len+=2;
id[x[i]]=len;
//x[len]=x[i];
}else id[x[i]]=++len;
}
则上面的id [1]=1,id[4]=3,id[6]=5,id[10]=7;
代码:
#include<iostream>
#include<algorithm>
#include<map>
#include<stdio.h>
#include<string.h>
#define lson (id*2)
#define rson (id*2+1)
using namespace std;
bool tree[160055];
bool lazy[160055];
int n;
int flag=0;
int tot;
struct node{
int x, y;
}q[10005];
int x[40005];
map<int ,int >id;
int cmp(int x,int y)
{
return x<y;
}
void push_up(int id)
{
if(tree[lson]==1&&tree[rson]==1) tree[id]=1;
return;
}
void query(int id,int L,int R,int l,int r)
{
// cout<<L<<" "<<R<<endl;
if(l>R||r<L) return ;
if(l>=L&&r<=R&&tree[id]==1) return ;
if(L>=l&&R<=r)
{
if(tree[id]==0) flag=1;
tree[id]=1;
lazy[id]=1;
return ;
}
if(lazy[id]==1)
{
tree[lson]=tree[rson]=lazy[lson]=lazy[rson]=1;
lazy[id]=0;
}
int mid=(L+R)>>1;
if(l<=mid)
query(lson,L,mid,l,r);
if(r>mid) query(rson,mid+1,R,l,r);
push_up(id);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
tot=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&q[i].x,&q[i].y);
x[++tot]=q[i].x;
x[++tot]=q[i].y;
}
sort(x+1,x+1+tot);
int len=0;
// x[++len]=x[1];
id[x[1]]=++len;
for(int i=2;i<=tot;i++)
{
if(x[i]==x[i-1]) continue;
else if(x[i]-x[i-1]>1)
{
len+=2;
id[x[i]]=len;
//x[len]=x[i];
}else id[x[i]]=++len;
}
// for(int i=len;i>1;i--) if(x[i]-x[i-1]>1) x[++len]=x[i]-1;
// for(int i=1;i<=tot;i++)
// if(!id[x[i]])
// {
// id[x[i]]=++len;
// }
for(int i=1;i<=4*len;i++)
tree[i]=0,lazy[i]=0;
int ans=0;
for(int i=n;i>=1;i--)
{
flag=0;
// cout<<i<<" "<<q[i].x<<" "<<q[i].y<<endl;
query(1,1,len,id[q[i].x],id[q[i].y]);
if(flag) ans++;
// cout<<ans<<endl;
}
printf("%d\n",ans);
}
}