讲道理我之前暂时跳过染色的题是因为总觉得有什么很高端的算法来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)的更多相关文章

  1. POJ 2528 Mayor's posters

    Mayor's posters Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

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

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

  3. POJ 2528 Mayor's posters(线段树区间染色+离散化或倒序更新)

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59239   Accepted: 17157 ...

  4. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

  5. poj 2528 (线段树+离散化)

    poj 2528 For each input data set print the number of visible posters after all the posters are place ...

  6. POJ - 2528 Mayor's posters(dfs+分治)

    POJ - 2528 Mayor's posters 思路:分治思想. 代码: #include<iostream> #include<cstdio> #include< ...

  7. POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化)

    POJ.2528 Mayor's posters (线段树 区间更新 区间查询 离散化) 题意分析 贴海报,新的海报能覆盖在旧的海报上面,最后贴完了,求问能看见几张海报. 最多有10000张海报,海报 ...

  8. POJ 2528——Mayor's posters——————【线段树区间替换、找存在的不同区间】

    Mayor's posters Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Sub ...

  9. poj 2528 线段树区间修改+离散化

    Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...

随机推荐

  1. 关于sql 2005 版本问题

    win7可以安装:标准版  其他的都安装不了 win2003 2008 :可以安装任何版本

  2. java实现服务端守护进程来监听客户端通过上传json文件写数据到hbase中

    1.项目介绍: 由于大数据部门涉及到其他部门将数据传到数据中心,大部分公司采用的方式是用json文件的方式传输,因此就需要编写服务端和客户端的小程序了.而我主要实现服务端的代码,也有相应的客户端的测试 ...

  3. Eclipse 搭建 Maven Web项目

    第一步:安装JDK: 第二步:安装Eclipse: 第三步:安装tomcat7: 第四步:安装maven插件: 4.1 下载maven:http://maven.apache.org/download ...

  4. http数据返回值

    HTTP 400 - 请求无效HTTP 401.1 - 未授权:登录失败HTTP 401.2 - 未授权:服务器配置问题导致登录失败HTTP 401.3 - ACL 禁止访问资源HTTP 401.4 ...

  5. 0526 Sprint1个人总结 & 《构建之法》第八、九、十章

    Sprint1的个人总结: 我是老人组的成员,我们是做一款四则运算训练的软件.然后我是接了界面设计的任务,所以我任务将会是sprint1中相对重一点的一方.我的感觉是,界面要做得充满童趣,毕竟我们的软 ...

  6. redis主从复制搭建

    1. 安装redis-2.4.6-setup-32-bit.exe 2. 打开一个cmd窗口,使用cd命令切换到指定目录(F:\Redis) 运行 redis-server.exe redis.con ...

  7. compare

    icompareble 默认的比较器 list.sort() compareto icompare Keyvaluepair<k,p>Where k:new()

  8. CxImage在VS2010下的配置

    http://blog.csdn.net/youzhuo/article/details/24601621 一.编译Cximage 1.在SourceForge上下载cximage702_full.7 ...

  9. NOIP 考前 计算几何练习

    BZOJ 1580 直接解析算出每段的时间然后模拟即可 #include <iostream> #include <cstdio> #include <cstring&g ...

  10. strip_tags() 函数

    定义和用法 strip_tags() 函数剥去 HTML.XML 以及 PHP 的标签. 语法 strip_tags(string,allow) 参数 描述 string 必需.规定要检查的字符串. ...