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. C++类型萃取

    stl中的迭代器和C++中的类型萃取: http://www.itnose.net/detail/6487058.html 赐教!

  2. Tornado 用户身份验证框架

    1.安全cookie机制 import tornado.web session_id = 1 class MainHandler(tornado.web.RequestHandler): def ge ...

  3. BAT齐聚阿里安全-ASRC生态大会:呼吁联合共建网络安全白色产业链

    图说:近日,阿里安全-ASRC生态大会在杭州举行,包括BAT在内的20余家国内知名互联网企业代表,回顾过去一年网络安全面临的问题与挑战,共谋生态安全治理思路. "123456.111111. ...

  4. php抽象类和接口的区别

    php抽象类和接口的区别 tags:抽象类 接口 抽象类和接口 php 引言:这是一个面试经常被问到的问题,也是一个经典问题.我们尽量引用官方权威的说明或者经过实验来证明本文所说的内容准确性. 抽象类 ...

  5. Python内置函数(34)——map

    英文文档: map(function, iterable, ...) Return an iterator that applies function to every item of iterabl ...

  6. angular2 学习笔记 ( 第3方插件 jQuery and ckeditor )

    refer : https://forums.meteor.com/t/importing-ckeditor-using-npm/28919/2   (ckeditor) https://github ...

  7. MicrosoftWebInfrastructure 之坑

    从svn下载下来的项目,还原提示缺少MicrosoftWebInfrastructure   包 网上大多数解决方法  PM> Install-Package Microsoft.Web.Inf ...

  8. 洛谷P1209-最大公约数与最小公倍数问题

    一个萌新的成长之路 Discription 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数 条件: 1.P, ...

  9. Oracle12c:创建主分区、子分区,实现自动分区插入效果

    单表自动单个分区字段使用方式,请参考:<Oracle12c:自动分区表> 两个分区字段时,必须一个主分区字段和一个子分区字段构成(以下代码测试是在oracle12.1版本): create ...

  10. 南阳OJ-6-喷水装置(一)

    题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=6 题目大意: 现有一块草坪,长为20米,宽为2米,要在横中心线上放置半径为Ri的喷 ...