POJ 2528 Mayor's posters

来源:互联网 发布:php软件指的是什么 编辑:程序博客网 时间:2024/05/19 13:19
Mayor's posters
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 60706 Accepted: 17570

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

给你n张海报和海报两端的坐标,求最后上面能够看见的海报的数量,但是海报的长度非常的大,那么就考虑离散话,因为只有10000张海报,那么离散话后最多也就20000个点, 可以用线段树来解决,每次更新区间,最后遍历整棵树找哪些海报是出现了的,这里要注意pushdown 的理解和运用,该把状态往下推的时候就往下推,因为最后访问的肯定是叶子节点,那么就没有pushup,还挺快的可能是数据比较水吧。


#include<stdio.h>#include<string.h>#include<math.h>#include<algorithm>#include<vector>using namespace std;#define maxn 20005int use[maxn*10],ans;struct node{    int l;    int r;    int c;    int lazy;}tree[maxn*10];int paozhi[maxn*10][2];vector<int>aa;int n;int find_key(int c){    int ans=lower_bound(aa.begin(),aa.end(),c)-aa.begin()+1;    return ans;}void build(int id,int tl,int tr){    tree[id].l=tl;    tree[id].r=tr;    int mid=(tl+tr)/2;    tree[id].c=0;    tree[id].lazy=0;    if(tl==tr)        return ;    build(id*2,tl,mid);    build(id*2+1,mid+1,tr);}void pushdown(int id){    if(tree[id].lazy!=0)    {        tree[id*2].c=tree[id].lazy;        tree[id*2].lazy=tree[id].lazy;        tree[id*2+1].c=tree[id].lazy;        tree[id*2+1].lazy=tree[id].lazy;        tree[id].lazy=0;    }}void update(int id,int x,int y,int z){    int tl=tree[id].l;    int tr=tree[id].r;    if(x>tr||y<tl)        return;    if(tl>=x&&tr<=y)    {        pushdown(id);        tree[id].c=z;        tree[id].lazy=z;        return ;    }    pushdown(id);    update(id*2,x,y,z);    update(id*2+1,x,y,z);}void cnt(int id){    if(tree[id].c!=0)    {        if(use[tree[id].c]==0&&tree[id].l==tree[id].r)        {            use[tree[id].c]++;            ans++;        }    }    pushdown(id);    if(tree[id].l==tree[id].r)        return;    cnt(id*2);    cnt(id*2+1);}int main(){    int t;    scanf("%d",&t);    while(t--)    {        ans=0;        memset(use,0,sizeof(use));        scanf("%d",&n);        for(int i=1;i<=n;i++)            scanf("%d%d",&paozhi[i][0],&paozhi[i][1]);        aa.clear();        int len=0,c;        for(int i=1;i<=n;i++)        {            aa.push_back(paozhi[i][0]);            aa.push_back(paozhi[i][1]);        }        sort(aa.begin(),aa.end());        aa.erase(unique(aa.begin(),aa.end()),aa.end());        int m=n;        n=aa.size();        build(1,1,n);        for(int i=1;i<=m;i++)        {            int x=find_key(paozhi[i][0]);            int y=find_key(paozhi[i][1]);            update(1,x,y,i);        }        cnt(1);        printf("%d\n",ans);    }return 0;}

0 0
原创粉丝点击