poj 2528 Mayor's posters 线段树成段更新+离散化

题目:

Mayor’s posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 67336 Accepted: 19470

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


题意:

在一个长方形的区域内贴海报,每个海报的高度都和区域高度相等。
给出海报的位置,要你告诉多少海报至少有一部分露在外面。


解法1:

如果一个海报i完全被盖住,那么一定有在它后面贴的海报能够将其完全覆盖。
也就是说如果先在线段树里面把后面的海报覆盖的区域表示出来,那么查询海报i覆盖的区域会发现,该区域正好完全覆盖。那么露在外面的海报数减1。
因为区域太宽,但是n不超过1W,故离散化。

补充一个,之前的代码是混过去的,数据不强,有个小问题已作
更正。
对于 [1,5] [1,2] [4,5] 这组数据,应该离散化为[1,5] [1,2] [4,5]
而不是[1,4] [1,2] [3,4] 否则会出错。

原因就是如果两个排序后相邻数离散化之前的差值大于1,那么在离散化时必须要在他们中间加1个数,让他们离散化后的仍大于1(实际为2)。


代码:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;

#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define lson  (ind<<1)
#define rson  (ind<<1|1)
#define MID   int mid=(le+ri)>>1
const int maxn=10000;

int n,border;
struct SEG
{
    int le,ri;
}seg[maxn+10];

struct A
{
    int ind,val;
    bool operator<(const A c)const
    {
        return val<c.val;
    }
}a[2*maxn+10];

void cope()//离散化
{
    sort(a,a+2*n);

    int nex=1;

    for0(i,2*n)
    {
        int ret;
        if(!i)  ret=nex++;
        else if(a[i].val!=a[i-1].val)
        {
            if(a[i].val-a[i-1].val>1) nex++;
            ret=nex++;
        }
        else ret=nex-1;

        int p=a[i].ind/2;
        if(a[i].ind&1) seg[p].ri=ret;
        else           seg[p].le=ret;
    }
    border=nex-1;

}
struct Node
{
    int le,ri,sum,lazy;
    void update()
    {
        sum=ri-le+1;
        lazy=1;
    }
};
struct SegMentTree
{
    Node C[16*maxn];
    void build(int ind ,int le,int ri)
    {
        C[ind].le=le,C[ind].ri=ri,C[ind].lazy=0; C[ind].sum=0;
        if(le==ri) return;

        MID;
        build(lson,le,mid);
        build(rson,mid+1,ri);
    }

    void pushDown(int ind)
    {
        if(C[ind].lazy)
        {
            C[lson].update();
            C[rson].update();
            C[ind].lazy=0;
        }
    }
    int query(int ind,int L,int R)
    {
        int le=C[ind].le,ri=C[ind].ri;
        if(L<=le&&ri<=R)
        {
            int ans=C[ind].sum;
            C[ind].update();
            return ans;
        }
        pushDown(ind);
        MID;
        int ans=0;
        if(L<=mid) ans+=query(lson,L,R);
        if(R>mid)  ans+=query(rson,L,R);

        C[ind].sum=C[lson].sum+C[rson].sum;
        return ans;

    }
}sgt;
void solve()
{
    sgt.build(1,1,border);
    int ans=n;
    for(int i=n-1;i>=0;i--)
    {
        int L=seg[i].le,R=seg[i].ri;
        int cnt=sgt.query(1,L,R);
        if(cnt==R-L+1)  ans--;

    }
    printf("%d\n",ans);
}
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for0(i,n)
        {
            scanf("%d%d",&a[2*i].val,&a[2*i+1].val);
            a[2*i].ind=2*i; a[2*i+1].ind=2*i+1;
        }
        cope();
        solve();
    }

    return 0;
}

解法2:

参考网上的解法。对于线段树中le==ri的结点只需标记color即可。
假设每张海报都具有不同的颜色,线段树维护了一个区域的情况。那么最后只需统计线段树中有多少不同颜色即可。因为query的本质是二分,所以在O(LlogL)的时间内一定能找到。

对于ri-le>1的结点,color值相当于是延迟标记。
这样表示只会用到pushDown操作,而用不到pushUp。

当一个延迟标记用过一次后,记得取消这个标记。


代码:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;

#define mem(a,x)  memset(a,x,sizeof a)
#define for0(a, n) for (int (a) = 0; (a) < (n); (a)++)
#define for1(a, n) for (int (a) = 1; (a) <= (n); (a)++)
#define lson  (ind<<1)
#define rson  (ind<<1|1)
#define MID   int mid=(le+ri)>>1
const int maxn=10000;

int n,border;
struct SEG
{
    int le,ri;
}seg[maxn+10];

struct A
{
    int ind,val;
    bool operator<(const A c)const
    {
        return val<c.val;
    }
}a[2*maxn+10];
bool vis[maxn+5];
void cope()//离散化
{
    sort(a,a+2*n);

    int nex=1;

    for0(i,2*n)
    {
        int ret;
        if(!i)  ret=nex++;
        else if(a[i].val!=a[i-1].val)
        {
            if(a[i].val-a[i-1].val>1) nex++;
            ret=nex++;
        }
        else ret=nex-1;

        int p=a[i].ind/2;
        if(a[i].ind&1) seg[p].ri=ret;
        else           seg[p].le=ret;
    }
    border=nex-1;

}
struct Node
{
    int le,ri,color;
    void update(int &c)
    {
        color=c;
    }
};
struct SegMentTree
{
    Node C[16*maxn];
    void build(int ind ,int le,int ri)
    {
        C[ind].le=le,C[ind].ri=ri,C[ind].color=0;
        if(le==ri) return;

        MID;
        build(lson,le,mid);
        build(rson,mid+1,ri);
    }

    void pushDown(int ind)
    {
        if(C[ind].color)
        {
            C[lson].update(C[ind].color);
            C[rson].update(C[ind].color);
            C[ind].color=0;//如果不取消,会重复更新子结点,造成错误。很重要。
            //对于C[ind].le!=C[ind].ri的结点,C[ind].color相当于是一种特殊的延迟标记。
        }
    }
    void update(int ind,int L,int R,int c)
    {
        int le=C[ind].le,ri=C[ind].ri;
        if(L<=le&&ri<=R)
        {
            C[ind].update(c);
            return;
        }
        pushDown(ind);
        MID;

        if(L<=mid) update(lson,L,R,c);
        if(R>mid)  update(rson,L,R,c);

    }
    int query(int ind,int L,int R)
    {
        int le=C[ind].le,ri=C[ind].ri;
        if(le==ri)
        {
            int tc=C[ind].color;
            if(tc&&!vis[tc])
            {
                vis[tc]=1;
                return 1;
            }
            return 0;
        }
        pushDown(ind);
        MID;
        return query(lson,le,mid)+query(rson,mid+1,ri);

    }
}sgt;
void solve()
{
    sgt.build(1,1,border);
    for0(i,n)
    {
        int L=seg[i].le,R=seg[i].ri;
        sgt.update(1,L,R,i+1);
    }
    mem(vis,0);
    printf("%d\n",sgt.query(1,1,border));
}
int main()
{
    int T;scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for0(i,n)
        {
            scanf("%d%d",&a[2*i].val,&a[2*i+1].val);
            a[2*i].ind=2*i; a[2*i+1].ind=2*i+1;
        }
        cope();
        solve();
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值