POJ 2528 Mayor's posters

Mayor's posters
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 65141   Accepted: 18824

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
解题思路,因为数据量太大,放入数组中进行离散化,端点值的下标就是要更新的区间,更新的值不同代表不同的海报,查询时,一个一个节点便利,如果一个节点存在 值,且标记值为false,说明该海报可以被看见,累加之后,标记值为true,然后在判断到该值时,直接跳过,因为该值之前已经判断过。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long LL;
const int maxn=111111;
bool hash[maxn];
int li[maxn],ri[maxn];
int pos[maxn*3];
int vis[maxn<<4];
int cnt;
void pushdown(int node)
{
    if(vis[node]!=-1)
    {
        vis[node<<1]=vis[node<<1|1]=vis[node];
        vis[node]=-1;
    }
}
void update(int ll,int rr,int val,int l,int r,int node)
{
    if(ll<=l && rr>=r)
    {
        vis[node]=val;
        return ;
    }
    pushdown(node);
    int mid=(l+r)>>1;
    if(ll<=mid) update(ll,rr,val,l,mid,node<<1);
    if(rr>mid) update(ll,rr,val,mid+1,r,node<<1|1);
}
void query(int l,int r,int node)
{
    if(vis[node]!=-1)
    {
        if(!hash[vis[node]]) cnt++;
        hash[vis[node]]=true;
        return ;
    }
    if(l==r) return ;
    int mid=(l+r)>>1;
    query(l,mid,node<<1);
    query(mid+1,r,node<<1|1);
}
int bin(int val,int n,int pos[])
{
    int l=0,r=n-1;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(pos[mid]==val)return mid;
        if(pos[mid]<val) l=mid+1;
        else r=mid-1;
    }
    return -1;
}
int main()
{
    int t,n,l,r;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int num=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&li[i],&ri[i]);
            pos[num++]=li[i];
            pos[num++]=ri[i];
        }
        sort(pos,pos+num);
        int m=1;
        for(int i=1;i<num;i++)
        {
            if(pos[i]!=pos[i-1]) pos[m++]=pos[i];
        }
        for(int i=m-1;i>0;i--)
        {
            if(pos[i]!=pos[i-1]+1)pos[m++]=pos[i-1]+1;
        }
        sort(pos,pos+m);
        memset(vis,-1,sizeof(vis));
        for(int i=0;i<n;i++)
        {
            l=bin(li[i],m,pos);
            r=bin(ri[i],m,pos);
            update(l,r,i,0,m,1);
        }
        cnt=0;
        memset(hash,false,sizeof(hash));
        query(0,m,1);
        printf("%d
",cnt);
    }
    return 0;
}
 
原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/7161610.html