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 ...
随机推荐
- Filter过滤器实现同一地址手机和电脑页面不同
最近做一个网站,客户要求在访问主域名的时候实现电脑访问时展示电脑页面,手机访问时展示h5的手机页面,这种需求的使用还是比较多的:尤其网站需要百度推广的时候,百度推广就要求同一域名下,手机访问时展示手机 ...
- try{...} catch {...} finally{...} 各种情况代码的执行情况
try { int i = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("in the 'try'"); } ca ...
- 错误记录--关于foreach,集合已修改;可能无法执行枚举操作
集合已修改,可能无法执行枚举操作.今天在使用foreach遍历的时候出现了这样的错误.查了一下,这个是使用foreach的典型的错误问题问题.foreach在遍历取数据的过程中,枚举器只允许读,不允许 ...
- SQL 插入查询的最大ID 号 进行批量
INSERT INTO tbl_image_language ( code, docomo_cd, au_cd, softbank_cd ) SELECT DISTINCT((SELECT max(c ...
- 实时错误 '91' :对象变量或with块变量未设置
大家这几天在做学生信息管理系统的时候,出现最多的应该就是这个问题了,“实时错误‘91’:对象变量或with块变量未设置”.如右图: 遇到这个问题,我们首先应该去参考MSDN,不过这时候MSDN似乎没有 ...
- Testlink接口使用方法-python语言远程调用
deepin@deepin-pc:~/test$ cat libclienttestlink.py #!/usr/bin/env python3 # -*- coding: utf-8 -*- #! ...
- 或许是python yield最好的答案 ?
地址:http://pyzh.readthedocs.org/en/latest/the-python-yield-keyword-explained.html#yield 译者: hit9 原文: ...
- JS键盘的键码(event.keyCode)
keycode 8 = BackSpace BackSpace keycode 9 = Tab Tab keycode 12 = Clear keycode 13 = Enter keycode 16 ...
- HDU2669 第六周练习I题(扩展欧几里算法)
第六周练习I题 I - 数论,线性方程 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- android.os.NetworkOnMainThreadException 在4.0之后谷歌强制要求连接网络不能在主线程进行访问
谷歌在4.0系统以后就禁止在主线程中进行网络访问了,原因是: 主线程是负责UI的响应,如果在主线程进行网络访问,超过5秒的话就会引发强制关闭, 所以这种耗时的操作不能放在主线程里.放在子线程里,而子线 ...