题目链接: 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. centos 安装postgresql 完整版

    按步骤 执行命令即可: yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pgdg-c ...

  2. 【Leetcode-easy】Roman to Integer

    罗马数字转化为整数 * 1.基本数字 Ⅰ.X .C 中的任何一个.自身连用构成数目.或者放在大数的右边连用构成数目.都不能超过三个:放在大数的左边只能用一个: * 2.不能把基本数字 V .L .D ...

  3. Ctags快速入门

    Ctags快速入门 在vim下阅读代码,特别是阅读不熟悉的代码时,ctags是一个提高效率的强大的工具. 1. ctags是什么? ctags可以将代码中的函数.方法.类.变量和其他的标识符进行索引, ...

  4. 解决使用mybatis做批量操作时发生的异常:Parameter '__frch_item_0' not found. Available parameters are [list] 记录

    本文主要描述 使用mybatis进行批量更新.批量插入 过程中遇到的异常及总结: 首先贴出使用批量操作报的异常信息: java.lang.RuntimeException: org.mybatis.s ...

  5. IDEA:Application Server was not connected before run configuration stop, reason: Unable to ping 1099

    原文链接 : http://blog.csdn.net/x6582026/article/details/70807269 最近第一次玩IDEA时碰到tomcat启动问题:Application Se ...

  6. RQNOJ 188 购物问题:树形dp

    题目链接:https://www.rqnoj.cn/problem/188 题意: 商场以超低价格出售n个商品,购买第i个商品所节省的金额为w[i]. 为了防止亏本,有m对商品是不能同时买的.但保证商 ...

  7. laravel基础课程---13、数据库基本操作2(lavarel数据库操作和tp对比)

    laravel基础课程---13.数据库基本操作2(lavarel数据库操作和tp对比) 一.总结 一句话总结: 非常非常接近:也是分为两大类,原生SQL 和 数据库链式操作 学习方法:使用时 多看手 ...

  8. 在线接口管理工具-eoapi

    为了方便和前端沟通,临时在局域网搭建了一个接口管理工具,查了一些资料都说eoapi不错,那就试了一下: 1.安装 要在服务器或者自己的电脑,准备web环境,Linux可以是Apache/nginx , ...

  9. 构建基于虚拟用户的vsftpd服务器

    安装: [root@server ~]# yum install -y vsftpd [root@server ~]# rpm -ql vsftpd /etc/logrotate.d/vsftpd / ...

  10. BZOJ_3550_[ONTAK2010]Vacation&&BZOJ_1283:_序列_网络流解线性规划

    BZOJ_3550_[ONTAK2010]Vacation&&BZOJ_1283:_序列_网络流解线性规划 Description 给出一个长度为 的正整数序列Ci,求一个子序列,使得 ...