题目链接: http://poj.org/problem?id=2528

题意: 第一行输入一个 t 表 t 组输入, 对于每组输入: 第一行  n 表接下来有 n 行形如 l, r 的输入, 表在区间 [l, r] 贴一张海报, 问最终能看见几张不同的海报;

思路: 线段树区间替换, 每次 update 时都会给对应区间加一个 lazy 标记, 最终统计标记的种数即为答案;

注意: 题目给出的 l, r 很大, 需要离散化, 和普通的离散化不同, 因为本题中每个单位是代表长度为一的一个区间,而非一个点, 所以对于两个数 a, b, 若 b > a + 1,

hash[a] = cnt, 那么hash[b] = cnt + 2 而非 hahs[b] = cnt + 1;不然对于一些数据会出错.

代码:

 #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <map>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std; const int MAXN = 1e4 + ;
int l[MAXN], r[MAXN], gel[MAXN << ], lazy[MAXN << ];
bool vis[MAXN << ];
map<int, int> hash;
int ans; void push_down(int rt){//将当前标记向下一层更新
if(lazy[rt] != -){
lazy[rt << ] = lazy[rt << | ] = lazy[rt];
lazy[rt] = -;
}
} void update(int L, int R, int x, int l, int r, int rt){//区间替换
if(L <= l && R >= r){
lazy[rt] = x;
return;
}
push_down(rt);
int mid = (l + r) >> ;
if(L <= mid) update(L, R, x, lson);
if(R > mid) update(L, R, x, rson);
} void query(int l, int r, int rt){
if(lazy[rt] != -){//标记的种数即为答案
if(!vis[lazy[rt]]){
ans++;
vis[lazy[rt]] = true;
}
return;
}
if(l == r) return;
int mid = (l + r) >> ;
query(lson);
query(rson);
} int main(void){
int t, n;
scanf("%d", &t);
while(t--){
int indx = , cnt = ;
scanf("%d", &n);
for(int i = ; i < n; i++){
scanf("%d%d", &l[i], &r[i]);
gel[indx++] = l[i];
gel[indx++] = r[i];
}
sort(gel, gel + indx);
hash[gel[]] = cnt;
for(int i = ; i < indx; i++){ //hash
if(gel[i] > gel[i - ] + ){
cnt += ;
hash[gel[i]] = cnt;
}else if(gel[i] > gel[i - ]){
cnt += ;
hash[gel[i]] = cnt;
}
}
memset(lazy, -, sizeof(lazy));
memset(vis, false, sizeof(vis));
for(int i = ; i < n; i++){
update(hash[l[i]], hash[r[i]], i, , cnt, );
}
ans = ;
query(, cnt, );
printf("%d\n", ans);
}
return ;
}

poj2528(线段树区间替换&离散化)的更多相关文章

  1. POJ-2528 Mayor's posters (线段树区间更新+离散化)

    题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...

  2. HDU 1698 Just a Hook(线段树 区间替换)

    Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...

  3. HDU.1689 Just a Hook (线段树 区间替换 区间总和)

    HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...

  4. hdu1698(线段树区间替换模板)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 题意: 第一行输入 t 表 t 组测试数据, 对于每组测试数据, 第一行输入一个 n , 表示 ...

  5. POJ2528:Mayor's posters(线段树区间更新+离散化)

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  6. POJ-2528 Mayor's posters(线段树区间更新+离散化)

    http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...

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

    题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...

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

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

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

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

随机推荐

  1. Spring/Java error: namespace element 'annotation-config' … on JDK 1.5 and higher

    Extract the jar file: mkdir spring cd spring jar xvf ../spring.jar Check the Spring version in META- ...

  2. 转 EBP ESP 的理解

    PS:EBP是当前函数的存取指针,即存储或者读取数时的指针基地址:ESP就是当前函数的栈顶指针.每一次发生函数的调用(主函数调用子函数)时,在被调用函数初始时,都会把当前函数(主函数)的EBP压栈,以 ...

  3. Oracle | PL/SQL Check约束用法详解

    1. 目标 实例讲解在Oracle中如何使用CHECK约束(创建.启用.禁用和删除) 2. 什么是Check约束? CHECK约束指在表的列中增加额外的限制条件. 注: CHECK约束不能在VIEW中 ...

  4. 解决ubuntu12.04下安装gitlabError Compiling CSS asset的错误以及401资源错误

    安装过程 https://www.gitlab.com.cn/installation/#ubuntu 解决过程 12.04ubuntu坑太多 解决有用的链接如下 https://blog.csdn. ...

  5. POJ3450 Corporate Identity —— 后缀数组 最长公共子序列

    题目链接:https://vjudge.net/problem/POJ-3450 Corporate Identity Time Limit: 3000MS   Memory Limit: 65536 ...

  6. POJ2774 Long Long Message —— 后缀数组 两字符串的最长公共子串

    题目链接:https://vjudge.net/problem/POJ-2774 Long Long Message Time Limit: 4000MS   Memory Limit: 131072 ...

  7. 【LeetCode】种花问题

    假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有.可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去. 给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花 ...

  8. promise介绍

    promise简介 Promise的出现,原本是为了解决回调地狱的问题.所有人在讲解Promise时,都会以一个ajax请求为例,此处我们也用一个简单的ajax的例子来带大家看一下Promise是如何 ...

  9. IntelliJ IDEA 同时启动多个Tomcat实例端口是会冲突

  10. listen and translation exercise 53

    It was hard work and there weren't any interesting things for him. You should be an expert with comp ...