kuangbin_SegTree D (POJ 2528)
讲道理我之前暂时跳过染色的题是因为总觉得有什么很高端的算法来query
直到我做了F题(ZOJ 1610)才发现就是个暴力统计.....也对 也就几万个长度单位而已....
F就不po上来了 选了有点难度的D题 需要用到离散化
单纯的离散化又会碰到后染色覆盖前染色的 所以要在每两个差距大于1的位置之间插入一个位置
以上思路参考了 http://blog.csdn.net/non_cease/article/details/7383736 但是原po的有点小错 混过了POJ的数据但是确实是错的...
就是说底色可能会被不小心算进去 所以统计的时候要忽略底色
#include <cstdio>
#include <cstring>
#include <algorithm>
#define INF 0x3f3f3f3f
#define mem(str,x) memset(str,(x),sizeof(str))
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
typedef long long LL; const int MAXN = ;
int n, hash[MAXN<<], col[MAXN<<];
int ans, li[MAXN], ri[MAXN];
bool vis[MAXN]; inline void push_down(int rt){
if(~col[rt]){
col[rt<<] = col[rt<<|] = col[rt];
col[rt] = -;
}
} void update(int L, int R, int c, int l, int r, int rt)
{
if(L <= l && R >= r){
col[rt] = c;
//printf("%d - %d : %d\n", l, r, c);
return;
} push_down(rt);
int m = (l + r) >> ;
if(L <= m) update(L, R, c, lson);
if(R > m) update(L, R, c, rson);
} void query(int l, int r, int rt)
{
if(l == r){
if(!vis[col[rt]] && ~col[rt]){
//printf("%d at %d\n", col[rt], l);
ans++;
vis[col[rt]] = true;
}
return;
} push_down(rt);
int m = (l + r) >> ;
query(lson);
query(rson);
} int binary_search(int l, int r, int c)
{
int m;
while(l <= r){
m = (l + r) >> ;
if(hash[m] == c) return m;
else if(hash[m] > c) r = m - ;
else l = m + ;
}
return -;
} int main()
{
int t;
scanf("%d", &t);
while(t--){
mem(col, -);
mem(vis, false);
scanf("%d", &n);
int nn = , mm = ;
for(int i = ; i <= n; i++){
scanf("%d%d", &li[i], &ri[i]);
hash[++nn] = li[i];
hash[++nn] = ri[i];
}
sort(hash+, hash++nn);
//去除重复数据
for(int i = ; i <= nn; i++){
if(hash[i] != hash[i-]) hash[++mm] = hash[i];
}
//在相距超过1的点之间插入一个点防止覆盖
for(int i = mm; i > ; i--){
if(hash[i] - hash[i-] > ) hash[++mm] = hash[i] - ;
}
sort(hash+, hash++mm);
//离散化构造线段树
for(int i = ; i <= n; i++){
int l = binary_search(, mm, li[i]);
int r = binary_search(, mm, ri[i]);
update(l, r, i, , mm, );
//printf("%d - %d : %d\n", l, r, i);
}
ans = ;
query(, mm, );
printf("%d\n", ans);
}
return ;
}
kuangbin_SegTree D (POJ 2528)的更多相关文章
- POJ 2528 Mayor's posters
Mayor's posters Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- 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 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
- poj 2528 (线段树+离散化)
poj 2528 For each input data set print the number of visible posters after all the posters are place ...
- POJ - 2528 Mayor's posters(dfs+分治)
POJ - 2528 Mayor's posters 思路:分治思想. 代码: #include<iostream> #include<cstdio> #include< ...
- POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)
POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...
- POJ 2528——Mayor's posters——————【线段树区间替换、找存在的不同区间】
Mayor's posters Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
- poj 2528 线段树区间修改+离散化
Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...
随机推荐
- hbase数据迁移-HDFS拷贝
1.把数据表test从hbase下拷出 hdfs dfs -get /hbase/data/default/test /home/hadoop/hbase/test 2.文件放到新集群的系统上 scp ...
- Ubuntu下mysql-server的安装
(1)更新 #apt-get update (2)安装 #apt-get install mysql-server 出现窗口设置"root"用户的密码为"456456&q ...
- scala学习----柯里化
1.鸭子类型,走起来像鸭子,叫起来像鸭子,就是鸭子.函数中使用{ def close(): Unit }作为参数类型,因此任何含有此函数的类都可以作为参数传递.好处是不必使用继承特性. def wit ...
- 在VBA中调用excel函数
以前不太会用VBA时,都是在excel中使用函数来计算一些数据.毕竟函数不如代码,效率比较低.所以,就学着怎么在VBA中引用Excel函数.平时我用得比较多的函数就是countif和sumif函数.1 ...
- 在oracle中通过connect by prior来实现递归查询!
注明:该文章为引用别人的文章,链接为:http://blog.csdn.net/apicescn/article/details/1510922 ,本人记录下来只是为了方便查看 原文: connect ...
- Elasticsearch学习之入门2
关于Elasticsearch的几个概念: 1)在Elasticsearch中,文档归属于类型type,而类型归属于索引index,为了方便理解,可以把它们与传统关系型数据库做类比: Relation ...
- Linux Shell shortcut
Ctrl+a跳到第一个字符前Ctrl+x同上但再按一次会从新回到原位置 Details see below: Linux shell shortcut
- Cannot forward after response has been committed
项目:蒙文词语检索 日期:2016-05-01 提示:Cannot forward after response has been committed 出处:request.getRequestDis ...
- HP T505恢复出厂系统
1.制作usb启动U盘. ------ 从HP网站上下载,或者找供应商提供 2.按F11,从U盘启动进去,会自动执行安装,等待完成即可以.
- Html 之菜单导航(二)
网页菜单 网页菜单是一个网页的重要部分,它提供了用户可以对网站所有页面的导航,也是可能是内容的分类,例如淘宝 衣服 鞋子 水果 电脑 等等之类,也可以是 类似于游戏宣传网页一样子,酷炫的js特效 f ...