poj2528(线段树+离散化)

来源:互联网 发布:冠新软件 中标 编辑:程序博客网 时间:2024/05/18 02:50

地址:http://poj.org/problem?id=2528

Mayor's posters
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 34263 Accepted: 9927

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

题意:输入海报的起始与终结位置,然后输出最后能看见多少张海报。

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;#define M 20010struct node{int l,r,sum;}tree[4*M];struct line{int li,num;}li[M*2];int hash[M];bool cmp(line a,line b){return a.li<b.li;}void ori_tree(int i,int l,int r){int m=(l+r)/2;tree[i].sum=0;tree[i].l=l;tree[i].r=r;if(l!=r){ori_tree(i*2,l,m);ori_tree(i*2+1,m+1,r);}}void updata(int i,int l,int r,int num){if(tree[i].l==l&&tree[i].r==r) tree[i].sum=num;else{if(tree[i].sum!=-1){tree[i*2].sum=tree[i].sum;tree[i*2+1].sum=tree[i].sum;tree[i].sum=-1;}int m=(tree[i].l+tree[i].r)/2;if(r<=m) updata(i*2,l,r,num);else if(l>m) updata(i*2+1,l,r,num);else{updata(i*2,l,m,num);updata(i*2+1,m+1,r,num);}}}void sum(int i){if(tree[i].sum>=0)hash[tree[i].sum]++;else{sum(i*2);sum(i*2+1);}}int main(){int t,m,l[10010],r[10010],i,ans,tp,kp;scanf("%d",&t);while(t--){scanf("%d",&m);ans=0;memset(hash,0,sizeof(hash));for(i=1;i<=m;i++)  //离散化:就是将没有用的大段空间省略,例如本题若存在一张海报的起点为1,终点为10000000,而且这中间没有别的海报,那么可以将其赋值为起点为1,终点为2的海报{scanf("%d%d",&l[i],&r[i]);li[(i-1)*2].li=l[i];li[(i-1)*2].num=-i;li[(i-1)*2+1].li=r[i];li[(i-1)*2+1].num=i;}sort(li,li+2*m,cmp);kp=li[0].li;tp=1;for(i=1;i<=m*2;i++){if(kp!=li[i-1].li){tp++;kp=li[i-1].li;}if(li[i-1].num<0)l[-li[i-1].num]=tp;if(li[i-1].num>0)r[li[i-1].num]=tp;}                  //离散化结束ori_tree(1,1,tp);for(i=1;i<=m;i++)updata(1,l[i],r[i],i);sum(1);for(i=1;i<=m;i++)if(hash[i]!=0) ans++;printf("%d\n",ans);}return 0;}