poj2528(线段树区间替换&离散化)
题目链接: 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(线段树区间替换&离散化)的更多相关文章
- POJ-2528 Mayor's posters (线段树区间更新+离散化)
题目分析:线段树区间更新+离散化 代码如下: # include<iostream> # include<cstdio> # include<queue> # in ...
- HDU 1698 Just a Hook(线段树 区间替换)
Just a Hook [题目链接]Just a Hook [题目类型]线段树 区间替换 &题解: 线段树 区间替换 和区间求和 模板题 只不过不需要查询 题里只问了全部区间的和,所以seg[ ...
- HDU.1689 Just a Hook (线段树 区间替换 区间总和)
HDU.1689 Just a Hook (线段树 区间替换 区间总和) 题意分析 一开始叶子节点均为1,操作为将[L,R]区间全部替换成C,求总区间[1,N]和 线段树维护区间和 . 建树的时候初始 ...
- hdu1698(线段树区间替换模板)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 题意: 第一行输入 t 表 t 组测试数据, 对于每组测试数据, 第一行输入一个 n , 表示 ...
- POJ2528:Mayor's posters(线段树区间更新+离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- POJ-2528 Mayor's posters(线段树区间更新+离散化)
http://poj.org/problem?id=2528 https://www.luogu.org/problem/UVA10587 Description The citizens of By ...
- POJ 2528 Mayor's posters (线段树区间更新+离散化)
题目链接:http://poj.org/problem?id=2528 给你n块木板,每块木板有起始和终点,按顺序放置,问最终能看到几块木板. 很明显的线段树区间更新问题,每次放置木板就更新区间里的值 ...
- poj 2528 线段树区间修改+离散化
Mayor's posters POJ 2528 传送门 线段树区间修改加离散化 #include <cstdio> #include <iostream> #include ...
- POJ 2528——Mayor's posters——————【线段树区间替换、找存在的不同区间】
Mayor's posters Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Sub ...
随机推荐
- ABAP 程序运行时间记录表
自建表记录程序运行时间,测试程序效率,可作为系统优化工具.
- main方法的参数
敲例子的时候无意中把主方法的参数给落下了,当时没有发现,保存之后就去编译,运行了,通常情况下编译没有错误那胜利就在掌握之中了,没想到这次我竟然在"不一般"的行列中,编译无误,运行出 ...
- Spring Aop切点
切点用于准确定位应该在什么地方应用切面的通知.通知和切点是切面的最基本的元素.在Spring AOP中要使用AspectJ的切点表达式来定义切点.下面我们列出Spring AOP所支持的AspectJ ...
- Java for LeetCode 121 Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- Java拓展教程:文件DES加解密
Java拓展教程:文件加解密 Java中的加密解密技术 加密技术根据一般可以分为对称加密技术和非对称加密技术.对称加密技术属于传统的加密技术,它的加密和解密的密钥是相同的,它的优点是:运算速度快,加密 ...
- (linux)BSP(板上支持包)概述
1. BSP概述 BSP即Board Support Package,板级支持包. 它来源于嵌入式操作系统与硬件无关的设计思想,操作系统被设计为运行在虚拟的硬件平台上. 对于具体的硬件平台,与硬件相关 ...
- iOS开发中集成Reveal
[转]http://blog.devzeng.com/blog/ios-reveal-integrating.html 配置方式一简介有效. Reveal 是一个界面调试工具.使用Reveal,我们可 ...
- TopCoder<SRM>上的一道1100分的题目解析附代码
首先我们来简单看一下这道题的statement Problem Statement Note that in the following problem statement, all quo ...
- 嵌入式选择与L1正则化
http://blog.csdn.net/irene_loong/article/details/73741521
- Linux-DHCP服务器的搭建
DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)通常被应用在大型的局域网络环境中,主要作用是集中的管理.分配IP地址,使网络环境中的主机动态的获得I ...