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. 在spring boot上基于maven使用redis——批量匹配并删除 (二)

    一.背景 在搭建了项目之后,由于需要通过触发动作,并删除redis中多个key. 二.思路 在查询了jedis并没有类似的删除方法之后,事情就变得清晰起来.完成上述任务,分为两个步骤,第一,找到要删除 ...

  2. Appium 安卓计算器demo

    package testProject; import org.openqa.selenium.*; import org.openqa.selenium.remote.DesiredCapabili ...

  3. nginx交替出现404和200

    今天在调试接口的时候,发现一个奇怪的问题,服务器接口交替返回404和200错误. 排查的时候发现nginx下有大量的404错误记录,而tomcat有两个,一个有正常的访问记录,而另一个虽然启动正常,但 ...

  4. Python爬虫入门(1-2):综述、爬虫基础了解

    大家好哈,最近博主在学习Python,学习期间也遇到一些问题,获得了一些经验,在此将自己的学习系统地整理下来,如果大家有兴趣学习爬虫的话,可以将这些文章作为参考,也欢迎大家一共分享学习经验. Pyth ...

  5. linux 性能分析命令及其解释

    很多时候,我们需要对linux上运行的环境大体有一个了解,那么久需要大体知道当前系统的相关资源的使用情况,那么可以用一些linux提供的丰富的命令来查看 性能分析 vmstat 虚拟内存统计 用法 U ...

  6. Alpha发布——美工+文案展示博客

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2283 文案: 学海无涯苦作舟,深海的远帆扬起成长的新程. 我将一滴水滴注 ...

  7. 1019psp

    1.本周psp: 2.本周进度条: 3.累计进度图(折线图): 4.psp饼状图:

  8. 软件工程第八周PSP

    1.本周PSP 2.本周进度条 代码行数 0 博文字数 356 知识点 无 3.时间分配饼状图 4.折线图

  9. c# apache服务器请求得到数据(初级)

    1.代码: string data = new WebClient().DownloadString("http://localhost:81/123.txt");

  10. HDU 5636 Shortest Path

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5636 题解: 1.暴力枚举: #include<cmath> #include<c ...