POJ2528-Mayor's posters(线段树+离散化 or 分治+dfs)

题目链接

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.
http://poj.org/images/2528_1.jpg

Sample Input

1

5

1 4

2 6

8 10

3 4

7 10

Sample Output

4


题意

在一面墙上贴海报,先贴的海报会被后贴的盖住,问最后能看到多少不同的海报。如果海报不完整,也算,但是完全被遮住了,就不算。

思路

线段树维护一个数组,tree[i].w表示当前维护的区间的情况。

tree[i].w = -1 表示当前区间没有海报,或者不止一个海报。

tree[i].w = n (n为正整数)表示当前区间被一张海报覆盖,即只有一张海报,n表示海报的编号。

query的过程,如果是w=n,说明有n这个编号的海报,vis[]数组标记一下,避免重复,然后返回1。如果w=0,且是叶子节点,则停止;如果不是叶子节点,则继续递归。

初始化建树的时候,要把w全部设为-1,所有节点。

离散化

1000 0000太大,建不了数组,但是1 0000个海报,最多只有2 0000个点,就是说数组中很多开了是完全没用到的,所以建一个1-1000 0000到1-2 0000的映射,就可以开数组了。把无限空间中有限的个体映射到有限的空间中去。

具体做法: 线段树离散化步骤

参考博客:链接

关于开数组大小的问题:Max=10010;tree[Max*12];12是3*4,3是算法的问题,本来应该是2,但是有坑的地方。

1
3
2 4
1 2
4 5

应该是3,但没有处理就会是2。因为有的位置被省略了,所以每次大于1的两个数之间都要加上一个数。所以本来海报的两个边本来是Max*2,如果两个数之间都加上一个数,就是Max*3,然后线段树要开4倍空间的大小,所以是12。

vis[Max*3],a[Max*3],和上面3一样。

方法2:分治+DFS

另一种方法是反向考虑,后贴的海报一定在前面,所以从后往前了话,贴的海报就一定贴上了,该位置就不能再贴别的海报。如果能贴则贴,贴完后还有位置就递归。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define  ll long long
using namespace std;

struct node
{
    int l,r,w;
};
const int Max=10010;
node tree[Max*12];
int vis[Max*3],init[Max][2],a[Max*3];

void build(int k,int l,int r)
{
    tree[k].l = l; tree[k].r = r; tree[k].w = -1;
    if(l==r){
        tree[k].w = -1;
        return;
    }
    int mid = (l + r)/2;
    build(k*2,l,mid);
    build(k*2+1,mid+1,r);
}

void pushdown(int k)
{
    tree[k*2].w = tree[k*2+1].w = tree[k].w;
    tree[k].w = -1;
}

void update(int k,int l,int r,int v) //区间更新
{
    if(l>tree[k].r||r<tree[k].l) return;
    if(l<=tree[k].l&&tree[k].r<=r){
        tree[k].w = v;
        return;
    }
    if(tree[k].w!=-1) pushdown(k);
    update(k*2,l,r,v);
    update(k*2+1,l,r,v);
}

int query(int k)
{
    if(tree[k].w!=-1){
       if(!vis[tree[k].w]){
            vis[tree[k].w] = 1;
            return 1;
        }
        else return 0;
    }
    if(tree[k].l==tree[k].r){//如果是叶子节点,退出
        return 0;
    }
    return query(k*2) + query(k*2+1);
}

int main()
{
    int T,N,tot;
    scanf("%d",&T);
    while(T--){
        tot = 0;
        memset(vis,0,sizeof(vis));
        memset(init,0,sizeof(init));

        scanf("%d",&N);
        for(int i=0;i<N;i++){
            scanf("%d%d",&init[i][0],&init[i][1]);
            a[tot++] = init[i][0];
            a[tot++] = init[i][1];
        }
        sort(a,a+tot);
        int m = unique(a,a+tot)-a;
        for(int i=m-1;i>=1;i--){
            if(a[i]-a[i-1]>1)
                a[m++] = a[i]-1;
        }
        build(1,1,m);
        sort(a,a+m);
        for(int i=0;i<N;i++){
            int x = lower_bound(a,a+m,init[i][0])-a+1;//树定义的下标从1开始,所以+1
            int y = lower_bound(a,a+m,init[i][1])-a+1;
            update(1,x,y,i+1);//i+1表示从1到N
        }
        printf("%d\n",query(1));
    }
}
#include <cstring>
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

bool vis[10010];
int c,n,ans;
int st[10010],ed[10010];

inline void dfs(int l,int r,int x)
{
    if(!x) return;
    if(st[x]<=r&&l<=ed[x]){//(st[x],ed[x])和(l,r)有交集
        if(!vis[x]){
            vis[x] = true; ans++;
        }
        if(l<st[x]) dfs(l,st[x]-1,x-1);
        if(r>ed[x]) dfs(ed[x]+1,r,x-1);
    }
    else dfs(l,r,x-1);
}

int main()
{
    int t; scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
            scanf("%d%d",&st[i],&ed[i]);
        }
        memset(vis,0,sizeof(vis));
        ans = 0;
        dfs(1,10000000,n);
        printf("%d\n",ans);
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值