poj 2528

For each input data set print the number of visible posters after all the posters are placed.

The picture below illustrates the case of the sample input. 

Sample Input

1
5
1 4
2 6
8 10
3 4
7 10

Sample Output

4

题意:贴报纸,可以互相覆盖,求最后能看见多少。

数据很大,不离散会超出内存。将浪费的部分去掉,将出现过的所有点其映射到相距更近的点上。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
#define N 100005
#define mod 258280327
#define MIN 0
#define MAX 1000001 int l[N],r[N];
int x[N];
int has[10000005]; struct node
{
int le,re;
bool covered;
} pnode[10*N]; void build(int i,int l,int r)
{
pnode[i].le = l;
pnode[i].re = r;
pnode[i].covered = false;
if(l == r)
return; build(2 * i,l,(l+r)/2);
build(2*i+1,(l+r)/2+1,r);
} bool insert(int i,int l,int r,int a,int b)//是否可见
{
if(pnode[i].covered == true)//如果要找的已经被覆盖
return false;
if(l == a && r == b)
{
pnode[i].covered = true;
return true;
} bool ans;
int mid = (l + r)>>1;
if (mid >= b)
ans = insert(i*2,l, mid, a, b);
else if (mid < a)
ans = insert(i*2+1,mid+1, r, a, b);
else
{
bool x1 = insert(i*2,l, mid, a, mid);
bool x2 = insert(i*2+1,mid+1, r, mid+1, b);
ans = x1 || x2;
} if(pnode[i*2].covered == true && pnode[i*2+1].covered == true)
pnode[i].covered = true;
return ans;
} int main()
{
int T,n,tot;
scanf("%d",&T); while(T--)
{
scanf("%d",&n);
tot = 0;
for(int i = 1; i <= n; i++)
{
scanf("%d%d",&l[i],&r[i]);
x[tot++] = l[i];
x[tot++] = r[i];
} sort(x,x+tot);
tot = unique(x,x+tot)-x;
int all=1;
for(int i = 0;i < tot;i++)
{
has[x[i]] = all;
if(i < tot - 1)
{
if(x[i+1] - x[i] == 1)
all++;
else
all+=2;
}
} build(1,1,all);
int num = 0; for(int i = n;i >=1;i--)
{
if(insert(1,1,all,has[l[i]],has[r[i]]))
num ++;
} printf("%d\n",num); }
return 0;
}

  

poj 2528 (线段树+离散化)的更多相关文章

  1. poj 2528(线段树+离散化) 市长的海报

    http://poj.org/problem?id=2528 题目大意是市长竞选要贴海报,给出墙的长度和依次张贴的海报的长度区间(参考题目给的图),问最后你能看见的海报有几张 就是有的先贴的海报可能会 ...

  2. poj 2528 线段树+离散化

    题意:在墙上贴一堆海报(只看横坐标,可以抽象成一线段),新海报可以覆盖旧海报.求最后能看到多少张海报 sol:线段树成段更新.铺第i张海报的时候更新sg[i].x~sg[i].y这一段为i. 然而坐标 ...

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

    离散化其实就是把所有端点放在一起,然后排序去个重就好了. 比如说去重以后的端点个数为m,那这m个点就构成m-1个小区间.然后给这m-1个小区间编号1~m-1,再用线段树来做就行了. 具体思路是,从最后 ...

  4. poj 2528 线段树 离散化的小技巧

    题意:在墙上贴海报,海报可以互相覆盖,问最后可以看见几张海报思路:直接搞超时+超内存,需要离散化.离散化简单的来说就是只取我们需要的值来 用,比如说区间[1000,2000],[1990,2012] ...

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

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

  6. poj 2528 线段树区间修改+离散化

    Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...

  7. Mayor's posters POJ - 2528 线段树(离散化处理大数?)

    题意:输入t组数据,输入n代表有n块广告牌,按照顺序贴上去,输入左边和右边到达的地方,问贴完以后还有多少块广告牌可以看到(因为有的被完全覆盖了). 思路:很明显就是线段树更改区间,不过这个区间的跨度有 ...

  8. Picture POJ - 1177 线段树+离散化+扫描线 求交叉图像周长

    参考  https://www.cnblogs.com/null00/archive/2012/04/22/2464876.html #include <stdio.h> #include ...

  9. Mayor's posters POJ - 2528 线段树区间覆盖

    //线段树区间覆盖 #include<cstdio> #include<cstring> #include<iostream> #include<algori ...

  10. POJ 2528 线段树

    坑: 这道题的坐标轴跟普通的坐标轴是不一样的-- 此题的坐标轴 标号是在中间的-- 线段树建树的时候就不用[l,mid][mid,r]了(这样是错的) 直接[l,mid][mid+1,r]就OK了 D ...

随机推荐

  1. Android 4.4 沉浸式透明状态栏

    原文链接:http://www.bkjia.com/Androidjc/913061.html 第一种方法 这里写代码片第一种方法,在代码设置: if(VERSION.SDK_INT >= VE ...

  2. LR录制脚本的时候打不开浏览器问题

    使用Chrome时,显示开始录制但是Action中无任何脚本,即脚本没成功生成. 使用Firefox(最新版),一直关闭程序,详细信息有StackHash_0a9e. 使用IE11时,也是显示开始录制 ...

  3. MapReduce工作机制——Word Count实例(一)

    MapReduce工作机制--Word Count实例(一) MapReduce的思想是分布式计算,也就是分而治之,并行计算提高速度. 编程思想 首先,要将数据抽象为键值对的形式,map函数输入键值对 ...

  4. Windows中添加自己的程序到开机启动中(添加服务,添加注册表)

    在系统启动的时候启动自己想要启动的程序: 方法一:利用开机启动文件夹 将exe文件或exe文件的快捷方式复制到(启动)文件夹下 以win7为例:开始→所有程序→启动→鼠标右键打开 方法二:添加系统服务 ...

  5. Python扩展模块——自动化(testlinkAPI的使用)

    使用TESTLINKAPI首先要安装TestLink_API_Python_client-0.6.4(当前最新版本) 目前只使用到了通过api获取testlink中的自定义字段and值 url = ' ...

  6. python 编码规范整理

    PEP8 Python 编码规范 一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不要使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号. ...

  7. win-zabbix_agent端配置解析

    Zabbix agent 在windows上安装部署 部署windows服务器需要在agent服务器上添加到10.4.200.2的路由 蓝泰服务器需要添加10.0.90.5的网关,联通的机器需要添加到 ...

  8. Python内置函数(40)——dir

    英文文档: dir([object]) Without arguments, return the list of names in the current local scope. With an ...

  9. 三十天学不会TCP,UDP/IP网络编程 -- RTT的计算

    欢迎去gitbook(https://www.gitbook.com/@rogerzhu/)看到完整版. 如果对和程序员有关的计算机网络知识,和对计算机网络方面的编程有兴趣,虽然说现在这种“看不见”的 ...

  10. 计算机基础,Python基础--变量以及简单的循环

    一.计算机基础 1.CPU 相当于人体的大脑,用于计算处理数据. 2.内存  用于存储数据,CPU从内存调用数据处理计算,运算速度很快. PS:问:既然在内存里的数据CPU运算速度快,为什么计算机不全 ...