(线段树-区间覆盖-离散化)D - Mayor's posters

POJ - 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.

题意为给出1-10000000长度的墙,有1<=n<=10000的海报张贴在墙上,因为海报有先后顺序去张贴,故有的海报会被全部覆盖,求最后能看到的海报数量。
原以为是我没学过的扫描线,结果是区间覆盖,最后求全区间内不同数量的数的数量
不能直接离散化,需要两遍离散化,第二次需要处理如果一个这个数比前一个数大于1,则新添一个数,值为前一个数+1。

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

typedef long long ll;
const int maxn = 10000 + 4;
struct SegementTree {
    int tre[maxn << 4], vis[maxn << 4], ans;
    SegementTree() {
        memset(tre, -1, sizeof(tre));
        memset(vis, 0, sizeof(vis));
        ans = 0;
    }
    void PushDown(int rt) {
        if(tre[rt] == -1) return;
        tre[rt << 1] = tre[rt << 1 | 1] = tre[rt];
        tre[rt] = -1;
    }
    void Modify(int rt, int L, int R, int l, int r, int val) {
        if(L <= l && r <= R) {
            tre[rt] = val;
            return;
        }
        PushDown(rt);
        int mid = l + r >> 1;
        if(mid >= R) Modify(rt << 1, L, R, l, mid, val);
        else if(mid < L) Modify(rt << 1 | 1, L, R, mid + 1, r, val);
        else Modify(rt << 1, L, R, l, mid, val), Modify(rt << 1 | 1, L, R, mid + 1, r, val);
    }
    void Query(int rt, int l, int r) {
        if(!vis[tre[rt]] && tre[rt] != -1) {
            ans++;
            vis[tre[rt]] = true;
        }
        if(l == r) return;
        PushDown(rt);
        int mid = l + r >> 1;
        Query(rt << 1, l, mid);
        Query(rt << 1 | 1, mid + 1, r);
    }
};
int D[maxn << 2], L[maxn << 1], R[maxn << 1];
int main() 
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t, n;
    cin >> t;
    while(t--) {
        cin >> n;
        int tot = 0, tmp;
        SegementTree Tre;
        for(int i = 0; i < n; ++i) {
            cin >> L[i] >> R[i];
            D[tot++] = L[i];
            D[tot++] = R[i];
        }
        sort(D, D + tot);
        tot = unique(D, D + tot) - D;
        tmp = tot;
        for(int i = 1; i < tmp; ++i) {
            if(D[i] > D[i - 1] + 1) 
                D[tot++] = D[i - 1] + 1;
        }
        sort(D, D + tot);
        for(int i = 0; i < n; ++i) {
            int x = lower_bound(D, D + tot, L[i]) - D;
            int y = lower_bound(D, D + tot, R[i]) - D;
            Tre.Modify(1, x, y, 0, tot - 1, i);
        }
        Tre.Query(1, 0, tot - 1);
        cout << Tre.ans << endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值