线段树 离散化 NYOJ-posters

posters

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 6
描述
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. 
输入
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.
输出
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. 
http://acm.pku.edu.cn/JudgeOnline/images/2528_1.jpg
样例输入
1
5
1 4
2 6
8 10
3 4
7 10

样例输出
4

题意就是在一面墙上贴海报,每个人贴着海报不一样宽,后贴的可以覆盖先贴的海报的部分或全部。求最后有多少海报可见(全部或部分)。


首先将输入了海报的左右位置离散化到一个较小的区间(即收集所有海报左右位置的点,并排序,可去同,该点集称为Point),然后对这个区间建立线段树,即将这个区间分成若干个小的。


由于先输入的先贴海报,所以这里就不用对输入的各个海报进行排序了。


对于每一个输入的海报,分别查找海报左右点在点集Point中的位置(二分法),然后对这对位置以及该海报的编号去更新线段树中的线段的编号。


最后收集线段树中不同的编号,不同的编号个数就是可以的海报数。


注意93行的操作,由于输入的是海报覆盖的线段区间(题中叫BYTE吧),不是点区间,所以这样操作转化成点区间,我是这么做的,可能有不需要转化的方法。


#include <iostream>
002. #include <algorithm>
003. #include <set>
004. using namespace std;
005.  
006. #define MAXN 10005
007.  
008. struct node
009. {
010. int left;
011. int right;
012. int index;
013. node():left(0),right(0),index(0){}
014. }Tree[MAXN*4],input[MAXN];
015.  
016. int point[MAXN*2],sum;
017. set<int> visible;
018.  
019. void BuildTree(int root,int left,int right)
020. {
021. Tree[root].left=left;
022. Tree[root].right=right;
023. Tree[root].index=-1;
024. if(left+1<right)
025. {  
026. int mid=(left+right)/2;
027. BuildTree(root*2,left,mid);
028. BuildTree(root*2+1,mid,right);
029. }
030. }
031.  
032. int Search(int k)
033. {
034. int lhs=0,rhs=sum-1,mid;
035. while(lhs<=rhs)
036. {
037. mid=(lhs+rhs)/2;
038. if(point[mid]==k)
039. return mid;
040. else if(k>point[mid])
041. lhs=mid+1;
042. else
043. rhs=mid-1;
044. }
045. }
046.  
047. void UpdateTree(int k,int a,int b,int index)
048. {
049. if(Tree[k].left==a&&Tree[k].right==b)
050. {
051. Tree[k].index=index;
052. return;
053. }
054. if(Tree[k].index!=-1)
055. {
056. Tree[2*k].index=Tree[2*k+1].index=Tree[k].index;
057. Tree[k].index=-1;
058. }
059. int mid=(Tree[k].left+Tree[k].right)/2;
060. if(b<=mid)
061. UpdateTree(2*k,a,b,index);
062. else if(a>=mid)
063. UpdateTree(2*k+1,a,b,index);
064. else
065. {
066. UpdateTree(2*k,a,Tree[2*k].right,index);
067. UpdateTree(2*k+1,Tree[2*k+1].left,b,index);
068. }
069. }
070.  
071. void SearchTree(int k)
072. {
073. if(Tree[k].index!=-1)
074. visible.insert(Tree[k].index);
075. else if(Tree[k].right>Tree[k].left+1)
076. {
077. SearchTree(2*k);
078. SearchTree(2*k+1);
079. }
080. }
081.  
082. int main()
083. {
084. int c,n;
085. cin>>c;
086. while(c--)
087. {
088. cin>>n;
089. sum=0;
090. for(int i=0;i<n;i++)
091. {
092. cin>>input[i].left>>input[i].right;
093. input[i].left--;
094. point[sum++]=input[i].left;
095. point[sum++]=input[i].right;
096. }
097. sort(point,point+sum);
098. sum=unique(point,point+sum)-point;
099. BuildTree(1,0,sum-1);
100. for(int i=0;i<n;i++)
101. {
102. int x=Search(input[i].left);
103. int y=Search(input[i].right);
104. UpdateTree(1,x,y,i);
105. }
106. visible.clear();
107. SearchTree(1);
108. cout<<visible.size()<<endl;
109. }
110. return 0;
111. }


还有很多与之相似的OJ题目,City Horizon, The Shadow等,思路都一样,细节不同。


参考 :http://blog.youkuaiyun.com/zsc09_leaf/article/details/6359061

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值