题意:那个城市里要竞选市长,然后在一块墙上可以贴海报为自己拉票,每个人可以贴连续的一块区域,后来帖的可以覆盖前面的,问到最后一共可以看到多少张海报。
思路:一看就知道是线段树,只是说要利用到离散化,也不是那么的难,当然注意,有的离散化错误也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(线段树+区间离散)的更多相关文章

  1. poj2528(线段树区间替换&离散化)

    题目链接: http://poj.org/problem?id=2528 题意: 第一行输入一个 t 表 t 组输入, 对于每组输入: 第一行  n 表接下来有 n 行形如 l, r 的输入, 表在区 ...

  2. POJ2528:Mayor's posters(线段树区间更新+离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  3. poj2528 Mayor's posters(线段树区间修改+特殊离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  4. POJ-2528 Mayor's posters(线段树区间更新+离散化)

    http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...

  5. POJ-2528 Mayor's posters (线段树区间更新+离散化)

    题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...

  6. poj-2528线段树练习

    title: poj-2528线段树练习 date: 2018-10-13 13:45:09 tags: acm 刷题 categories: ACM-线段树 概述 这道题坑了我好久啊啊啊啊,,,, ...

  7. POJ 2482 Stars in Your Window (线段树区间合并+扫描线)

    这题开始一直被矩形框束缚了,想法一直都是枚举线,但是这样枚举都需要O(n^2)...但是看了别人的思路,感觉这题思想真心很好(PS:开头好浪漫的描述啊,可惜并没有什么用)  题意就是在平面上给你一些星 ...

  8. HDU3577 线段树(区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3577 ,普通的线段树区间更新题目,较简单. 相当于一个区间覆盖问题,有一点要注意的就是叶子节点是一个长 ...

  9. POJ 2823 Sliding Window 线段树区间求和问题

    题目链接 线段树区间求和问题,维护一个最大值一个最小值即可,线段树要用C++交才能过. 注意这道题不是求三个数的最大值最小值,是求k个的. 本题数据量较大,不能用N建树,用n建树. 还有一种做法是单调 ...

随机推荐

  1. React(0.13) 组件的组合使用

    <html> <head> <title>组件的组合调用</title> <script src="build_0.13/react.m ...

  2. mongodb的serverstatus

    MongoDB shell version: 2.0.5 connecting to: test { "host" : "TENCENT64.site", -- ...

  3. 横竖屏切换时不销毁当前activity 和 锁定屏幕

    首先在Mainifest.xml的Activity元素中加入android:configChanges="orientation|keyboardHidden"属性 <act ...

  4. IntelliJ IDEA 最新激活码(截止到2018年10月14日)

    IntelliJ IDEA 注册码: EB101IWSWD-eyJsaWNlbnNlSWQiOiJFQjEwMUlXU1dEIiwibGljZW5zZWVOYW1lIjoibGFuIHl1IiwiYX ...

  5. hibernate 注释 唯一键约束 uniqueConstraints

    @Table 注解包含一个schema和一个catelog 属性,使用@UniqueConstraints 可以定义表的唯一约束. 如果是联合约束就用下面这种 @Table(name="tb ...

  6. 【struts2】值栈(后篇)

    在值栈(前篇)我们学习了值栈的基本知识,接下来,来看看在程序中具体如何使用值栈. 1 ActionContext的基本使用 1.1 如何获取? 要获取ActionContext有两个基本的方法,如果在 ...

  7. Error Code: 1030. Got error -1 from storage engine

    这个问题通常是数据库可以建表,旧表可以插入数据,正常:可是新表无法插入数据,无法改名等操作: 先从文件权限找方法,没法解决: 在网上搜了一通,大家都说的磁盘满了,但是我们的磁盘还空着呢! 后来,发现! ...

  8. Linux系统排查——CPU负载篇

    本随笔介绍CPU负载的排查手段. 查看系统负载的工具:uptime,w,都能查看系统负载,系统平均负载是处于运行或不可打扰状态的进程的平均数, 可运行:运行态,占用CPU,或就绪态,等待CPU调度. ...

  9. mysqldump脚本crontab执行失败,但是手动执行成功

    检查备份时发现,cron中定时备份mysql的脚本备份失败,但是手动自行mysql备份脚本却能正常成功 检查cron的环境变量: # cat /etc/crontab ... SHELL=/bin/s ...

  10. 温故而知新 原来 cheerio 还可以操作XML