D - Mayor's posters - 2528(区间覆盖)
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn = 40005; int Hash[maxn];//记录离散化后的数据
struct Post{int l, r;}p[maxn];//记录海报
struct Tree
{
int L, R;
bool isCover;//记录这段区间是否被覆盖
int Mid(){return (L+R)/2;}
}tree[maxn*4]; void Up(int root)//向上更新,如果左右子树都被覆盖,那么他也会被覆盖
{
if(tree[root].L != tree[root].R)
if(tree[root<<1].isCover && tree[root<<1|1].isCover)
tree[root].isCover = true;
}
void Build(int root, int L, int R)
{
tree[root].L = L, tree[root].R = R;
tree[root].isCover = false; if(L == R)return ; Build(root<<1, L, tree[root].Mid());
Build(root<<1|1, tree[root].Mid()+1, R);
}
//如果区间能内还有位置返回真,否则返回假
bool Insert(int root, int L, int R)
{
if(tree[root].isCover)return false; if(tree[root].L == L && tree[root].R == R)
{
tree[root].isCover = true;
return true;
}
bool ans; if(R <= tree[root].Mid())
ans = Insert(root<<1, L, R);
else if(L > tree[root].Mid())
ans = Insert(root<<1|1, L, R);
else
{
bool Lson = Insert(root<<1, L, tree[root].Mid());
bool Rson = Insert(root<<1|1, tree[root].Mid()+1, R); ans = Lson | Rson;
} Up(root); return ans;
} int main()
{
int T; scanf("%d", &T); while(T--)
{
int i, N, nh=0; scanf("%d", &N); for(i=1; i<=N; i++)
{
scanf("%d%d", &p[i].l, &p[i].r);
Hash[nh++] = p[i].l, Hash[nh++] = p[i].l-1;
Hash[nh++] = p[i].r, Hash[nh++] = p[i].r+1;
}
sort(Hash, Hash+nh);
nh = unique(Hash, Hash+nh) - Hash; Build(1, 1, nh); int ans = 0; for(i=N; i>0; i--)
{
int l = lower_bound(Hash, Hash+nh, p[i].l) - Hash;
int r = lower_bound(Hash, Hash+nh, p[i].r) - Hash; if(Insert(1, l, r) == true)
ans += 1;
} printf("%d\n", ans);
} return 0;
}
D - Mayor's posters - 2528(区间覆盖)的更多相关文章
- POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】
任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ 2528 - Mayor's posters - [离散化+区间修改线段树]
题目链接:http://poj.org/problem?id=2528 Time Limit: 1000MS Memory Limit: 65536K Description The citizens ...
- POJ 2528 Mayor's posters(线段树,区间覆盖,单点查询)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45703 Accepted: 13239 ...
- POJ 2528 Mayor's posters (线段树+区间覆盖+离散化)
题意: 一共有n张海报, 按次序贴在墙上, 后贴的海报可以覆盖先贴的海报, 问一共有多少种海报出现过. 题解: 因为长度最大可以达到1e7, 但是最多只有2e4的区间个数,并且最后只是统计能看见的不同 ...
- Mayor's posters POJ - 2528 线段树区间覆盖
//线段树区间覆盖 #include<cstdio> #include<cstring> #include<iostream> #include<algori ...
- POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 59239 Accepted: 17157 ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ 2528 Mayor's posters(线段树/区间更新 离散化)
题目链接: 传送门 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Description The citizens of By ...
- poj 2528 Mayor's posters 线段树区间更新
Mayor's posters Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=2528 Descript ...
随机推荐
- sharesdk 的使用
社交分享组件有很多 介绍一下sharesdk 的使用 官网:http://sharesdk.cn/ 1.先上效果图 2.主要代码: public class TestShare extends Act ...
- 一段jquery代码,保存
@CHARSET "UTF-8"; #table_id tbody tr.odd td:hover{ background-color:#93CFE5; } #table_id t ...
- 触发TreeView的TreeNodeCheckChanged事件
这个事件不会主动postback,需要手动写javascript触发.对网上找到的方法做了些改进,增加UpdatePanel,以免页面不停的刷.这里就不考虑性能神马的了,因为既然项目已经允许选择使用T ...
- 说说oracle的 sysdate、trunc函数
SQL> select trunc(sysdate)+1/24+3 from dual; TRUNC(SYSDATE)+1/24-------------------2015-08-14 01: ...
- PHP 数据库 ODBC
PHP 数据库 ODBC ODBC 是一种应用程序编程接口(Application Programming Interface,API),使我们有能力连接到某个数据源(比如一个 MS Access 数 ...
- 跟我学android-常用控件之EditText
EditText 是TextView的直接子类,它与TextView的区别在于,EditText可以接受用户输入. 下面通过一个实例来说明EditText的用法 实例:sina 微博的登录界面(注意, ...
- c语言中3n+1溢出问题解决
3n+1问题是一个简单有趣而又没有解决的数学问题.这个问题是由L. Collatz在1937年提出的.克拉兹问题(Collatz problem)也被叫做hailstone问题.3n+1问题.Hass ...
- 谈谈PHP、Python与Ruby
假如你想帮他尽快找个活儿,赚到钱,推荐PHP. 假如你想让他成为一个高效工程师,推荐 Python. 假如你想让他爱上他的工作,推荐 Ruby. 语言的选择 编程语言非常重要,不要认为他们都图灵等价, ...
- N3292x IBR介绍
N3292x IBR介绍 1 IBR启动流程 图1-1 IBR启动流程 CHIP_CFG[0] Mode 0 Boot from IBR Recovery Mode with crystal inpu ...
- 解决 Ubuntu15.04 登陆界面无限循环 的问题
本人新手,在学习linux 安装NVIDIA 驱动的时候出现了一个奇怪的问题:登陆界面输入正确的账户密码,短暂闪烁后又返回了登陆界面.经查阅多种资料,已解决此问题 以下内容来自:http://blog ...