传送门: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 ); }
}

poj/OpenJ_Bailian - 2528 离散化+线段树的更多相关文章

  1. D - Mayor's posters POJ - 2528 离散化+线段树 区间修改单点查询

    题意 贴海报 最后可以看到多少海报 思路 :离散化大区间  其中[1,4] [5,6]不能离散化成[1,2] [2,3]因为这样破坏了他们的非相邻关系 每次离散化区间 [x,y]时  把y+1点也加入 ...

  2. 【POJ】2528 Mayor's posters ——离散化+线段树

    Mayor's posters Time Limit: 1000MS    Memory Limit: 65536K   Description The citizens of Bytetown, A ...

  3. 南阳理工 题目9:posters(离散化+线段树)

    posters 时间限制:1000 ms  |  内存限制:65535 KB 难度:6   描述 The citizens of Bytetown, AB, could not stand that ...

  4. SGU 180 Inversions(离散化 + 线段树求逆序对)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...

  5. hpu校赛--雪人的高度(离散化线段树)

    1721: 感恩节KK专场——雪人的高度 时间限制: 1 Sec  内存限制: 128 MB 提交: 81  解决: 35 [提交][状态][讨论版] 题目描述 大雪过后,KK决定在春秋大道的某些区间 ...

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

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

  7. 【bzoj4636】蒟蒻的数列 离散化+线段树

    原文地址:http://www.cnblogs.com/GXZlegend/p/6801379.html 题目描述 蒟蒻DCrusher不仅喜欢玩扑克,还喜欢研究数列 题目描述 DCrusher有一个 ...

  8. 离散化+线段树/二分查找/尺取法 HDOJ 4325 Flowers

    题目传送门 题意:给出一些花开花落的时间,问某个时间花开的有几朵 分析:这题有好几种做法,正解应该是离散化坐标后用线段树成端更新和单点询问.还有排序后二分查找询问点之前总花开数和总花凋谢数,作差是当前 ...

  9. Mayor's posters (离散化线段树+对lazy的理解)

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

随机推荐

  1. 基于Spark Mllib的Spark NLP库

    SparkNLP的官方文档 1>sbt引入: scala为2.11时 libraryDependencies += "com.johnsnowlabs.nlp" %% &qu ...

  2. iphone开发中使用nib(xib)文件的内存管理

    iphoneuinavigationcontrollercocoauiviewvariableswindows 在使用nib文件做界面开发的过程中,加载nib文件后,由于设置了outlet和deleg ...

  3. flask 与celery

    在flask 中使用celery 是特别简单的,celery官网都没有特别介绍如何使用. 使用celery首先要知道怎么配置celery.   1. 实例化celery celery = Celery ...

  4. ActiveX控件的消息处理函数

    首先切换到类视图 然后鼠标单击选中类(如果你要给ClockCtrl类添加事件,你就选中ClockCtrl类) PS:顺便多说一句,如果不用这种方法,而是手动添加,即使你的代码跟MFC添加的一模一样,那 ...

  5. 利用backtrace和ucontex定位segment错误

    C程序运行时,经常会碰到"segmentfault"错误.这是由于程序中非法访问内存导致的.当操作系统的内存保护机制发现进程访问了非法内存的时候会向此进程发送一个SIGSEGV信号 ...

  6. docker下载容器镜像

    下载镜像的命令非常简单,使用docker pull命令即可. 在docker的镜像索引网站上面,镜像都是按照用户名/镜像名的方式来存储的. 有一组比较特殊的镜像,比如ubuntu这类基础镜像,经过官方 ...

  7. thinkphp 公用函数

    thinkphp 的项目中指定了common/function.php的函数是公用函数. 不但php页面可以使用,模板文件也可以使用:

  8. swap function & copy-and-swap idiom

    在C++中,众所周知在一个资源管理类(例如含有指向堆内存的指针)中需要重新定义拷贝构造函数.赋值运算符以及析构函数(Big Three),在新标准下还可能需要定义移动构造函数和移动赋值运算符(Big ...

  9. SharpDX初学者教程第4部分:绘制三角形

    原文 http://www.johanfalk.eu/blog/sharpdx-beginners-tutorial-part-4-drawing-a-triangle 现在我们有了一个Direct3 ...

  10. iOS tableView优化

    iOS: Autolayout和UITableViewCell的动态高度 http://www.mgenware.com/blog/?p=507 优化UITableViewCell高度计算的那些事 h ...