poj2528 线段树
来源:互联网 发布:集换式卡牌手游 知乎 编辑:程序博客网 时间:2024/06/08 00:34
题目:http://poj.org/problem?id=2528
Description
- 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
Output
The picture below illustrates the case of the sample input.
Sample Input
151 42 68 103 47 10
Sample Output
4
Source
题目分析:
给出一系列被贴海报的左端点和右端点,求最后能看见几张海报。虽然数据量不大(10000张),但是左右端点的值可能很大(10e8),为节省内存和时间,需要等效压缩端点,因不用考虑区间长度,故如端点(2,100)间3—99的数只用保存一个下来就行,可用上述思路对端点值排序并压缩,可缩小空间和时间复杂度。每次贴海报相当于在此区间染色,最后统计区间颜色数量即可。线段树的域存储当前线段颜色,初始未染色为-1,每张海报颜色为0之n-1。使用延迟标记,当更新到或查询到当前节点则把当前的标记往下更新。故用-1表示当前线段没有被染色,或者已经将其颜色传递给其子树(此时查询时应查询他的两个子树)。最后可用hash表统计共出现的颜色种类数量。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int l[10005],r[10005],a[40005];
bool h[40005];
int ans=0;
int line[40000*4];
int bfind(int low,int high,int key){
int m;
while(low<high){
m=(low+high)>>1;
if(a[m]<key)
low=m+1;
else high=m;
}
return low;
}
void pushdown(int rt){
if(line[rt]!=-1){
line[rt<<1]=line[rt<<1|1]=line[rt];
line[rt]=-1;
}
}
void update(int a,int b,int c,int l,intr,int rt){
if(a<=l && r<=b){
line[rt]=c;return;
}
int m=(l+r)>>1;
pushdown(rt);
if(a<=m) update(a,b,c,lson);
if(b>m) update(a,b,c,rson);
}
void query(int l,int r,int rt){
if(line[rt]!=-1){
if(!h[line[rt]])
ans++;
h[line[rt]]=true;
return;
}
if(l==r) return ;
pushdown(rt);
int m=(l+r)>>1;
query(lson);
query(rson);
}
int main()
{
intt;
scanf("%d",&t);
while(t--){
int n,i;
ans=0;
memset(line,-1,sizeof(line));
memset(h,false,sizeof(h));
scanf("%d",&n);
int cnt=0;
for(i=0;i<n;++i){
scanf("%d%d",&l[i],&r[i]);
a[cnt++]=l[i];
a[cnt++]=r[i];
}
sort(a,a+cnt);
int j=1;
for(i=1;i<cnt;++i){
if(a[i]!=a[i-1])
a[j++]=a[i];
}
for(i=j-1;i>0;--i){
if(a[i]-a[i-1]!=1)
a[j++]=a[i-1]+1;
}
sort(a,a+j);
for(i=0;i<n;++i){
int a=bfind(0,j-1,l[i]);
int b=bfind(0,j-1,r[i]);
update(a,b,i,0,j,1);
}
query(0,j,1);
printf("%d\n",ans);
}
return 0;
}
- 线段树(poj2528)
- poj2528---线段树
- 线段树 POJ2528
- poj2528(线段树)
- POJ2528线段树基础
- poj2528 线段树
- POJ2528线段树变体
- poj2528 离散线段树
- poj2528&&线段树模板
- poj2528 线段树+离散化
- poj2528线段树+离散化
- 线段树典型例题--poj2528
- POJ2528 离散化线段树
- poj3264 poj3468 poj2528【线段树】
- POJ2528【线段树经典染色。】
- 线段树区间更新+poj2528
- poj2528(线段树+离散化)
- POJ2528 线段树区间覆盖
- 在windows下安装yii2框架
- Qt学习之路-简易画板4(鼠标点击拖动图形功能)
- RakNet(59)学习 -- PHP Directory Server(目录服务器)
- ZigZag Conversion
- leetcode subset
- poj2528 线段树
- linux下 tar解压 gz解压 bz2等各种解压文件使用方法
- spark源码包的编译
- 脚本入门(接口)
- linux container_of 宏
- 随记 记录数字出现次数
- geohash:用字符串实现附近地点搜索
- C++ 使用stringstream进行字符串与数字的转换
- OC语法<2.2>内存管理:手动内存管理2