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. manjaro i3下 dmenu terminal 和 terminal_hold 打开方式记录

    分别用type为terminal 和 terminal_hold 打开eclipse 用terminal_hold打开,终端和界面分左右显示 用terminal打开,终端和界面分上下显示 除了排列方式 ...

  2. 数据库sql优化总结之2-百万级数据库优化方案+案例分析

    项目背景 有三张百万级数据表 知识点表(ex_subject_point)9,316条数据 试题表(ex_question_junior)2,159,519条数据 有45个字段 知识点试题关系表(ex ...

  3. [C++基础] tips

    1. 在g++ 中使支持C++11 https://askubuntu.com/questions/773283/how-do-i-use-c11-with-g This you can do by ...

  4. 性能度量RMSE

    回归问题的典型性能度量是均方根误差(RMSE:Root Mean Square Error).如下公式. m为是你计算RMSE的数据集中instance的数量. x(i)是第i个实例的特征值向量 ,y ...

  5. CSS3在线实战

    作者声明:本博客中所写的文章,都是博主自学过程的笔记,参考了很多的学习资料,学习资料和笔记会注明出处,所有的内容都以交流学习为主.有不正确的地方,欢迎批评指正. 本节课视频网站:https://www ...

  6. Pythagorean Triples毕达哥斯拉三角(数学思维+构造)

    Description Katya studies in a fifth grade. Recently her class studied right triangles and the Pytha ...

  7. VIM字符编码基础知识

    1 字符编码基础知识 字符编码是计算机技术中最基本和最重要的知识之一.如果缺乏相关知识,请自行恶补之.这里仅做最简要的说明. 1.1 字符编码概述 所谓的字符编码,就是对人类发明的每一个文字进行数字 ...

  8. 【转】nodeJs学习之项目结构

    新建的项目结构应该是这样 bin:项目的启动文件,也可以放其他脚本. node_modules:用来存放项目的依赖库. public:用来存放静态文件(css,js,img). routes:路由控制 ...

  9. 我是IT小小鸟读书笔记

    阅读了我是IT小小鸟后发现,自己开发程序是真的很苦难的,在现在这个空对空的时期,我们学习到大部分的全都是理论知识,而没有真正的去进行实践.没有经过实践,我们在程序开发过程中也就无法发现自身的困难. 在 ...

  10. SpringBoot与Swagger2整合

    一.Swagger简介与优势 相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还为了以后交接方便,都有要求写API文档. Swa ...