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

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.

题目链接
题意:一堵墙,贴n张海报,给你海报左右端点的坐标,然后求最终还能看到几张海报

这个是线段树离散化很经典的一个题目了。因为坐标数太大了,所以就记录海报的左右端点,排个序,去个重,因为后续你最多也就是会出现这些点,然后再对这些点建树就行了。这里我的方法是每一个节点都是一条线段。然后倒着往上放海报,因为你最后放上的海报一定会挡住前面放的,这样,如果放过就把这段区间标记一下,如果某一段区间已经标记放过,那么之前的海报放也是肯定会被盖住了,直接返回就行了,每次,只要判断能不能有空间贴就行了。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX_SIZE = 1e4+5;

struct node
{
    int left, right;    //记录左右端点
    int vis, flag;  //vis记录这整个区间是否都已经被覆盖过
}tree[MAX_SIZE<<4];
int point[MAX_SIZE<<1], l[MAX_SIZE], r[MAX_SIZE];
int n, num, flag;

void Build(int left, int right, int cur)
{
    tree[cur].left = point[left], tree[cur].right = point[right];
    tree[cur].vis = 0;  tree[cur].flag = 0;
    if(left+1 == right)
    {
        tree[cur].flag = 1;
        return ;
    }
    int mid = (left+right)>>1;
    Build(left, mid, cur<<1);
    Build(mid, right, cur<<1|1);
    return ;
}

void Push_Up(int cur)
{
    tree[cur].vis = tree[cur<<1].vis & tree[cur<<1|1].vis;  //;两个子区间都被覆盖了才说明整个的被覆盖
    return ;
}

void Up_Date(int Left, int Right, int cur)
{
    if(tree[cur].vis)   return ;    //如果这个区间最后会贴上海报,也就是已经记录过了,那么就可以直接跳过了
    if(Left <= tree[cur].left && tree[cur].right <= Right)  //如果这片区域没有被记录过,并且被这张海报盖住,那么flag=1,说明又能多看见一张海报
    {
        tree[cur].vis = 1;
        flag = 1;
        return ;
    }
    if(tree[cur].flag)  return ;
    if(tree[cur].left+1 == tree[cur].right) return ;
    if(Left <= tree[cur<<1].right)  Up_Date(Left, Right, cur<<1);
    if(Right >= tree[cur<<1|1].left)    Up_Date(Left, Right, cur<<1|1);
    Push_Up(cur);
    return ;
}

int main()
{
    int T, n;
    cin >> T;
    while(T--)
    {
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d%d", &l[i], &r[i]);
            point[i*2-1] = l[i], point[i*2] = ++r[i];
        }
        sort(point+1, point+1+n*2);
        int Point_Num = 0;
        //去重
        for(int i = 1; i <= 2*n; i++)
            if(point[i] != point[Point_Num])
                point[++Point_Num] = point[i];
        //去重
        Build(1, Point_Num, 1);
        int ans = 0;
        for(int i = n; i >= 1; i--)
        {
            flag = 0;
            Up_Date(l[i], r[i], 1);
            ans += flag;
        }
        printf("%d\n", ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_40788897/article/details/89186758
今日推荐