hihoCoder - 1079 - 离散化 (线段树 + 离散化)
#1079 : 离散化
- 例子输入
-
5 10
4 10
0 2
1 6
5 9
3 4 - 例子输出
-
5
描写叙述
小Hi和小Ho在回国之后,又一次过起了朝7晚5的学生生活。当然了。他们还是在一直学习着各种算法~
这天小Hi和小Ho所在的学校举办社团文化节,各大社团都在宣传栏上贴起了海报,可是贴来贴去,有些海报就会被其它社团的海报所遮挡住。看到这个场景,小Hi便产生了这种一个疑问——最后究竟能有几张海报还能被看见呢?
于是小Ho肩负起了解决问题的责任:由于宣传栏和海报的高度都是一样的。所以宣传栏能够被视作长度为L的一段区间。且有N张海报依照顺序依次贴在了宣传栏上。当中第i张海报贴住的范围能够用一段区间[a_i, b_i]表示。当中a_i, b_i均为属于[0, L]的整数。而一张海报能被看到当且仅当存在长度大于0的一部分没有被后来贴的海报所遮挡住。
那么问题就来了:到底有几张海报能被看到呢?
输入
每一个測试点(输入文件)有且仅有一组測试数据。
每组測试数据的第1行为两个整数N和L,分别表示总共贴上的海报数量和宣传栏的宽度。
每组測试数据的第2-N+1行,依照贴上去的先后顺序。每行描写叙述一张海报。当中第i+1行为两个整数a_i, b_i。表示第i张海报所贴的区间为[a_i, b_i]。
对于100%的数据。满足N<=10^5,L<=10^9,0<=a_i<b_i<=L。
输出
对于每组測试数据,输出一个整数Ans。表示总共同拥有多少张海报能被看到。
待理解。。。
AC代码:
#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std; const int maxn = 200005; struct Poster {
int l, r;
}pt[maxn]; struct Tree {
int l ,r;
int c;
int mid() { return (l + r) >> 1; }
}node[maxn << 2]; int d[maxn];
int c; int used[maxn]; void build(int l, int r, int rt) {
node[rt].l = l;
node[rt].r = r;
node[rt].c = 0;
if(l + 1 == r) {
return;
}
int mid = node[rt].mid();
build(l, mid, rt << 1);
build(mid, r, rt << 1 | 1);
} void update(int l, int r, int c, int rt) {
if(l <= node[rt].l && node[rt].r <= r) {
node[rt].c = c;
return;
}
int mid = node[rt].mid();
if(node[rt].c != -1) { //有新的海报贴进来,所以说要将当前这一块往下更新
node[rt << 1].c = node[rt << 1 | 1].c = node[rt].c;
node[rt].c = -1;
} //对于当前区间的三种情况
if (r <= mid) update(l, r, c, rt << 1);
else if (l >= mid) update(l, r, c, rt << 1 | 1);
else
{
update(l, mid, c, rt << 1);
update(mid, r, c, rt << 1 | 1);
}
} void query(int rt)
{
if (node[rt].c != -1)
{
used[node[rt].c] = 1;
return;
}
query(rt << 1);
query(rt << 1 | 1);
} int main() {
int n, l;
scanf("%d %d", &n, &l); if(n == 0) {
printf("0\n");
return 0;
} c = 0;
for(int i = 1; i <= n; i ++) {
scanf("%d %d", &pt[i].l, &pt[i].r);
d[c ++] = pt[i].l;
d[c ++] = pt[i].r;
used[i] = 0;
} sort(d, d + c); c = unique(d, d + c) - d; //去重 // for(int i = 0; i < c; i ++) {
// cout << d[i] << " ";
// }
// cout << endl; build(0, 2 * c + 1, 1); for(int i = 1; i <= n; i ++) { //模拟离散化贴海报的过程。从1到n张海报
int x = lower_bound(d, d + c, pt[i].l) - d;
int y = lower_bound(d, d + c, pt[i].r) - d;
// cout << x << " " << y << " " << (x << 1) << " " << (y << 1 | 1) << endl;
update(x << 1, y << 1 | 1, i, 1);
} query(1);
int ans = 0;
for (int i = 1; i <= n; i ++) ans += used[i];
printf("%d\n", ans); return 0;
}
hihoCoder - 1079 - 离散化 (线段树 + 离散化)的更多相关文章
- hihoCoder:#1079(线段树+离散化)
题目大意:给n个区间,有的区间可能覆盖掉其他区间,问没有完全被其他区间覆盖的区间有几个?区间依次给出,如果有两个区间完全一样,则视为后面的覆盖前面的. 题目分析:区间可能很长,所以要将其离散化.但离散 ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- poj 2528 Mayor's posters(线段树+离散化)
/* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...
- 南阳理工 题目9:posters(离散化+线段树)
posters 时间限制:1000 ms | 内存限制:65535 KB 难度:6 描述 The citizens of Bytetown, AB, could not stand that ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
- SGU 180 Inversions(离散化 + 线段树求逆序对)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=180 解题报告:一个裸的求逆序对的题,离散化+线段树,也可以用离散化+树状数组.因为 ...
- [UESTC1059]秋实大哥与小朋友(线段树, 离散化)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...
- 【POJ】2528 Mayor's posters ——离散化+线段树
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Description The citizens of Bytetown, A ...
- hpu校赛--雪人的高度(离散化线段树)
1721: 感恩节KK专场——雪人的高度 时间限制: 1 Sec 内存限制: 128 MB 提交: 81 解决: 35 [提交][状态][讨论版] 题目描述 大雪过后,KK决定在春秋大道的某些区间 ...
- poj 2528 Mayor's posters 线段树+离散化技巧
poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...
随机推荐
- Vue初识:一个前端萌新的总结
一.前言 时隔三年,记得第一次写博客还是2015年了,经过这几年的洗礼,我也从一个后端的小萌新变成现在略懂一点点知识的文青.如今对于前端的东东也算有一知半解,个人能力总的来说,也能够独立开发产品级项目 ...
- Laravel5.1学习笔记20 EloquentORM 关系
Eloquent: Relationships Introduction Defining Relationships One To One One To Many Many To Many Has ...
- HTML和CSS网页开发基础
一 HTML文档结构 HTML文档结构:<html>.<head>.<title>.<body>构成HTML页面中最基本的元素. HTML常用标记:1. ...
- Android Eclipse 安装教程 2016.06.13版
2016.8.16修改 第一步,也是最为关键的一步——修改hosts文件 为什么说是最关键的一步呢?因为接下来的操作,我们都需要连接google网,也就是要连接国外的网站.一般情况下,国外的网站是无法 ...
- Android集成二维码扫描功能
文章转载自 https://github.com/yipianfengye/android-zxingLibrary 在具体介绍该扫描库之前我们先看一下其具体的使用方式,看看是不是几行代码就可以集成 ...
- Win32基础知识整理
1.定义字符串 在资源新建String table,增加新字符串: (win32加载) TCHAR tcIDName[255]=_T(""); LoadString(hInstan ...
- html5——动画
基本介绍 /*执行函数gun,执行时间,重复执行,反向执行,匀速执行,延迟执行时间*/ animation: gun 4s infinite alternate linear 5s; 动画序列 1.g ...
- SQL基本操作——select into与临时表
SELECT INTO 语句从一个表中选取数据,然后把数据插入另一个表中,常用于创建表的备份复件或者用于对记录进行存档. --制作 "Persons" 表的备份复件: SELECT ...
- C语言保留字
数值变量相关: int float double char long short unsigned signed 储存说明符 const 用于声明常量 static用于限制变量/函数的作用范围等等 e ...
- (三)Python 学习第三天--GUI桌面项目
(代码参考了别人的代码,只做学习用途!!!最近因为写论文,好久没有记录,好内疚...今天学习了一个小案例,做一下) 主要使用模块:tkinter 代码如下: from tkinter import * ...