离散化处理+Mayor's posters

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

 

1 5 1 4 2 6 8 10 3 4 7 10

 

 

Sample Output

 

4

先看一份实际上错误,但由于样例不好AC了的代码:

#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;
}

上边这份代码错了!!!!可能是poj并没有这个类似的样例:

3

3

1 10

1 4

6 10

上边方法hash之后对应的是1 2 3 4

就是1 4,1 2,3 4

这样子就成了2,实际上应该输出3.

解决方法是在不相邻的两个数字之间加一个数字,比如说上边hash之后的数字应该是  13 4 5 6  7 10.

用二分查出>=a[i]的第一个的位置。这样位置就少了。

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=10000+100;
int a[maxn],l[maxn],r[maxn];
#define lson l,mid,id*2
#define rson mid+1,r,id*2+1
int vis[maxn];
int lazy[maxn*4];
void pushdown(int id){
    if(lazy[id]){
        lazy[id*2]=lazy[id*2+1]=lazy[id];
        lazy[id]=0;
    }
}
void build(int l,int r,int id){
    if(l==r){
        lazy[id]=0;
        return ;
    }
    int mid=(l+r)/2;
    lazy[id]=0;
    build(lson);
    build(rson);
}
void update(int l,int r,int id,int L,int R,int color){
    if(l>=L && r<=R)
    {
        lazy[id]=color;
        ///sum[id]=color;
     ///   cout<<"l:"<<l<<"    "<<"r:"<<r<<endl;
        return;
    }
    pushdown(id);
    int mid=(l+r)/2;
    if(L<=mid){
        update(lson,L,R,color);
    }
    if(R>mid){
        update(rson,L,R,color);
    }

}
int query(int l,int r,int id,int L,int R){
    int ans=0;
    ///if(l>=L &&r<=R){
    if(l==r){
        int lala=lazy[id];
        if(!vis[lala] &&lala!=0 )
        {
            ans++;
            vis[lala]=1;
           /// cout<<"ans:"<<ans<<endl;

        }
          return ans;

    }
    if(lazy[id]!=0)
        pushdown(id);
    int mid=(l+r)/2;
    if(L<=mid){
        ans+=query(lson,L,R);
    }
    if(R>mid){
        ans+=query(rson,L,R);
    }
    return ans;
}
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--){
        memset(vis,0,sizeof(vis));
        scanf("%d",&n);
        int cnt=0;
        for(int i=1;i<=n;i++){
            scanf("%d%d",&l[i],&r[i]);
            a[++cnt]=l[i];
            a[++cnt]=r[i];
        }
        sort(a+1,a+1+cnt);
        int cnt2=0;
        a[++cnt2]=a[1];
        for(int i=1;i<cnt;i++){
            if(a[i+1]!=a[i])
                a[++cnt2]=a[i+1];
        }
       /* for(int i=1;i<=cnt2;i++){
            cout<<a[i]<<"&&&"<<endl;
        }
        */
        ///for(int j = 1; j < tmp; ++j)

		int tmp=cnt2;
        for(int i=1;i<=tmp;i++){
            if(a[i+1]-a[i]>1)
                a[++cnt2]=a[i]+1;
        }
        sort(a+1,a+1+cnt2);
        /*for(int i=1;i<=cnt2;i++){
            cout<<a[i]<<"  ";
        }
        cout<<endl;
        */
        build(1,cnt2,1);
      ///  cout<<"cnt2:"<<cnt2<<endl;
        for(int i=1;i<=n;i++){
            int X=lower_bound(a+1,a+1+cnt2,l[i])-a;
            int Y=lower_bound(a+1,a+1+cnt2,r[i])-a;
   ///         cout<<X<<"****"<<Y<<endl;
            update(1,cnt2,1,X,Y,i);
        }
       /* for(int i=1;i<=13;i++){
            cout<<"i:"<<i<<"    "<<lazy[i]<<endl;
        }
        */
        printf("%d\n",query(1,cnt2,1,1,cnt2));
    }
}
/*
33
3
1 10
1 4
6 10
*/

更新时间:2018.8.31. 9 :02

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值