传送门:http://bailian.openjudge.cn/practice/2528?lang=en_US

//http://poj.org/problem?id=2528

题意:

给你n长海报,每张海报在墙上贴的区间范围是l,r

问你这n张海报贴完后,可以看见的海报数量是多少

题解:

离散化+线段树

因为l,r的数据范围是1e7,而题目只给了64MB的空间,直接存的话空间会炸掉,所以需用用到离散化的技巧

然后按照端点单点更新即可

现在重新写这题发现这个题坑点在于每一个点他都代表一个长度为1的东东,所以我们普通的离散话会出问题

1-10 1-4 5-10
1-10 1-4 6-10

譬如 如上这组例子

所以我们离散化时做一下优化, 如果相邻间数字间距大于1时我们就在其中加上任意一个数字

这样离散话下来后做线段树就可以过
这组数据
3
5 6
4 5
6 8
3
1 10
1 3
6 10
5
1 4
2 6
8 10
3 4
7 10

这组数据

答案是3,3,4

代码:

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
#define ls rt<<1
#define rs rt<<1|1
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define bug printf("*********\n")
#define FIN freopen("input.txt","r",stdin);
#define FON freopen("output.txt","w+",stdout);
#define IO ios::sync_with_stdio(false),cin.tie(0)
#define debug1(x) cout<<"["<<#x<<" "<<(x)<<"]\n"
#define debug2(x,y) cout<<"["<<#x<<" "<<(x)<<" "<<#y<<" "<<(y)<<"]\n"const double eps = 1e-8;
const int mod = 1e9 + 7;
const int maxn = 3e5 + 5;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3;
struct node {int l, r;
} q[maxn];
int col[maxn];
int vis[maxn];
int ans;
void push_down(int rt) {if(col[rt] != -1) {col[ls] = col[rs] = col[rt];col[rt] = -1;}return;
}
void update(int L, int R, int c, int l, int r, int rt) {if(L <= l && r <= R) {col[rt] = c;return;}push_down(rt);int mid = (l + r) >> 1;if(L <= mid) update(L, R, c, lson);if(R > mid) update(L, R, c, rson);
}
void query(int l, int r, int rt) {if(col[rt] != -1) {if(!vis[col[rt]]) ans++;vis[col[rt]] = true;return;}if(l == r) return;int mid = (l + r) >> 1;query(lson);query(rson);
}
int x[maxn];
int main() {int T;scanf("%d", &T);while(T--) {int cnt = 0;int n;scanf("%d", &n);for(int i = 0; i < n; i++) {scanf("%d%d", &q[i].l, &q[i].r);x[cnt++] = q[i].l;x[cnt++] = q[i].r;}sort(x, x + cnt);int m = 1;for(int i = 1; i < cnt; i++) {if(x[i] != x[i - 1]) {x[m++] = x[i];}}for(int i = m - 1; i >= 1; i--) {if(x[i] != x[i - 1] + 1) {x[m++] = x[i - 1] + 1;}}sort(x, x + m);memset(col, -1, sizeof(col));for(int i = 0; i < n; i++) {int l = lower_bound(x, x + m, q[i].l) - x;int r = lower_bound(x, x + m, q[i].r) - x;update(l, r, i, 0, m, 1);}ans = 0;memset(vis, false, sizeof(vis));query(0, m, 1);printf("%d\n",ans );}
}

转载于:https://www.cnblogs.com/buerdepepeqi/p/10864798.html

poj/OpenJ_Bailian - 2528 离散化+线段树相关推荐

  1. poj 2528 离散化+线段树 hdu 1698 线段树 线段树题目类型一:染色计数 外加离散化

    第一次听到离散化是今年省赛的时候,一道矩形并的题,很水,就两个矩形... 今天再去做线段树已经发现离散化忘得差不多了...水逼的悲哀啊... 先看简单点的hdu 1698 http://acm.hdu ...

  2. 【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树

    [BZOJ1645][Usaco2007 Open]City Horizon 城市地平线 Description Farmer John has taken his cows on a trip to ...

  3. [牛客网#35D 树的距离]离散化+线段树合并

    [牛客网#35D 树的距离]离散化+线段树合并 分类:Data Structure SegMent Tree Merge 1. 题目链接 [牛客网#35D 树的距离] 2. 题意描述 wyf非常喜欢树 ...

  4. 850. 矩形面积 II:扫描线+离散化+线段树

    Difficulty: hard 标签: 扫描线, 离散化, 线段树 题目链接 力扣 题目解析 面试代码 /** x轴方向使用扫描线,y轴方向使用线段树维护扫描线的长度和每个区间覆盖的次数.由于y轴方 ...

  5. Mayor's posters POJ - 2528 (离散化+线段树)

    题意: 在1~10000000这个区间中读取n个海报的区间信息,后面的海报会覆 盖前面的海报,问最后能看到几张海报.(本题是一道bug题下面会提) 题目: The citizens of Byteto ...

  6. 离散化/线段树 (POJ - 2528 Mayor's posters)

    Mayor's posters https://vjudge.net/problem/POJ-2528#author=szdytom 线段树 + 离散化 讲解:https://blog.csdn.ne ...

  7. POJ - 2528 Mayor's posters (浮水法+线段树/离散化+线段树)

    题目链接 题意: n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000) .求出最后还能看见多少张海报. 分析1 离散 ...

  8. POJ Mayor's posters——线段树+离散化

    原文:http://blog.163.com/cuiqiongjie@126/blog/static/85642734201261151553308/ 大致题意: 有一面墙,被等分为1QW份,一份的宽 ...

  9. POJ 3667 Hotel(线段树)

    POJ 3667 Hotel 题目链接 题意:有n个房间,如今有两个操作 1.找到连续长度a的空房间.入住,要尽量靠左边,假设有输出最左边的房间标号,假设没有输出0 2.清空[a, a + b - 1 ...

最新文章

  1. proftpd登陆速度慢的解决[转]
  2. 【转】02.Dicom 学习笔记-DICOM C-Find 消息服务
  3. 《暗时间》-----摘记
  4. ASP.NET MVC5 与EF6学习系列
  5. 考试君 - 基于.NET 5语言的Furion框架开发在线考试系统
  6. mysql 字符串函数
  7. BZOJ 2469 [中山市选2010]简单数谜
  8. 信号与系统(六)z变换
  9. C++基础学习-33模板全特化、偏特化(局部特化)
  10. 设计一个抽象类图形类,在该类中包含有至少两个抽象方法求周长和求面积,分别定义圆形类、长方形类、正方形类、三角形类来继承图形类,并实现上述两个方法
  11. word域锁定和更新
  12. Android Canvas.scale缩放
  13. H3C   VALN-MSTP-VRRP实验日记
  14. NUIST2022级第一周集训
  15. 天池大赛——天猫用户复购预测
  16. vue3.x 重复点击路由报错
  17. Intellij IDEA 的激活方法
  18. 国内可以为程序员提供兼职的平台有哪些?
  19. Matlab归一化实现
  20. 基于php的bbs论坛教程,基于PHPMySQL技术BBS论坛

热门文章

  1. go语言离线查看说明文档
  2. 构造函数和析构函数深拷贝和浅拷贝
  3. Dockerfile: no such file or directory
  4. Tomcat设置虚拟目录的方法, 不修改server.xm
  5. 不用eclipse创建第一个servlet
  6. linux编程:getenv,putenv,setenv
  7. ioctl中的ifconf ifreg 结构
  8. STM32开发 -- YModem详解
  9. python数据分析第七章实训3_《利用Python进行数据分析·第2版》第7章 数据清洗和准备7.1 处理缺失数据7.2 数据转换7.3 字符串操作7.4 总结...
  10. java的flush方法_Java中的BufferedWriter flush()方法及示例