OpenJ_Bailian - Mayor's posters(线段树 & 离散化)

OpenJ_Bailian - 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
题目链接

题目大意:市长选举,然后每个市长都会有自己的宣传海报,给你一面墙,强的长度给你,每一张海报的左右端点坐标也都告诉你,并且海报高度都一样(这个也没啥用),求最后你能看到几个人的海报。
这个的话我刚开始想不到跟线段树能扯上啥关系,虽然在学这个(看来还是学的不好),然后就是看题解,是需要离散化的,但是题解代码看不懂,让人头疼、、、
这个题目呢,我是借鉴了学长的代码,将所有的端点都拿出来,然后排序,去重,之后以这些点构成的线段建一棵树,然后遍历海报的时候,从后往前遍历然后,用一个数组来记录你的这一长度范围内的墙是否已经被完全覆盖了,如果已经覆盖了的话,那么就说明前面的海报一定被盖住了一部分或者全部,前面海报到这里直接return就行了,可以节省很多判断。

#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 4e5 + 5;
int flag;
int x[maxn];

struct node
{
    int l, r;
}p[maxn];

struct edge
{
    int l, r;
    int vis;
    int flag;
}e[maxn];

void build(int l, int r, int cur)
{
    e[cur].l = x[l],    e[cur].r = x[r];
    e[cur].vis = 0; e[cur].flag = 0;
    if(l + 1 == r)
    {
        e[cur].flag = 1;
        return ;
    }
    int m = (l + r) >> 1;
    build(l, m, cur << 1);
    build(m, r, cur << 1  | 1);
}

void pushup(int cur)
{
    e[cur].vis = e[cur << 1].vis & e[cur << 1 | 1].vis;
}

void update(int ll, int rr, int cur)
{
    if(e[cur].vis)  return ;
    int l = e[cur].l, r = e[cur].r, m = (l + r) >> 1;
    if(ll <= l && r <= rr)
    {
        e[cur].vis = 1;
        flag = 1;
        return ;
    }
    if(e[cur].flag) return ;
    if(l + 1 == r)  return ;
    if(ll <= m) update(ll, rr, cur << 1);
    if(m <= rr) update(ll, rr, cur << 1 | 1);
    pushup(cur);
}

int main()
{
    int t, n, num;
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &p[i].l, &p[i].r);
            p[i].r++;
            x[2 * i - 1] = p[i].l, x[2 * i] = p[i].r;
        }
        sort(x + 1, x + n * 2 + 1);
        num = unique(x + 1, x + 1 + n * 2) - x - 1;
        build(1, num, 1);
        int ans = 0;
        for(int i = n; i >= 1; i--)
        {
            flag = 0;
            update(p[i].l, p[i].r, 1);
            ans += flag;
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40788897/article/details/81570448