poj2528 Mayor's posters
来源:互联网 发布:sqlserver语法 编辑:程序博客网 时间:2024/05/16 17:53
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 l i 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 <= l i <= ri <= 10000000. After the i-th poster is placed, it entirely covers all wall segments numbered l i, l i+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
151 42 68 103 47 10
Sample Output
4
#include<cstdio>#include<algorithm>#include<iostream>#include<cstring>#include<string>using namespace std;struct node{ int q,m,cl;} q[1000000];int tree[4000000],ha[4000000],c[4000000];int ans;void pushdown(int left,int right,int mid,int root){ if(tree[root]) { tree[root<<1]=tree[root<<1|1]=tree[root]; tree[root]=0; }}void update(int x,int y,int c,int left,int right,int root){ if(x<=left&&y>=right) { tree[root]=c; return ; } int mid=(left+right)>>1; pushdown(left,right,mid,root); if(x<=mid) update(x,y,c,left,mid,root<<1); if(y>mid) update(x,y,c,mid+1,right,root<<1|1);}void query(int left,int right,int root){ if(tree[root]) { if(!ha[tree[root]]) { ans++; ha[tree[root]]=1; } return ; } if(left==right) return ; int mid=(left+right)>>1; query(left,mid,root<<1); query(mid+1,right,root<<1|1);}int erfen(int x,int right){ int left=0,mid; while(left<=right) { mid=(left+right)>>1; if(c[mid]==x) return mid; else if(c[mid]<x) left=mid+1; else right=mid-1; }}int main(){ int t,n; scanf("%d",&t); while(t--) { memset(tree,0,sizeof(tree)); memset(ha,0,sizeof(ha)); scanf("%d",&n); int k=0; for(int i=0; i<n; i++) { scanf("%d %d",&q[i].q,&q[i].m); q[i].cl=i+1; c[k++]=q[i].q; c[k++]=q[i].m; } sort(c,c+k-1); int cnt=1; for(int i=1; i<k; i++) { if(c[i]!=c[i-1]) c[cnt++]=c[i]; } for(int i=cnt-1; i>=1; i--) //补成空位进行离散化 { if(c[i]!=c[i-1]+1) c[cnt++]=c[i-1]+1; } sort(c,c+cnt); for(int i=0; i<n; i++) { int l=erfen(q[i].q,cnt-1); int r=erfen(q[i].m,cnt-1); update(l,r,q[i].cl,0,cnt-1,1); } ans=0; query(0,cnt-1,1); printf("%d\n",ans); } return 0;}
0 0
- POJ2528 Mayor's posters
- POJ2528 Mayor's posters
- poj2528 Mayor's posters
- POJ2528 Mayor's posters
- POJ2528 Mayor's posters
- POJ2528--Mayor's posters
- POJ2528 Mayor's posters
- POJ2528 Mayor's posters
- POJ2528 Mayor's posters
- poj2528-Mayor's posters
- poj2528 Mayor's posters
- poj2528 Mayor's posters
- poj2528 Mayor's posters
- poj2528 Mayor's posters
- POJ2528 Mayor's posters
- POJ2528 Mayor's posters
- poj2528 Mayor’s posters
- poj2528 Mayor's Posters
- POJ 2777 Count Color (线段树区间更新加查询)
- HDU 1596 find the safest road (最短路)
- KMP思想
- 欧拉常数 Euler's constant
- Python 将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。
- poj2528 Mayor's posters
- Oracle索引错误的解决办法
- Mybatis 中在传参时,$ 和# 的区别
- Spring WebSocket教程(二)
- linux
- 【140814】VC++台球游戏源码
- ISelectionSet接口
- uva10635Prince and Princess(LIS)
- 分数转化为小数