离散化处理+Mayor's posters

来源:互联网 发布:复制淘宝店铺违规吗 编辑:程序博客网 时间:2024/06/16 13:34

Mayor's posters

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 90   Accepted Submission(s) : 23
Problem 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 l<sub>i</sub> 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<sub>i</sub> <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l<sub>i</sub>, l<sub>i</sub>+1 ,... , ri.
 

Output
For each input data set print the number of visible posters after all the posters are placed. < br>< br>The picture below illustrates the case of the sample input.< br><img src=images/2528_1.jpg>
 

Sample Input
151 42 68 103 47 10
 

Sample Output
4

 





#include<iostream>
#include<algorithm>
#include<stdio.h>


#define M 10010
using namespace std;
int x[M*8];
int lala[M*1000];
struct node2
{
    int l;
    int r;
}a[M*8];
struct node
{
    int l;int r;int flag;
}tree[M*8];
void build(int id,int l,int r)
{
        tree[id].l=l;
        tree[id].r=r;
        tree[id].flag=0;
         if(l==r)
        return;
    int m=(l+r)/2;
    build(id*2,l,m);
    build(id*2+1,m+1,r);
}


int query(int id,int L,int R)
{
    int result;
   if(tree[id].flag!=0)//因为我是倒着来的,所以在最后涂的一定不会被遮盖,所以如果这个位置标记不为0,则说明有广告了,不能露出来,返回0
    return 0;
   if(tree[id].l==L&&tree[id].r==R)
    {
        tree[id].flag=1;//此处有颜色了,忘记改了
        return 1;
    }
   int mid=(tree[id].l+tree[id].r)/2;
   if(R<=mid)
    result=query(id*2,L,R);
    else if(L>mid)
        result=query(id*2+1,L,R);
    else
    {
        result=query(id*2,L,mid)|query(id*2+1,mid+1,R);(??????)
    }
    if(tree[id*2].flag!=0&&tree[id*2+1].flag!=0)
        tree[id].flag=1;//孩子节点反馈父亲节点(这部分为什么不能省????)


    return result;
}
int main()
{
    int t,n;


    cin>>t;
    while(t--)
    {
        scanf("%d",&n);
        int cnt=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&a[i].l,&a[i].r);
            x[cnt++]=a[i].l;
            x[cnt++]=a[i].r;


        }
        sort(x,x+cnt);
        cnt=unique(x,x+cnt)-x;//消除相同的坐标
        for(int i=0;i<cnt;i++)
        {
            lala[x[i]]=i;//此处很巧妙,记录了位置,与下呼应
        }
        build(1,0,cnt-1);
        int ans=0;
        for(int i=n-1;i>=0;i--)
        {
            if(query(1,lala[a[i].l],lala[a[i].r]))
                ans++;
        }
        cout<<ans<<endl;


    }
    return 0;
}


原创粉丝点击