题目链接: 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. Java for LeetCode 086

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  2. ruby 字符串

    字符串处理函数 1.返回字符串的长度 str.length => integer 2.判断字符串中是否包含另一个串 str.include? other_str => true or fa ...

  3. File.basename

    File.basename函数 返回filename中的最后一条斜线后面的部分.若给出了参数suffix且它和filename的尾部一致时,该方法会将其删除并返回结果. 例: p File.basen ...

  4. 7-3 堆栈模拟队列(25 point(s)) 【数据结构】

    7-3 堆栈模拟队列(25 point(s)) 设已知有两个堆栈S1和S2,请用这两个堆栈模拟出一个队列Q. 所谓用堆栈模拟队列,实际上就是通过调用堆栈的下列操作函数: int IsFull(Stac ...

  5. 让我们再谈谈 iOS 安全

    前言 安全方面的话题总是聊不完的.这不,国外一家有名的专门攻击别人的安全公司 Hacking Team 自己被 Hack 了,结果有 400 多 G 的攻击资料泄漏出来,包括一些 0-day 的漏洞. ...

  6. 分享知识-快乐自己:什么是MVC

    1.什么是mvc: Model View Controller,是模型-视图-控制器的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离的方法组织代码,将业务逻辑聚集到一个组件里,在改进和个性 ...

  7. smack

    XMPP/Smack/Openfire javax.net.ssl.SSLException: Received fatal alert: internal_error 解决: 1.在链接openfi ...

  8. POJ3728The merchant (倍增)(LCA)(DP)(经典)(||并查集压缩路径?)

    There are N cities in a country, and there is one and only one simple path between each pair of citi ...

  9. 1135 Is It A Red-Black Tree(30 分)

    There is a kind of balanced binary search tree named red-black tree in the data structure. It has th ...

  10. bzoj 4671 异或图 —— 容斥+斯特林反演+线性基

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4671 首先,考虑容斥,就是设 \( t[i] \) 表示至少有 \( i \) 个连通块的方 ...