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 ...
随机推荐
- 在Spring中基于JDBC进行数据访问时如何控制超时
超时分类 超时根据作用域可做如下层级划分: Transaction Timeout > Statement Timeout > JDBC Driver Socket Timeout Tra ...
- 3D 图片播放焦点图插件Adaptor
在线演示 本地下载
- 架构设计:系统间通信(34)——被神化的ESB(上)
1.概述 从本篇文章开始,我们将花一到两篇的篇幅介绍ESB(企业服务总线)技术的基本概念,为读者们理清多个和ESB技术有关名词.我们还将在其中为读者阐述什么情况下应该使用ESB技术.接下来,为了加深读 ...
- 4.7 希尔(shell)排序法
4-7 ShellSort.c #include <stdio.h> #include "4-1 CreateData.c" //生成随机数的函数 #define AR ...
- 鸟哥的Linux私房菜-第10/11/12/13章(vim程序编辑器、学习bash、正则表达式与文件格式化处理、学习Shell Scripts)
第10章 vim程序编辑器 可以将vim看做vi的进阶版本,vim可以用颜色或底线等方式来显示出一些特殊的信息. 为何要学习vim?因为: a. 所有的 Unix Like 系统都会内建 vi 文书编 ...
- pycharm解决Inconsistent indentation:mix of tabs and spaces
- Python 使用正则表达式验证密码必须包含大小写字母和数字
校验密码是否合法的程序. 输入一个密码 1.长度5-10位 2.密码里面必须包含,大写字母.小写字母和数字 3.最多输入5次 ===================================== ...
- 国际电话号码的区号mysql数据表
-- phpMyAdmin SQL Dump-- version 3.5.2-- http://www.phpmyadmin.net---- Host: localhost-- Generation ...
- storm相关技术
There are two kinds of nodes on a Storm cluster: the master node and the worker nodes. 有两种节点,主节点和wor ...
- Java之动态代理简介
图截于<大话设计模式> Proxy模式是常用的设计模式,其特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等. 用户可以更 ...