POJ 2528 Mayor's posters

来源:互联网 发布:sql中单引号转义 编辑:程序博客网 时间:2024/06/05 08:21
Mayor's posters
Time Limit: 1000MS
 Memory Limit: 65536KTotal Submissions: 63165 Accepted: 18226

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

151 42 68 103 47 10

Sample Output

4


题意:

一块墙壁贴海报,a,b 表示从a点贴到b点,区间之间可能会覆盖,问你全部贴完了之后,还有那些海报能被看到。


解题思路

由于1 <= li <= ri <= 10000000, 难以建一颗树 1-10000000的线段树,空间花费太大。因为可能出现 两张海报,一张贴在1-5 另一张贴在 999999-10000000 中间差很多数,但是这些数都是无效的。我们希望能做一个映射关系, 让原本的999999 对应下标6。

例如样例

所有端点分别为

1 2 3 4  6 7 8 10

我们希望给这些端点重新编号, 得到如下关系

1  2  3  4  5  6  7   8 ///下标

1  2  3  4  6  7  8  10  

相当于做了一个映射关系,使得原本不连续的数变为连续的。


贴海报相当于给区间染色,

1 如果当前结点区域 [完全] 包含在染色区域内 ,直接将本区间颜色改为当前颜色,并返回。

2 如果当前节点区域 部分 包含在染色区域内,说明当前区间将会颜色不唯一了,将当前节点颜色传递给左右儿子,本身颜色置零


统计颜色时候,每种颜色只能统计一次,并且

当遇到一个区间存在唯一颜色,说明其左右子树也为同样颜色, 没必要统计了


代码:

#include <iostream>#include <cstdio>#include <map>#include <cmath>#include <string.h>#include <algorithm>#include <set>#include <sstream>#include <vector>#include <queue>#include <stack>using namespace std;const int maxn = 10000*2*4;const int INF = 0x3f3f3f3f;struct node{    int l,r;    int col;///颜色编号}tree[maxn];void buildtree(int node,int b,int e){    int mid = (b+e)/2;    tree[node].l = b;    tree[node].r  = e;    tree[node].col = 0;    if(b==e) return;    if(b <= mid) buildtree(node*2,b,mid);    if(e > mid) buildtree(node*2+1,mid+1,e);}void push_down(int node){    if(tree[node].col){        tree[node*2].col = tree[node].col;        tree[node*2+1].col = tree[node].col;        tree[node].col = 0;    }}void update(int node,int ql,int qr,int z){    if(ql <= tree[node].l && qr >= tree[node].r){        tree[node].col = z;        return;    }    push_down(node);    int mid  = (tree[node].l + tree[node].r)/2;    if(ql <= mid) update(node*2,ql,qr,z);    if(qr > mid) update(node*2+1,ql,qr,z);}bool visit[10000];int ans;void query(int node){    if(tree[node].col){        if(!visit[tree[node].col]){            ans++;            visit[tree[node].col] = 1;        }        return;///这个区间内全是这种颜色,不需要再进入左右子树了    }    if(tree[node].l == tree[node].r) return;///注意递归结束条件    query(node*2);    query(node*2+1);}int main(){    int t;    scanf("%d",&t);    while(t--){        int n;        int colnum = 1;        scanf("%d",&n);        int a[10010];        int b[10010];        int x[20010];        int cnt = 0;///注意下标从零开始        for(int i = 0;i<n;++i){            scanf("%d%d",&a[i],&b[i]);            x[cnt++] = a[i];            x[cnt++] = b[i];        }        ///离散化        sort(x,x+cnt);        cnt = unique(x,x+cnt) - x;///第一个重复元素的下标        buildtree(1,1,cnt);///下标        for(int i = 0;i < n; ++i){            ///找到a[i]和b[i]在新数组中的下标            a[i] = lower_bound(x,x+cnt,a[i]) - x+1;///从1开始标号            b[i] = lower_bound(x,x+cnt,b[i]) - x+1;            update(1,a[i],b[i],colnum++);        }        memset(visit,0,sizeof(visit));///这种颜色统计过没有        ans = 0;        query(1);        printf("%d\n",ans);    }    return 0;}


1 0
原创粉丝点击