题目链接:http://poj.org/problem?id=2528

题意:有n张海报要贴,每张需要用的区间为L到R,后面的可以贴在之前的上面,就是吧之前的挡住,求最后我们能看到几张海报;

我们可以倒着处理,因为最后贴的我们是能看到的;如果区间被贴过了result不加,没有贴过就+1并标记一下;

由于数据范围太大所以用线段树

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std; #define N 10100
#define Lson r<<1
#define Rson r<<1|1 struct Post
{
int L,R;
}p[N]; struct SegTree
{
int L, R, isCover;
int Mid()
{
return (L+R)/;
}
}a[N*]; int Hash[*N]; void Up(int r)
{
if(a[r].L!=a[r].R)
if(a[Lson].isCover== && a[Rson].isCover==)//向上更新,如果左右子树都被覆盖,那么他也会被覆盖
a[r].isCover = ;
} void BuildSegTree(int r, int L, int R)
{
a[r].isCover = ;
a[r].L = L; a[r].R = R;
if(L == R)
return ;
BuildSegTree(Lson, L, a[r].Mid());
BuildSegTree(Rson, a[r].Mid()+, R);
}
int judge(int r, int L, int R)
{
if(a[r].isCover==)
return ;//被覆盖返回0;
if(a[r].R == R && a[r].L == L)
{
a[r].isCover = ;
return ;
}
int ans;
if(L>a[r].Mid())
ans = judge(Rson, L, R);
else if(R<=a[r].Mid())
ans = judge(Lson, L, R);
else
{
int ans1 = judge(Lson, L, a[r].Mid());
int ans2 = judge(Rson, a[r].Mid()+, R);
ans = ans1 | ans2;//只要有一个没被覆盖就说明能贴;
}
Up(r);
return ans;
}
int main()
{
int T, n, k;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
k=;
for(int i=; i<=n; i++)
{
scanf("%d%d", &p[i].L, &p[i].R);
Hash[k++]=p[i].L-;//防止出现离散化后原本不相邻的数变成相邻的了
Hash[k++]=p[i].L;
Hash[k++]=p[i].R+;
Hash[k++]=p[i].R;
}
sort(Hash, Hash+k);
int len = unique(Hash, Hash+k) - Hash;
BuildSegTree(, , len);
int result = ;
for(int i=n; i>; i--)
{
int L = lower_bound(Hash, Hash+len, p[i].L) - Hash;
int R = lower_bound(Hash, Hash+len, p[i].R) - Hash; int ans=judge(, L, R);
if(ans == )
result++;
}
printf("%d\n", result);
}
return ;
}

 过了那么久再写一遍

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<stack>
using namespace std; #define INF 0xfffffff
#define N 100050
#define Lson r<<1
#define Rson r<<1|1 struct SegmentTree
{
int L, R;
bool IsCover;///判断此区间是否被覆盖; int Mid() { return (L+R)>>;}
int len() { return R-L+; } } a[N<<]; void Build(int r, int L, int R)
{
a[r].L = L, a[r].R = R;
a[r].IsCover = false; if(L == R) return; Build(Lson, L, a[r].Mid());
Build(Rson, a[r].Mid()+, R); a[r].IsCover = a[Lson].IsCover && a[Rson].IsCover;
} int Query(int r, int L, int R)
{
int ans;
if(a[r].IsCover == true)
a[Lson].IsCover = a[Rson].IsCover = a[r].IsCover; if(a[r].IsCover == true)///被覆盖了,返回0;
return ;
if(a[r].L == L && a[r].R == R)///没被覆盖,把它覆盖,并返回1;
{
a[r].IsCover = true;
return ;
} if(R<=a[r].Mid())
ans = Query(Lson, L, R);
else if(L>a[r].Mid())
ans = Query(Rson, L, R);
else
{
int ans1 = Query(Lson, L, a[r].Mid());
int ans2 = Query(Rson, a[r].Mid()+, R);
ans = (ans1 || ans2);///只要有一边没被覆盖就说明能贴;
}
a[r].IsCover = a[Lson].IsCover && a[Rson].IsCover;///向上更新,因为有了这个更新所以不能在上面直接返回结果;
return ans;
} struct node
{
int L, R;
}p[N]; int Hash[N*], cnt; int main()
{
int T, n, L, R;
scanf("%d", &T);
while(T--)
{
cnt = ;
scanf("%d", &n);
for(int i=; i<=n; i++)
{
scanf("%d %d", &p[i].L, &p[i].R);
Hash[cnt++] = p[i].L; Hash[cnt++] = p[i].R;
Hash[cnt++] = p[i].L-; Hash[cnt++] = p[i].R+;
///加上左右边界是为了防止出现本身没有相连的,而离散化之后相连的情况;
}
sort(Hash, Hash+cnt);
cnt = unique(Hash, Hash+cnt)-Hash; Build(, , cnt); int ans = ; for(int i=n; i>=; i--)
{
L = lower_bound(Hash, Hash+cnt, p[i].L) - Hash;
R = lower_bound(Hash, Hash+cnt, p[i].R) - Hash;
ans += Query(, L, R);
}
printf("%d\n", ans);
}
return ;
}

Mayor's posters---poj2528线段树、离散化的更多相关文章

  1. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

  2. D - Mayor's posters(线段树+离散化)

    题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...

  3. POJ2528 Mayor's posters(线段树+离散化)

    题意 : 在墙上贴海报, n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000).求出最后还能看见多少张海报. 分析 ...

  4. 【POJ 2528】Mayor’s posters(线段树+离散化)

    题目 给定每张海报的覆盖区间,按顺序覆盖后,最后有几张海报没有被其他海报完全覆盖.离散化处理完区间端点,排序后再给相差大于1的相邻端点之间再加一个点,再排序.线段树,tree[i]表示节点i对应区间是 ...

  5. poj2528 Mayor's posters (线段树+离散化)

    恩,这区间范围挺大的,需要离散化.如果TLE,还需要优化一下常数. AC代码 #include <stdio.h> #include <string.h> #include & ...

  6. Mayor's posters(线段树+离散化)

    这道题最关键的点就在离散化吧. 假如有三张海报[1, 10] [10, 13][15,  20] 仅仅三个区间就得占用到20了. 但是离散化后就可以是[1, 2] [2, 3] [4, 5] n到1e ...

  7. Mayor's posters(线段树+离散化+区间染色)

    题目链接:http://poj.org/problem?id=2528 题目: 题意:将n个区间进行染色(对于同一个区间,后一次染色会覆盖上一次的染色),问最后可见的颜色有多少种. 思路:由于区间长度 ...

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

    http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...

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

    题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...

  10. poj2528(线段树+离散化)Mayor's posters

    2016-08-15 题意:一面墙,往上面贴海报,后面贴的可以覆盖前面贴的.问最后能看见几种海报. 思路:可以理解成往墙上涂颜色,最后能看见几种颜色(下面就是以涂色来讲的).这面墙长度为1~1000 ...

随机推荐

  1. [WallProxy] WallProxy

    1. 在Linux/Ubuntu平台导入CA.crt证书. 1.1. 首先安装libnss3-tools:sudo apt-get install libnss3-tools. 1.2. 导入证书:c ...

  2. javaweb项目中表单生成的验证码以及校验

    首先先来看一下项目的结构吧,有两个servlet,一个是进行验证码的生成以及存储的,一个是进行校验的,还有一个jsp页面是用来实现form表单的书写和展示: 我们只需要看这三个就行了,其他的自动忽略: ...

  3. JAVA对URL的解码【转】

    前段时间做URL的中文转换,有些url是utf8的格式,有的是gb2312的格式,很难区分到底是utf8还是gb2312,找了好久,发现网上的一个牛人写的转换代码: package org.apach ...

  4. 处理i18n国际电话区号的代码实践

    本文转载至 http://adad184.com/2015/08/18/practice-in-i18n-dialling-code/ 前言 上周在忙产品的国际化(i18n)的问题 其中一个很重要的地 ...

  5. codeforces水题100道 第十五题 Codeforces Round #262 (Div. 2) A. Vasya and Socks (brute force)

    题目链接:http://www.codeforces.com/problemset/problem/460/A题意:Vasya每天用掉一双袜子,她妈妈每m天给他送一双袜子,Vasya一开始有n双袜子, ...

  6. std::u32string conversion to/from std::string and std::u16string

    I need to convert between UTF-8, UTF-16 and UTF-32 for different API's/modules and since I know have ...

  7. VScode首选项

    首选项相关的设置: // 将设置放入此文件中以覆盖默认设置 { "workbench.iconTheme": "vscode-icons", "wor ...

  8. pip安装python包出现Cannot fetch index base URL http://pypi.python.org/simple/

    pipinstall***安装python包,出现 Cannot fetch index base URL  http://pypi.python.org/simple /错误提示或者直接安装不成功. ...

  9. Packetbeat协议扩展开发教程(1)

    Packetbeat ( https://www.elastic.co/products/beats/packetbeat )是一个开源的网络抓包与分析框架,内置了很多常见的协议解析,如HTPP.My ...

  10. iOS开发过程中使用Core Data应避免的十个错误

    原文出处: informit   译文出处:cocoachina Core Data是苹果针对Mac和iOS平台开发的一个框架,主要用来储存数据.对很多开发者来说,Core Data比较容易入手,但很 ...