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
来源
题解:一开始想着直接模拟就好了,然后超了一发内存,发现n有10000000这么大,好吧,换姿势,将所有点离散化一发,然后坚持模拟,TLE。。。还是姿势不对,好吧,查了发题解,线段树搞搞(之前没搞过区间染色,没啥经验),想着模拟就OK了,线段树的话就简单了,直接区间标记颜色(指标号)即可,然后就是无限的wa啊,什么情况呀。。。。难道还是姿势不对哦,看了大神的博客:http://blog.youkuaiyun.com/metalseed/article/details/8041334发现离散化有个bug啊,如:[1,10] [1,4] [6,10],竟然是2种(正解肯定是3种啦),好吧,算是加深对离散化的记忆了,呢该怎么办?有一个简单的方法就是在离散化后相邻的两个数假如差值大于1,则无脑填一个这两个数之前的任意数即可(最好是较小的数+1),这样就不会出现上述情况了。。
#include<map> #include<stack> #include<queue> #include<vector> #include<math.h> #include<stdio.h> #include<iostream> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; typedef long long ll; #define inf 1000000000 #define mod 1000000007 #define maxn 1005000 #define lowbit(x) (x&-x) #define eps 1e-10 int l[maxn],r[maxn],pos[maxn],col[maxn],cnt,flag[maxn],ans; int sech(int x) { int l=1,r=cnt; while(l<=r) { int mid=(l+r)/2; if(pos[mid]==x) return mid; if(pos[mid]<x) l=mid+1; else r=mid-1; } } void pushdown(int id) { if(col[id]) { col[id*2]=col[id]; col[id*2+1]=col[id]; col[id]=0; } } void update(int id,int l,int r,int a,int b,int x) { if(l>b || r<a) return; if(l>=a && r<=b) { col[id]=x; return; } pushdown(id); int mid=(l+r)/2; if(a<=mid) update(id*2,l,mid,a,b,x); if(b>mid) update(id*2+1,mid+1,r,a,b,x); } void query(int id,int l,int r) { if(col[id]) { if(flag[col[id]]==0) ans++; flag[col[id]]=1; return; } if(l==r)return; int mid=(l+r)/2; query(id*2,l,mid); query(id*2+1,mid+1,r); } int main(void) { int T,i,j,n,num; scanf("%d",&T); while(T--) { num=1;ans=0;cnt=0; memset(col,0,sizeof(col)); memset(flag,0,sizeof(flag)); scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d%d",&l[i],&r[i]); pos[++cnt]=l[i]; pos[++cnt]=r[i]; } sort(pos+1,pos+cnt+1); for(i=2;i<=cnt;i++) if(pos[i]!=pos[i-1]) pos[++num]=pos[i]; cnt=num; for(i=cnt;i>1;i--) if(pos[i]-1!=pos[i-1]) pos[++cnt]=pos[i-1]+1; sort(pos+1,pos+cnt+1); for(i=1;i<=n;i++) { int t1=sech(l[i]); int t2=sech(r[i]); update(1,1,cnt,t1,t2,i); } query(1,1,cnt); printf("%d\n",ans); } return 0; }