POJ2528(离散化+线段树区间更新)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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
1
5
1 4
2 6
8 10
3 4
7 10
Sample Output
4
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=;
struct Node{
int color;//0表示没有贴海报,-1表示贴有多个海报
int l,r;
}a[MAXN*];
struct Query{
int l,r;
}qre[MAXN];
int n;
int hax[MAXN],cnt;
int vis[MAXN],res;
void pushUp(int rt)
{
if(a[rt<<].color==a[(rt<<)|].color)
{
a[rt].color=a[rt<<].color;
}
else
{
a[rt].color=-;
}
}
void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
if(l==r)
{
a[rt].color=;
return ;
}
int mid=(l+r)>>;
build(rt<<,l,mid);
build((rt<<)|,mid+,r);
pushUp(rt);
}
void pushDown(int rt)
{
a[rt<<].color=a[rt].color;
a[(rt<<)|].color=a[rt].color;
}
void update(int rt,int l,int r,int val)
{
if(a[rt].l==l&&a[rt].r==r)
{
a[rt].color=val;
return ;
}
if(a[rt].color!=&&a[rt].color!=-)
{
pushDown(rt);
}
int mid=(a[rt].l+a[rt].r)>>;
if(r<=mid)
{
update(rt<<,l,r,val);
}
else if(mid<l)
{
update((rt<<)|,l,r,val);
}
else
{
update(rt<<,l,mid,val);
update((rt<<)|,mid+,r,val);
}
pushUp(rt);
}
void query(int rt,int l,int r)
{
if(a[rt].color==)
{
return ;
}
if(a[rt].l==l&&a[rt].r==r)
{
if(a[rt].color!=-)
{
if(!vis[a[rt].color])
{
vis[a[rt].color]=;
res++;
}
return ;
}
}
if(a[rt].color!=&&a[rt].color!=-)
{
pushDown(rt);
}
int mid=(a[rt].l+a[rt].r)>>;
query(rt<<,l,mid);
query((rt<<)|,mid+,r);
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
cnt=;
res=;
memset(vis,,sizeof(vis));
scanf("%d",&n);
for(int i=;i<n;i++)
{
scanf("%d%d",&qre[i].l,&qre[i].r);
hax[cnt++]=qre[i].l;
hax[cnt++]=qre[i].r;
}
sort(hax,hax+cnt);
cnt=unique(hax,hax+cnt)-hax;
build(,,cnt);
int col=;
for(int i=;i<n;i++)
{
int l=lower_bound(hax,hax+cnt,qre[i].l)-hax+;
int r=lower_bound(hax,hax+cnt,qre[i].r)-hax+;//需要+1
update(,l,r,col);
col++;
}
query(,,cnt);
printf("%d\n",res);
}
return ;
}
最新文章
- ajaxform使用
- 【linux】jdk安装
- gulp-nodemon 和 gulp-livereload 配置
- CentOS6设置密码过期时间
- 09day2
- UVA 11426 GCD - Extreme (II) 欧拉函数
- 深入了解当前ETL中用到的一些基本技术
- bzoj1588 [HNOI2002]营业额统计(Treap)
- 验证整数 Double 转 int 两种写法
- Finally-操作返回值
- ROS(indigo)机器人操作系统学习有趣丰富的Gazebo仿真示例evarobot
- html5标签基础
- netty的编解码器理解(转)
- 配置MySQL的数据源
- 【转】Oracle之索引
- springboot启动的时候日志缺少Mapping日志等
- lightoj 1282 取对数的操作
- BZOJ1069 SCOI2007最大土地面积(凸包+旋转卡壳)
- Android studio的主题颜色修改
- centos5 升级到centos6