poj2528 线段树 区间合并 特殊离散化
来源:互联网 发布:科普数据米兔医讯 编辑:程序博客网 时间:2024/06/06 08:48
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
151 42 68 103 47 10
Sample Output
4
很显然是线段树的区间合并问题,但是这个题的离散化是个问题,于是看了网上的题解,真是学到了,这种以后要小心!!!
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn=11111;bool hash[maxn];int li[maxn],ri[maxn],x[maxn*3],cover[maxn<<4],cnt;void pushdown(int rt){if(cover[rt]!=-1){cover[rt<<1|1]=cover[rt<<1]=cover[rt];cover[rt]=-1;}}void update(int x,int y,int c,int l,int r,int rt){if(x<=l&&y>=r){cover[rt]=c;return;}pushdown(rt);int m=(l+r)>>1;if(x<=m)update(x,y,c,lson);if(y>m)update(x,y,c,rson);}int find(int key,int n){int m,l=0,r=n;while(l<=r){m=(l+r)>>1;if(x[m]==key)return m;if(x[m]<key)l=m+1;else r=m-1;}}void query(int l,int r,int rt){if(cover[rt]!=-1){if(!hash[cover[rt]])cnt++;hash[cover[rt]]=1;return;}if(l==r)return;int m=(l+r)>>1;query(lson);query(rson);}int main(){int n,t,i;scanf("%d",&t);while(t--){scanf("%d",&n);int num=0;for(i=0;i<n;i++){scanf("%d%d",&li[i],&ri[i]);x[num++]=li[i];x[num++]=ri[i];}//去重 sort(x,x+num);int m=1;for(i=1;i<num;i++)if(x[i]!=x[i-1])x[m++]=x[i];for(i=m-1;i>0;i--)if(x[i]!=x[i-1]+1)x[m++]=x[i-1]+1;//倒着来,因为m会变又懒得设变量 sort(x,x+m);memset(cover,-1,sizeof(cover));for(i=0;i<n;i++){int l=find(li[i],m),r=find(ri[i],m);update(l,r,i,0,m,1);}cnt=0;memset(hash,0,sizeof(hash));query(0,m,1);printf("%d\n",cnt);}}
0 0
- poj2528 线段树 区间合并 特殊离散化
- poj2528 离散化+线段树区间更新
- poj2528(线段树区间染色+离散化)
- POJ2528 线段树 区间更新 离散化
- poj2528 线段树 区间更新+染色+离散化
- POJ2528:Mayor's posters(线段树区间更新+离散化)
- POJ2528解题报告,区间离散化,线段树
- POJ2528 Mayor's posters 线段树区间更新+离散化
- POJ2528 Mayor's posters(线段树区间更新,离散化)
- poj2528(离散化线段树区间更新)
- poj2528-Mayor's posters(线段树,区间,离散化)
- poj2528 线段树+离散化
- poj2528线段树+离散化
- POJ2528 离散化线段树
- poj2528(线段树+离散化)
- poj2528 线段树 离散化
- poj2528离散化+线段树
- 线段树+离散化 poj2528
- android 常用知识点文章
- ZooKeeper系列之六:ZooKeeper四字命令
- ZooKeeper系列之七:ZooKeeper命令行工具
- 算数表达式求值C++实现
- Unity3d开发(九) 动画模型资源导入预处理
- poj2528 线段树 区间合并 特殊离散化
- .Net Web Api——获取客户端浏览器信息
- ZooKeeper系列之八:ZooKeeper的简单操作
- c++上机训练
- 从3个红球3个白球6个黑球中任取八个有多少种搭配
- matlab sort()升序和降序
- Linked List Cycle
- ZooKeeper系列之九:ZooKeeper API简介及编程
- 文件读写问题