POJ2528Mayor's posters 线段树+离散化
Mayor's posters
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 51098 | Accepted: 14788 |
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:
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.
- 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.
The picture below illustrates the case of the sample input.
Sample Input
1 5 1 4 2 6 8 10 3 4 7 10
Sample Output
4
题意:
给定一些海报,可能相互重叠,告诉你每个海报
的宽度(高度都一样的)和先后叠放顺序,问没有被完全盖住的有多少张?
题解:
首先对n海报进行离散化
再线段树找出现的就好了
代码写得挫
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define maxn 200051 #define mod 10007 #define eps 1e-9 const int inf=0x3f3f3f3f; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //****************************************************************** struct node { int x,index,jj; }a[100001]; bool cmp(node a,node b) { return a.x<b.x; } struct ss { int l,r,tag,v; }tr[100001]; int n,aa[100001],bb[100001],hash[100001]; void pushdown(int k) { if(tr[k].tag==0)return; if(tr[k].l==tr[k].r)return; tr[k<<1].tag=tr[k].tag; tr[k<<1|1].tag=tr[k].tag; tr[k<<1].v=tr[k].tag; tr[k].v=tr[k].tag; tr[k].tag=0; } void build(int k,int s,int t) { tr[k].l=s; tr[k].r=t; tr[k].tag=0; if(t==s){ tr[k].v=0;return; } int mid=(s+t)>>1; build(k<<1,s,mid); build(k<<1|1,mid+1,t); } void update(int k,int s,int t,int c) { if(tr[k].tag){ pushdown(k); } if(s==tr[k].l&&tr[k].r==t) { tr[k].tag=c; tr[k].v=c; return; } int mid=(tr[k].l+tr[k].r)>>1; if(t<=mid)update(k<<1,s,t,c); else if(s>mid)update(k<<1|1,s,t,c); else { update(k<<1,s,mid,c); update(k<<1|1,mid+1,t,c); } } void ask(int k,int s,int t) { if(tr[k].l==s&&t==tr[k].r&&tr[k].tag){ hash[tr[k].tag]=1; return ; } int mid=(tr[k].l+tr[k].r)>>1; if(t<=mid)ask(k<<1,s,t); else if(s>mid)ask(k<<1|1,s,t); else { ask(k<<1,s,mid); ask(k<<1|1,mid+1,t); } } int main() { int T=read(); while(T--) { memset(hash,0,sizeof(hash)); scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d%d",&a[i*2-1].x,&a[i*2].x); a[i*2-1].index=i; a[i*2].index=i; a[i*2-1].jj=1; a[i*2].jj=2; } a[0].x=0; sort(a+1,a+n*2+1,cmp); //for(int i=1;i<=n*2;i++)cout<<a[i].index<<" "<<a[i].x<<endl; int tmp=1; for(int i=1;i<=n*2;i++){ if(a[i].x==a[i-1].x)tmp--; if(a[i].jj%2==1) aa[a[i].index]=tmp; else bb[a[i].index]=tmp; tmp++; }//for(int i=1;i<=n;i++)cout<<" "<<aa[i]<<" "<<bb[i]<<endl; build(1,1,tmp-1); for(int i=1;i<=n;i++){ update(1,aa[i],bb[i],i); }//for(int i=1;i<=16;i++)cout<<tr[i].tag<<endl; ask(1,1,tmp-1); int ans=0; for(int i=1;i<=n;i++) { if(hash[i])ans++; } cout<<ans<<endl; } return 0; }