poj 2528 Mayor's posters(线段树+离散化)

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 i 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 i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+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

题解:
n(n<=10000) 个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000) 。求出最后还能看见多少张海报。

由于线段的数据范围太大,所以如果这么开数据一定会mle,我们会发现其实数据一共就10000*2个点,没必要开这么大,我们只需记录线段的相对大小就可以了,因此需要用到离散化~
由于没学离散化外加一直无心学习~~~emmm浪费了好多天

离散化见这~
https://blog.csdn.net/ling_wang/article/details/81707676

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

const int N = 10010;//由于将线段大小离散化了,所以最大值就变成有多少个点
int ha[10000010];//记录每个点的哈希值
int x[N<<1];//离散化用的备份,记录所有线段的左右端点大小

struct node
{
    int l, r;//记录左右端点
    bool flag;//记录该线段是否被覆盖
}tree[N<<3];//正常的线段树不是开到4倍吗,,,为啥这里要开到8倍

struct post
{
    int l, r;//记录离散化前的原始数据,即所有线段左右端点大小
}po[N];

void build(int l, int r, int rt)//建树
{
    tree[rt].l = l; tree[rt].r = r; tree[rt].flag = 0;
    if(l == r)      return;
    int m = (l+r)>>1;
    build(l, m, rt<<1);
    build(m+1, r, rt<<1|1);
}

int made(int l, int r, int rt)
{
    if(tree[rt].flag)//如果已被覆盖,该数据就不记录
        return 0;
    else if(tree[rt].l == l && tree[rt].r == r){//如果该数据未被覆盖,就记录加一,且进行标记
        tree[rt].flag = 1;
        return 1;
    }
    int m = (tree[rt].l + tree[rt].r) >> 1;
    bool re;//记录当所求线段与当前线段不同时,就向下递归的子树的覆盖情况
    if(m >= r)    re = made(l, r, rt<<1);//如果该线段全被位于当前线段的左子树
    else if(m < l)   re = made(l, r, rt<<1|1);//如果该线段全位于当前线段的右子树
    else{//如果该线段覆盖了当前线段的两个子树,就都进行遍历
        bool flag1 = made(l, m, rt<<1);
        bool flag2 = made(m+1, r, rt<<1|1);
        re = flag1 || flag2;//只要有一个子树没被覆盖,当前线段就没被覆盖
    }
    if(tree[rt<<1].flag == 1 && tree[rt<<1|1].flag == 1)
        tree[rt].flag = 1;//如果两个子树都被覆盖了,说明当前线段也被覆盖了
    return re;
}

int main()
{
    int t, i, cnt, n;
    scanf("%d", &t);
    while(t--){
        cnt = 0;
        scanf("%d", &n);
        for(i = 0; i < n; i++){
            scanf("%d%d", &po[i].l, &po[i].r);
            x[cnt++] = po[i].l;
            x[cnt++] = po[i].r;
        }
        //离散化处理
        sort(x, x+cnt);
        cnt = unique(x, x+cnt) - x;
        for(i = 0; i < cnt; i++){
            ha[x[i]] = i;
        }
        build(0, cnt-1, 1);
        int s = 0;
        for(i = n-1; i >=0; i--){//从覆盖在最上面的线段往下寻找
            if(made(ha[po[i].l], ha[po[i].r], 1))
                s++;
        }
        printf("%d\n", s);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值