讲道理我之前暂时跳过染色的题是因为总觉得有什么很高端的算法来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. mysql 新建用户、授权、远程访问

    新建用户 insert into mysql.user(Host,User,Password) values("localhost","u",password( ...

  2. Objective C中数组排序几种情况的总结

    总结OC中数组排序3种方法:sortedArrayUsingSelector:;sortedArrayUsingComparator:;sortedArrayUsingDescriptors: 数组排 ...

  3. HTML、CSS、JS在前端开发中都扮演怎样的角色

    前端开发,需要经常接触 HTML.DOM.CSS.JS等,那么HTML.CSS.JS在前端开发中究竟扮演怎样的角色呢?以下是个人的一些观点... HTML:超文本标记语言 (Hyper Text Ma ...

  4. Math.abs(~2018),掌握规律即可!

    Math.abs(~2018) 某前端群的入门问题长姿势了,一个简单的入门问题却引发了我的思考,深深的体会到自己在学习前端技术的同时忽略遗忘了一些计算机的基础知识. 对于 JS Math对象没什么可说 ...

  5. 微信支付curl出错及错误码解决方案

    1. curl错误码6 出现场景 PHP Fatal error: Uncaught exception 'WxPayException' with message 'curl出错,错误码:6' in ...

  6. ORACLE存储过程学习

    存储过程 1 CREATE OR REPLACE PROCEDURE 存储过程名 2 IS 3 BEGIN 4 NULL; 5 END; 行1: CREATE OR REPLACE PROCEDURE ...

  7. SpringMvc+Spring+Mybatis的jar包依赖关系图

  8. git使用学习

    windows系统需要安装git,mac系统自带git git相关命令: git clone  https://git.coding.net/tenchina/wangteng_python1.git ...

  9. array_unshift() 、

    定义和用法 array_unshift() 函数在数组开头插入一个或多个元素. 被加上的元素作为一个整体添加,这些元素在数组中的顺序和在参数中的顺序一样. 该函数会返回数组中元素的个数. 语法 arr ...

  10. Java 正则表达式 量词 --- 三种匹配模式【贪婪型、勉强型、占有型】

    1.Greediness(贪婪型):最大匹配X?.X*.X+.X{n,}都是最大匹配.例如你要用“<.+>”去匹配“a<tr>aava</tr>abb”,也许你所期 ...