POJ 2528【线段树染色+离散化】

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.
Sample Input
1
5
1 4
2 6
8 10
3 4
7 10
Sample Output
4

题意:
给你一些线段,新的线段区间可能会覆盖之前的,问最后你能看到的不同线段个数;
分析:(有些染色问题用二进制表示,技巧性题目,当n比较小时可取)
对每个线段染不同色,最后统计你能看到的颜色种类数即可。如果这个区间范围不大,就是一个简单的染色问题,但是区间很大需要进行离散化处理后染色,就是把输入数据离散化,然后放到染色模板里就行了。

离散化:
比如[5, 100],中间的 -INF~4,6~99,101~INF空间浪费了,需要离散化操作,进行空间优化。

具体步骤:
[3, 10], [1, 4], [3, 8],把这些区间左右端点sort一下再去重:
排序后结果: 1 3 4 8 10
对应原区间: L2 L3 R2 R3 R1
生成新下标: 1 2 3 4 5

把排序后结果,相邻值大于1的插入一个值:
排序后结果:1 2 3 4 7 8 9 10
生成新下标: 1 2 3 4 5 6 7 8

查询新的染色区间进行updata:
[3, 10] -> [3, 8]
[1, 4] -> [1, 4]
[3, 8] -> [3, 6]
由于序列始终是递增的,原端点对应新端点的值查询,用二分即可;

#include <cstdio> 
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;

typedef long long LL;
const int mod = 24 * 60;
const int MAXN = 1e6 + 10;
int col[MAXN], num[MAXN];
int x[MAXN], y[MAXN], s[MAXN];
int ncol = -1;

void down(int root) {
    if(col[root] != -1) { //区间染色
        col[root << 1] = col[root << 1 | 1] = col[root];
        col[root] = -1;
    }
}

void updata(int root, int L, int R, int l, int r, int c) { //区间染色
    if(L >= l && R <= r) {
        col[root] = c;
        return ;
    }
    int mid = (L + R) >> 1;
    down(root);
    if(l <= mid) updata(root << 1, L, mid, l, r, c);
    if(r > mid) updata(root << 1 | 1, mid + 1, R, l, r, c);
}

void query(int root, int L, int R) { //区间染色
    if(L == R) {
        if(col[root] != -1 && col[root] != ncol){
            num[col[root]]++;
        }
        ncol = col[root];
        return;
    }
    int mid = (L + R) >> 1;
    down(root);
    if(L <= mid) query(root << 1, L, mid);
    if(R > mid) query(root << 1 | 1, mid + 1, R);
}

int bin(int L, int R, int x) { //有序进行二分
    while(L <= R) {
        int mid = (L + R) >> 1;
        if(s[mid] == x) return mid;
        if(s[mid] < x) L = mid + 1;
        else R = mid - 1;
    }
}

int main() {
    int n, t;  
    scanf("%d", &t);
    while(t--) {
        memset(col, -1, sizeof(col));
        memset(num, 0, sizeof(num));
        scanf("%d", &n);
        int p = 1;
        ncol = -1;
        for(int i = 1; i <= n; i++) {
            scanf("%d %d", &x[i], &y[i]);
            s[p++] = x[i];
            s[p++] = y[i];
        }
        sort(s + 1, s + p); //下面是离散化操作
        int u = 2;
        for(int i = 2; i < p; i++) { //去重
            if(s[i] != s[i - 1]) s[u++] = s[i];
        }
        for(int i = u - 1; i > 1; i--) { //插点
            if(s[i] - s[i - 1] > 1) s[u++] = s[i] - 1;
        }
        sort(s + 1, s + u);
        for(int i = 1; i <= n; i++) { //离散后,进行区间染色
            int L = bin(1, u - 1, x[i]);
            int R = bin(1, u - 1, y[i]);
            updata(1, 1, u - 1, L, R, i);
        }
        query(1, 1, u - 1); //统计颜色
        int ans = 0;
        for(int i = 1; i <= n; i++) {
            if(num[i]) ans++;
        }
        printf("%d\n", ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值