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

 

InputOutput
1
5
1 4
2 6
8 10
3 4
7 10
4

题目大意:给出一面长度10000000墙,给出n张海报贴在墙上,每张海报都会覆盖一个范围,问最后可以看到多少张海报

线段树+离散化

海报覆盖的范围很大,直接使用数组存不下,但是只有最多10000张海报,也就是说最多出现20000个点,所以可以使用离散化,将每个点离散后,.可以直接使用线段树染色

0代表有多种颜色,1到m代表各自的颜色,线段树只要在0时向下深入,其他的直接统计颜色。

而离散化不同于一般离散化方法,用二分的方法将原题中两个海报之间的距离换成两个海报数量的差距
 

具体细节看代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define ls x << 1
#define rs x << 1 | 1

using namespace std;

typedef long long ll;
const int N = 2e5 + 5;
int cnt[N << 2], a[N << 1];
struct node
{
	int l, r, L, R;
}q[N << 1];
struct tree
{
	int l, r, v;
}t[N << 2];

void pushup(int x)
{
    if(t[ls].v == t[rs].v)
		t[x].v = t[ls].v;//如果两个儿子的颜色是一样的,说明大区间颜色一样
    else 
		t[x].v = 0;//否则,整个区间颜色不一,大区间改为0
}

void pushdown(int x)
{
    if(!t[x].v)
		return;//如果大区间的颜色都是不确定的,没有必要往下传
    t[ls].v = t[rs].v = t[x].v;//大区间颜色确定,说明下面所有小区间颜色都是一样的
    t[x].v = 0;
}

void build(int x, int l, int r)
{
    t[x].l = l, t[x].r = r;
	if(l == r)
        return;
    int mid = (l + r) >> 1;
    build(ls, l, mid);
    build(rs, mid + 1, r);
}

void update(int x, int l, int r, int c)
{
    if(t[x].l >= l && t[x].r <= r)
    {
        t[x].v = c;//找到大区间后,把大区间的颜色更改,下面的小区间等要用到的时候更新
        return;
    }
    int mid = (t[x].l + t[x].r) >> 1;
    pushdown(x);//更新小区间颜色
    if(l <= mid)
    	update(ls, l, r, c);
    if(r >= mid + 1)
		update(rs, l, r, c);
    pushup(x);//向上更新父亲
}

void ask(int x, int l, int r)
{
    if(t[x].v)//如果这个大区间的颜色是知道的,那么下面所有的小区间的颜色都一样,不用再找了
    {
        cnt[t[x].v] = 1;
        return;
    }//大区间颜色不确定,再往下找
    int mid = (t[x].l + t[x].r) >> 1;
    pushdown(x);
    if(l <= mid)
    	ask(ls, l, r);
    if(r >= mid + 1)
		ask(rs, l, r);
}

int main()
{
    int T;
	scanf("%d", &T);
    while(T--)
    {
    	int n, tot = 0, ans = 0;
    	memset(a, 0, sizeof(a));
    	memset(q, 0, sizeof(q));
    	memset(t, 0, sizeof(t));
    	memset(cnt, 0, sizeof(cnt));
    	scanf("%d", &n);
    	for(int i = 1; i <= n; i++)
    	{
    		scanf("%d%d", &q[i].l, &q[i].r);
    		a[++tot] = q[i].l;
    		a[++tot] = q[i].r;
		}//区间询问问题离散化:先找个数组存储每次询问的区间端点值,排序 
		//用lower_bound函数找数组中第一个存储区间端点值的位置 
		sort(a + 1, a + 1 + tot);
		tot = unique(a + 1, a + 1 + tot) - a - 1;//不去重会RE 
		for(int i = 1; i <= n; i++)
		{
			q[i].L = lower_bound(a + 1, a + 1 + tot, q[i].l) - a;
			q[i].R = lower_bound(a + 1, a + 1 + tot, q[i].r) - a;
		}
		build(1, 1, tot);
		for(int i = 1; i <= n; i++)
			update(1, q[i].L, q[i].R, i);
		ask(1, 1, tot);
		for(int i = 1; i <= n; i++)
			if(cnt[i])
				ans++;
		printf("%d\n", ans);
	}
return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值