POJ.2528 Mayor’s posters (线段树 区间更新 区间查询 离散化)

题意分析

贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报。

最多有10000张海报,海报左右坐标范围不超过10000000。

一看见10000000肯定就要离散化了,因为建树肯定是建不下。离散化的方法是:先存到一个数组里面,然后sort,之后unique去重,最后查他离散化的坐标lower_bound就行了。特别注意如果是从下标为0开始存储,最后结果要加一。多亏wmr神犇提醒。

这题是覆盖问题。如果正向考虑,后者就可以覆盖在前者之上;如果逆向考虑,就是前者不能涂在后者之上。这里采用逆向考虑的方法。

具体实现细节是,我们按照给出的顺序逆向枚举,将查询和更新一体化,如果按照给出的区间查询到某个地方,发现没有涂色,那说明这张海报一定可以看见,反之不能被看见,根据每次查询的结果,来决定计数器是否自增。

代码总览

#include <cstdio>
#include <cstring>
#include <algorithm>
#define nmax 10005
using namespace std;
struct Tree{
int l,r;
//int val;
bool iscover;
int mid(){
return (l+r)>>1;
}
};
Tree tree[nmax<<4];
struct point{
int l;
int r;
}p[nmax<<1];
int finalpos[nmax<<1];
bool flag = false;
int ans = 0;
void PushUp(int rt)
{
tree[rt].iscover = tree[rt<<1].iscover && tree[rt<<1|1].iscover;
}
void Build(int l, int r, int rt)
{
tree[rt].l = l; tree[rt].r = r;
tree[rt].iscover = false;
if(l == r){
return;
}
Build(l,tree[rt].mid(),rt<<1);
Build(tree[rt].mid()+1,r,rt<<1|1);
}
void Query(int l,int r,int rt)
{
if(tree[rt].iscover) return;
if(l>tree[rt].r || r<tree[rt].l) return ;
if(tree[rt].l == tree[rt].r){
if(!tree[rt].iscover){
tree[rt].iscover = true;
flag = true;
}
return;
}
//PushDown(rt);
//if(l <= tree[rt].l && tree[rt].r <= r) return tree[rt].val;
Query(l,r,rt<<1) , Query(l,r,rt<<1|1);
PushUp(rt);
}
int t,n;
int tag = 0;
void discretization()
{
tag = 0;
for(int i = 0;i<n;++i){
scanf(" %d %d",&p[i].l,&p[i].r);
finalpos[tag++] = p[i].l;
finalpos[tag++] = p[i].r;
}
sort(finalpos,finalpos+tag);
tag = unique(finalpos,finalpos+tag) - finalpos;
}
int main()
{
//freopen("in2528.txt","r",stdin);
scanf("%d",&t);
while(t--){
scanf("%d",&n);
discretization();
Build(1,nmax<<2,1);
ans = 0;
for(int i = n-1; i>=0;--i){
flag = false;
int a = lower_bound(finalpos,finalpos+tag,p[i].l) - finalpos+1;
int b = lower_bound(finalpos,finalpos+tag,p[i].r) - finalpos+1;
Query(a,b,1);
if(flag) ans++;
}
printf("%d\n",ans);
}
return 0;
}

POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)的更多相关文章

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

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

  2. poj 2528 Mayor's posters 线段树区间更新

    Mayor's posters Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...

  3. POJ 2528 Mayor's posters(线段树,区间覆盖,单点查询)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 45703   Accepted: 13239 ...

  4. POJ 2528 Mayor's posters (线段树+区间覆盖+离散化)

    题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...

  5. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

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

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

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

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

  8. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  9. poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化

    Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...

随机推荐

  1. 订单号生成逻辑,C#和JAVA双版

    五年没写过博客了,倒是天天在看 转来转去,又转回技术 原来一直在使用微软爸爸的东西,最近一两年开始玩android,玩java,还有PostgreSQL 都有些应用了,倒是可以整理些随笔出来,这就是其 ...

  2. C++自学第一课:函数

    此贴并非教学,主要是自学笔记,所述内容只是些许个人学习心得的记录和备查积累,难以保证观点正确,也不一定能坚持完成. 如不幸到访,可能耽误您的时间,也难及时回复,贴主先此致歉.如偶有所得,相逢有缘,幸甚 ...

  3. IDEA 配置Junit4

    Junit4 主要用来执行java程序的单元测试: 1 安装junit4插件 因为我安装过了,没有安装的再输入框搜索,然后安装就行 2 选择默认使用Junit4 3 红框中的test去掉,变为“$en ...

  4. JAVA学习笔记--简介几个常见关键字static、final、this、super

    一.static static(静态的),可以放在类.方法.字段之前. 通常,当创建类时,就是在描述那个类的外观与行为.除非用 new 创建那个类的对象,否则,实际上并未获得任何对象.执行 new 来 ...

  5. Python 深浅复制

    (一)浅复制 复制列表最简单的方式是使用内置类型的构造方法: >>> l1 = [1, [2, 3], (4, 5)] >>> l2 = list(l1) > ...

  6. Week4_Linux书本一二两章

    第一章的学习内容就是对Linux内核有一个基本的了解,同时知道一些关于Linux的知识. 学习Linux,可以自己有一台装有Linux操作系统的机器,源代码的作用无可替代: Linux发展历程简介:L ...

  7. c# dataGridView排序

    一.对阿拉伯数字进行自定义排序: 简单有效方法: 1.该列的sortmode属性为auto...(一般默认) 2.比如首列序号,添加该列数据的时候直接添加int即可.切忌不要用string. obje ...

  8. 博弈---ZOJ 2083 Win the Game(染绳子)

    原题:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2083 大意:两个人分别对n条绳子染 每次染m长 最后染不下的输,问先 ...

  9. Java 更改日期格式

    import java.util.*; import java.text.*; public class TestDateFormat { public static void main(String ...

  10. APUE(unix环境高级编程)第三版---first day---部署书中实例的运行环境(apue.h)

    操作环境:RHEL7.0 部署apue.h实例运行环境 1.下载头文件src.3e.tar.gz 2.解压 tar zxvf src.3e.tar.gz 3.创建普通用户(我仿照书上创建的sar用户) ...