zoukankan      html  css  js  c++  java
  • poj 2528 线段树离散化+染色

    题目链接
    Mayor’s posters
    Time Limit: 1000MS Memory Limit: 65536K
    Total Submissions: 92628 Accepted: 26452
    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
    Source

    Alberta Collegiate Programming Contest 2003.10.18

    由于题目的数据是达到了1e7,简单的用线段树大概率会超时,但是n只到1e4,所以这里考虑用离散化来写。
    首先我们先来讲一下什么是离散化。
    就拿上面的数据举例:
    把出现的下标从小到大排序,就有了:1 2 3 4 6 7 8 10。
    所以我们定义其在数组中有 :
    a[1] = 1, a[2] = 2, a[3] = 3, a[4] = 4, a[5] = 6, a[6] = 7, a[7] = 8, a[8] = 10.
    一下子数据从原来的10,变成了8,所以这里最多只有2e4个数据,对应这道题,离散化是一个完全合理的算法。

    一开始不知道线段树是如何离散化的,当我看完线段树是如何离散之后,相信满满的去写这道题,发现我还是太年轻了, 这道题目离散化的过程有个坑。
    例如:
    1
    3
    1 6
    5 6
    1 3
    这组数据,离散化后a[1] = 1, a[2] = 3, a[3] = 5, a[4] = 6
    进行1 6后所有颜色都是1,
    进行5 6后1 2 是颜色1,3 4 是颜色2,
    进行1 3后1 2 是颜色3,3 4 是颜色2,
    最后我们得到答案是 2,
    但是仔细想想这个答案对了吗,显然是错的,对于原来的数据3 ~ 5之间还是颜色1,
    因此我们的答案应该是3。
    所以在离散化的时候我们应该对与坐标间距离大于1的在中间插入一个数。这样就可以保证颜色不会消失了。

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #define mid ((l + r) >> 1)
    #define lson rt << 1, l, mid
    #define rson rt << 1 | 1, mid + 1, r
    #define ls rt << 1
    #define rs rt << 1 | 1
    using namespace std;
    const int N = 1e4 + 10;
    int visit[N << 3], pos[N << 3], li[N], ri[N], n, ans, color[N << 4];
    void push_down(int rt) {
    	if(color[rt]) {
    		color[ls] = color[rs] = color[rt];
    		color[rt] = 0;
    	}
    }
    void update(int rt, int l, int  r, int L, int R, int k) {
    	if(l >= L && r <= R) {
    		color[rt] = k;
    		return ;
    	}
    	push_down(rt);
    	if(L <= mid)	update(lson, L, R, k);
    	if(R > mid)	update(rson, L, R, k);
    }
    void query(int rt, int l, int r) {
    	if(color[rt]) {
    		if(!visit[color[rt]]) {
    			visit[color[rt]]++;
    			ans++;
    		}
    		return ;
    	}
    	if(l == r)	return ;
    	query(lson);
    	query(rson);
    }
    int main() {
    	int m, t;
    	scanf("%d", &t);
    	while(t--) {
    		memset(visit, 0, sizeof visit);
    		memset(color, 0, sizeof color);
    		scanf("%d", &m);
    		n = 0;
    		for(int i = 1; i <= m; i++) {
    			scanf("%d %d", &li[i], &ri[i]);
    			pos[++n] = li[i];
    			pos[++n] = ri[i];
    		}
    		sort(pos + 1, pos + n + 1);
    		int temp = n;
    		n = 1;
    		for(int i = 2; i <= temp; i++)	if(pos[i] != pos[i - 1])	pos[++n] = pos[i];//去重。
    		for(int i = n ; i > 1; i--)	if(pos[i] - pos[i - 1] > 1)	pos[++n] = pos[i - 1] + 1;//插入中间值。
    		sort(pos + 1, pos + n + 1);
    		for(int i = 1; i <= m; i++) {
    			int l = lower_bound(pos + 1, pos + n + 1, li[i]) - pos;
    			int r = lower_bound(pos + 1, pos + n + 1, ri[i]) - pos;
    			update(1, 1, n, l, r, i);
    		}
    		ans = 0;
    		query(1, 1, n);
    		printf("%d
    ", ans);
    	}
    	return 0;
    }
    
  • 相关阅读:
    [转]google gflags 库完全使用
    机器学习者面试,看这10个建议
    分享10个数据分析的小技巧(Python)
    工作学习上实用的编程相关知识分享
    前端React 框架- UmiJS有听说过吗?
    PyTorch如何构建深度学习模型?
    Sigmoid 和 Softmax 如何进行函数处理分类?
    从零开始学习机器学习最简单的 kNN 算法
    监督学习中的决策树算法(含代码)
    可视化Bert网络,发掘其中真实世界的嵌入
  • 原文地址:https://www.cnblogs.com/lifehappy/p/12601172.html
Copyright © 2011-2022 走看看