您的位置:首页 > 其它

poj_2528 Mayor's posters(线段树染色+离散化处理)

2017-01-12 17:58 477 查看
Mayor's posters
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 61164 Accepted: 17674
DescriptionThe 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 postersand 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 candidatesstarted 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. InputThe 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 amongthe 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. Afterthe i-th poster is placed, it entirely covers all wall segments numbered li, li+1 ,... , ri.OutputFor 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
在墙上贴海报,问最后能看见多少张海报
可以把贴海报看成在某一个区间内染色,区间问题可以考虑用线段树,
比如样例数据离散化处理后,变成
第一张海报0-3,
第二张   1-5,
第三张   7-9,
第四张   2-3,
第五张   6-9
反映到线段树染色如下图(加上了lazy操作),*表示未染色,括号内从左到右表示变化过程,括号内不同数字表示不同颜色,
需要说明的是未染色包括区间内出现两种或两种以上颜色的情况。
[0, 10]
                           (*)
                  /                   \
              [0,5]                   [6,10]
               (*)                     (*)
           /         \             /         \
       [0,2]       [3,5]        [6,8]         [9,10]
      (*0*)        (*1*)         (*4)           (*)
         /\          /\           /\            /\
   [0,1] [2,2]   [3,4] [5,5]  [6,7] [8,8]  [9,9]  [10,10]
   (*0*) (*013)  (*1*)  (*1)    (*)  (*2)  (*24)    (*)
   /   \         /   \        /   \            
[0,0] [1,1]   [3,3] [4,4]  [6,6] [7,7]
(*0)   (*1)   (*03)  (*1)   (*)   (*2)
这样 能看到多少种报纸 就等价于 区间树的上层(即若某区间已有颜色,其子区间不再考虑,因lazy操作的影响)有多少种颜色。
接下来是离散的细节,将数据集中非重复数字,也就是我们所需要的值映射到0~m之后,由于题目的数字代表的是单位长度,
所以需要在非连续数字的中间再安插一个数,体现出单位长度的特征。
得到所需数值到0~n的映射,可以用二分查询单调数组,也可以建个哈希表。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 20010#define mod 1000000007#define PI acos(-1.0)#define LL long long#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;struct Node{int l, r;int mid(){ return l+r>>1; }}tree[maxn<<4];int n, cot;int x[maxn<<4];int ql[maxn], qr[maxn];int color[maxn<<4];bool vis[maxn];void build(int l, int r, int rt){tree[rt].l = l, tree[rt].r = r;if(l == r) return ;int m = l+r>>1;build(lson);build(rson);}void pushDown(int rt){if(color[rt] != -1){color[rt<<1] = color[rt<<1|1] = color[rt];color[rt] = -1;}}void update(int c, int L, int R, int rt){if(tree[rt].l >= L && tree[rt].r <= R){color[rt] = c;return ;}pushDown(rt);int m = tree[rt].mid();if(L <= m) update(c, L, R, rt<<1);if(R > m) update(c, L, R, rt<<1|1);}int ans;void query(int rt){if(color[rt] != -1){if(!vis[color[rt]]) vis[color[rt]] = 1, ans++;return;}if(tree[rt].l == tree[r4000t].r) return ;query(rt<<1);query(rt<<1|1);}int main(){int T;scanf("%d", &T);while(T--){cot = ans = 0;scanf("%d", &n);for(int i = 0; i < n; i++) scanf("%d%d", &ql[i], &qr[i]);for(int i = 0; i < n; i++) x[cot++] = ql[i], x[cot++] = qr[i];sort(x, x+cot);int M = 1;for(int i = 1; i < cot; i++) if(x[i] != x[i-1]) x[M++] = x[i];for(int i = 0; i < cot-1; i++) if(x[i+1] != x[i]+1) x[M++] = x[i]+1;sort(x, x+M);build(0, M, 1);memset(color, -1, sizeof(color));for(int i = 0; i < n; i++){int L = lower_bound(x, x+M, ql[i]) - x;int R = lower_bound(x, x+M, qr[i]) - x;update(i, L, R, 1);}memset(vis, 0, sizeof(vis));query(1);printf("%d\n", ans);}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: