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 ...
随机推荐
- hibernate整合spring开发的时候遇到的一些小问题
1 在spring整合hibernate开发的时候,在数据源里面配置show_sql为true,但是在实际查询的时候并没有打印sql语句,正确的解决方案为: 把<prop key="s ...
- WC项目要求
#include "stdio.h" #include "string.h" #include "stdlib.h" int charcal ...
- 每天一点 js join 函数
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- bak骗子公司
李波 身份证:310101197510313215 手机:13916407777 18621624812 13916821206住址:上海QQ:87766938 沪EE5781 奥迪Q7李寻欢77 s ...
- java 文件压缩和解压(ZipInputStream, ZipOutputStream)
最近在看java se 的IO 部分 , 看到 java 的文件的压缩和解压比较有意思,主要用到了两个IO流-ZipInputStream, ZipOutputStream,不仅可以对文件进行压缩,还 ...
- 把一个英语句子中的单词次序颠倒后输出。例如输入“how are you”,输出“you are how”;
import java.util.Scanner; public class Test2 { public void reverse(String str) { String[] wordArray ...
- css 笔记
外边距合并 当一个元素出现在另一个元素的上面时,第一个元素的下外边距和第二个元素的上外边距会产生合并,两个盒子之间的上下间距为大的数值. 当一个子元素包含在另外一个父元素(假设没有内边距 没有边框), ...
- php 画图片2
<?php // 使用php操作gd库做图 // 1. 创建一个画布资源 $im = imagecreatetruecolor(200, 50); // 2. 创建背景色 // 2.1 得到背景 ...
- 转一下关于struct字对齐的原因
作者:張道遠链接:https://www.zhihu.com/question/27862634/answer/38506197来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- php 队列
一.php中对共享内存,消息队列的操作 http://blog.csdn.net/haitun312366/article/details/8614797 二.PHP memcache 队列类 htt ...