poj2528(线段树+区间离散)
题意:那个城市里要竞选市长,然后在一块墙上可以贴海报为自己拉票,每个人可以贴连续的一块区域,后来帖的可以覆盖前面的,问到最后一共可以看到多少张海报。
思路:一看就知道是线段树,只是说要利用到离散化,也不是那么的难,当然注意,有的离散化错误也ac了......当然,在最后没必要还一个个去询问是否覆盖,直接开个标记数组,询问到一个区间只有一个覆盖值,然后进行标记就可以。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define M 100005
struct node
{
int ll,rr;
int num;
int cnt;
}tree[8*M];
int s[4*M][2],t[4*M];
int xx[4*M];
int ans=0,flag=0;
void creat(int i,int l,int r)
{
tree[i].ll=l;
tree[i].rr=r;
tree[i].cnt=0;
tree[i].num=0;
int mid=(l+r)/2;
if(l==r)
{
//tree[i].num=0;
return;
}
creat(i*2,l,mid);
creat(i*2+1,mid+1,r);
}
void updata(int i,int left,int right,int k)
{
if(tree[i].ll==left&&tree[i].rr==right)
{
tree[i].cnt=1;
tree[i].num=k;
//if(k==2)
//printf("%d %d\n",left,right);
return;
}
if(tree[i].cnt==1)
{
tree[i*2].cnt=1;
tree[i*2+1].cnt=1;
tree[i*2].num=tree[i].num;
tree[i*2+1].num=tree[i].num;
tree[i].cnt=2;
tree[i].num=0;
}
int mid=(tree[i].ll+tree[i].rr)/2;
if(mid>=right) updata(i*2,left,right,k);
else if(mid<left) updata(i*2+1,left,right,k);
else
{
updata(i*2,left,mid,k);
updata(i*2+1,mid+1,right,k);
}
if(tree[i*2].cnt==1&&tree[i*2+1].cnt==1&&tree[i*2].num==tree[i*2+1].num)
{
tree[i].cnt=1;
tree[i].num=tree[i*2].num;
} else tree[i].cnt=2;
//printf("updata\n");
}
void quest(int i)
{
//if(k==1)
//printf("%d %d %d %d %d\n",tree[i].ll,tree[i].rr,tree[i].cnt,tree[i].num,k);
if(tree[i].cnt==1)
{
xx[tree[i].num]=1;
return;
}
if(tree[i].ll==tree[i].rr)
return;
//if(flag==1)
//return;
if(tree[i].cnt==0)
return;
quest(i*2);
quest(i*2+1); // printf("quest\n");
}
int erfen(int ll,int rr,int num)
{
while(ll<=rr)
{
int mid=(ll+rr)/2;
if(t[mid]>num)
rr=mid-1;
else
ll=mid+1;
// printf("erfen\n");
}
return rr;
}
int main()
{
int text;
scanf("%d",&text);
while(text--)
{
int n;
scanf("%d",&n);
//creat(1,1,3*n);
int cnt=0;
for(int i=0;i<n;i++)
{
scanf("%d%d",&s[i][0],&s[i][1]);
t[cnt++]=s[i][0];
t[cnt++]=s[i][1];
}
sort(t,t+cnt);
int cnt1=1; for(int i=1;i<cnt;i++)
if(t[i]!=t[i-1]) t[cnt1++]=t[i]; for(int i=cnt1-1;i>0;i--)
if(t[i]!=t[i-1]+1) t[cnt1++]=t[i-1]+1;
sort(t,t+cnt1);
for(int i=cnt1;i>0;i--)
t[i]=t[i-1];
creat(1,1,cnt1+5);
for(int i=1;i<=n;i++)
{
int ll=1,rr=cnt1;
int tmp=erfen(ll,rr,s[i-1][0]);
int tmp1=erfen(ll,rr,s[i-1][1]);
//tmp;
//tmp1;
//printf("%d %d\n",tmp,tmp1);
updata(1,tmp,tmp1,i);
}
ans=0;
memset(xx,0,sizeof(xx));
quest(1);
for(int i=1;i<=n;i++)
if(xx[i]==1)
ans++;
printf("%d\n",ans);
}
return 0;
}
poj2528(线段树+区间离散)的更多相关文章
- poj2528(线段树区间替换&离散化)
题目链接: http://poj.org/problem?id=2528 题意: 第一行输入一个 t 表 t 组输入, 对于每组输入: 第一行 n 表接下来有 n 行形如 l, r 的输入, 表在区 ...
- POJ2528:Mayor's posters(线段树区间更新+离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- poj2528 Mayor's posters(线段树区间修改+特殊离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- POJ-2528 Mayor's posters(线段树区间更新+离散化)
http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...
- POJ-2528 Mayor's posters (线段树区间更新+离散化)
题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...
- poj-2528线段树练习
title: poj-2528线段树练习 date: 2018-10-13 13:45:09 tags: acm 刷题 categories: ACM-线段树 概述 这道题坑了我好久啊啊啊啊,,,, ...
- POJ 2482 Stars in Your Window (线段树区间合并+扫描线)
这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用) 题意就是在平面上给你一些星 ...
- HDU3577 线段树(区间更新)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3577 ,普通的线段树区间更新题目,较简单. 相当于一个区间覆盖问题,有一点要注意的就是叶子节点是一个长 ...
- POJ 2823 Sliding Window 线段树区间求和问题
题目链接 线段树区间求和问题,维护一个最大值一个最小值即可,线段树要用C++交才能过. 注意这道题不是求三个数的最大值最小值,是求k个的. 本题数据量较大,不能用N建树,用n建树. 还有一种做法是单调 ...
随机推荐
- Linux下安装LAMP(Apache+PHP+MySql)和禅道
1.更新yum源: yum update -y 2.安装Apache+PHP+MySql yum install httpd mysql-devel mysql-server mysql-php ph ...
- 【Linux】双向重导向命令tee
想个简单的东西,我们知道 > 会将数据流整个传送给文件或装置,因此我们除非去读取该文件或装置, 否则就无法继续利用这个数据流.万一我想要将这个数据流的处理过程中将某段信息存下来,应该怎么做? 利 ...
- Spring Cloud启动应用时指定IP或忽略某张网卡配置
说明:分布式应用部署到服务上,由于服务器可能存在多张网卡,造成IP地址不准的问题. 解决方法: 1.直接添加忽略某张网卡的配置: spring.cloud.inetutils.ignored-inte ...
- php性能分析工具xhprof
安装方法: wget http://pecl.php.net/get/xhprof-0.9.x.tgz cp xhprof-0.9.x.tgz /home/www/xhprof.tgz tar zxv ...
- javascript中 try catch finally 的使用
例一:function message(){ try { adddlert("Welcome guest!") } catch(err) { txt="此页面存在一个错误 ...
- Linux增加swap空间
1.添加交换文件并设置其大小为12G,使用如下命令 # dd if=/dev/zero of=/home/swapfile bs=1024 count=12288000 12288000+0 reco ...
- MFC中无标题栏窗口的移动
原文链接: http://blog.sina.com.cn/s/blog_6288219501015dwa.html 移动标准窗口是通过用鼠标单击窗口标题条来实现的,但对于没有标题条的窗口,就需要 ...
- 供CImage类显示的半透明PNG文件处理方法
原文链接: http://blog.sina.com.cn/s/blog_4070692f010003gy.html 前补:没想到这个帖子好像挺多人看哪……看来大家都被这个png郁闷的够呛.显示p ...
- 数据库查询实例(包含所有where条件例子)
查询指定列 [例1] 查询全体学生的学号与姓名. SELECT Sno,Sname FROM Student: [例2] 查询全体学生的姓名.学号.所在系. SELECT Sname,Sno,Sdep ...
- ASP.NET项目在IIS上使用虚拟目录
在IIS中,应用程序与虚拟目录特别容易混淆,但两者又是完全不同的概念. 应用程序是一个逻辑边界,这个边界可以分隔网站及其组成部分.虚拟目录则是一个真实的指针,这个指针指向了一个本地或远程的物理路径.虚 ...