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

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

先把每条线段的单独存下来,同时把每个端点的值存在另一个数组里

来一发排序,sort即可,然后处理一下相邻数据的差超过1的情况。先解释一下为什么,线段树是以点的形式存储了线段,所以难免会有局限,比如区间[1, 4]和[5, 6], 在线段数里面其实是相连接的,也就是说中间没有空隙(注意!!!),按照普通的离散化,两个不连续的数离散化之后会是连续的数,比如[1, 4], [2, 8], [7, 10]这三条线段,离散后:[1, 3], [2, 5], [4, 6], 然后算算离散前后的结果,一个是3, 一个是2,所以说就要把相差超过1的数据的特点体现出来,使得其之间有空隙。方法就是在这两个数中间再加一个数。就是x[i - 1]++这个操作了,最后再来发sort就ok了
然后把数组中重复的元素去掉,因为接下来我们要用二分查找来确定每条线段的标号,去重才能保证标号唯一。

下来就是通过二分查找把每条线段离散后的范围和标号对应起来,进行区间更新。

最后再查询一波就好了。线段树我用-1代表此区间没有颜色或者不是纯色的情况

这道题的离散化是很巧妙的。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef double ld;
const int INF=0x3f3f3f3f;
const int N = 10003;
int Tree[8 * N], li[4 * N], ri[4 * N], x[4 * N], y[4 * N], sum;
bool vis[4 * N];

inline int Bin(int l, int r, int x)
{
    int m;
    while(l < r)
    {
        m = (l + r) / 2;
        if(y[m] == x)
            return m;
        else if(y[m] > x)
            r = m - 1;
        else
            l = m + 1;
    }
    return l;
}

void Update(int L, int R, int op, int l, int r, int rt)
{
    if(l >= L && r <= R)
    {
        Tree[rt] = op;
        return ;
    }
    if(Tree[rt] != -1)
    {
        int ans = Tree[rt];
        Tree[rt*2] = ans;
        Tree[rt*2+1] = ans;
        Tree[rt] = -1;
    }
    int m = (l + r) >> 1;
    if(m >= L)
        Update(L, R, op, l, m, rt << 1);
    if(m < R)
        Update(L, R, op, m + 1, r, rt << 1 | 1);
}


void Query(int l, int r, int rt)
{
    if(Tree[rt] != -1)
    {
        if(!vis[Tree[rt]])
        {
            vis[Tree[rt]] = true;
            sum++;
        }
        return ;
    }
    if(l == r)
        return ;
    int m = (l + r) / 2;
    Query(l, m, rt << 1);
    Query(m + 1, r, rt << 1 | 1);
}


int main()
{
    int t, n;
    cin >> t;
    while(t--)
    {
        cin >> n;
        memset(vis, false, sizeof(vis));
        memset(Tree, -1, sizeof(Tree));
        int cnt = 1;
        for(int i = 1; i <= n; ++ i)
        {
            cin >> li[i] >> ri[i];
            x[cnt++] = li[i];
            x[cnt++] = ri[i];
        }

        sort(x + 1, x + cnt);
        int m = cnt;
        for(int i = 2; i < cnt; ++ i)
        {
            if(x[i] - x[i - 1] > 1)
            {
                x[m++] = x[i - 1]++;
            }
        }
        sort(x + 1, x + m);
        int top = 2;
        y[1] = x[1];
        for(int i = 2; i < m; ++ i)
        {
            if(x[i] != x[i - 1])
            {
                y[top++] = x[i];
            }
        }
        for(int i = 1; i <= n; ++ i)
        {
            int L = Bin(1, top - 1, li[i]);
            int R = Bin(1, top - 1, ri[i]);
            Update(L, R, i, 1, top - 1, 1);
        }
        sum = 0;
        Query(1, top - 1, 1);
        cout << sum << endl;
    }
    return 0;
}

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值