#1079 : 离散化

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描写叙述

小Hi和小Ho在回国之后,又一次过起了朝7晚5的学生生活。当然了。他们还是在一直学习着各种算法~

这天小Hi和小Ho所在的学校举办社团文化节,各大社团都在宣传栏上贴起了海报,可是贴来贴去,有些海报就会被其它社团的海报所遮挡住。看到这个场景,小Hi便产生了这种一个疑问——最后究竟能有几张海报还能被看见呢?

于是小Ho肩负起了解决问题的责任:由于宣传栏和海报的高度都是一样的。所以宣传栏能够被视作长度为L的一段区间。且有N张海报依照顺序依次贴在了宣传栏上。当中第i张海报贴住的范围能够用一段区间[a_i, b_i]表示。当中a_i, b_i均为属于[0, L]的整数。而一张海报能被看到当且仅当存在长度大于0的一部分没有被后来贴的海报所遮挡住。

那么问题就来了:到底有几张海报能被看到呢?

提示一:正确的认识信息量

提示二:小Hi大讲堂之线段树的节点意义

输入

每一个測试点(输入文件)有且仅有一组測试数据。

每组測试数据的第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。表示总共同拥有多少张海报能被看到。

例子输入
5 10
4 10
0 2
1 6
5 9
3 4
例子输出
5

待理解。。。

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 - 离散化 (线段树 + 离散化)的更多相关文章

  1. hihoCoder:#1079(线段树+离散化)

    题目大意:给n个区间,有的区间可能覆盖掉其他区间,问没有完全被其他区间覆盖的区间有几个?区间依次给出,如果有两个区间完全一样,则视为后面的覆盖前面的. 题目分析:区间可能很长,所以要将其离散化.但离散 ...

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

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  3. poj 2528 Mayor's posters(线段树+离散化)

    /* poj 2528 Mayor's posters 线段树 + 离散化 离散化的理解: 给你一系列的正整数, 例如 1, 4 , 100, 1000000000, 如果利用线段树求解的话,很明显 ...

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

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

  5. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

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

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

  7. [UESTC1059]秋实大哥与小朋友(线段树, 离散化)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...

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

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

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

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

  10. poj 2528 Mayor's posters 线段树+离散化技巧

    poj 2528 Mayor's posters 题目链接: http://poj.org/problem?id=2528 思路: 线段树+离散化技巧(这里的离散化需要注意一下啊,题目数据弱看不出来) ...

随机推荐

  1. 363 Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

  2. 321 Create Maximum Number 拼接最大数

    已知长度分别为 m 和 n 的两个数组,其元素由 0-9 构成,直观地表示两个自然数各位上的数字.现在从这两个数组中选出 k (k <= m + n) 个数字拼接成一个新的数,要求从同一个数组中 ...

  3. Spring Boot (23) Lettuce Redis

    Spring Boot除了支持常见的ORM框架外,更是对常用的中间件提供了非常好的封装,随着SpringBoot2.x的到来,支持的组件也越来越丰富,也越来越成熟,其中对Redis的支持不仅仅是它丰富 ...

  4. 新认知之WinForm窗体程序

    Windows应用程序和控制台应用程序有很大的区别 >Form1.cs  :窗体文件,程序员对窗体编写的代码一般都存放在这个文件中. >Form1.Designer.cs :窗体设计文件, ...

  5. Listview模板

    每次写listview都要翻以前的代码,好烦.所以记下模板,方便下次的使用. xml文件部分代码: <ListView android:id="@+id/listview" ...

  6. Angular——tab切换案例

    基本介绍 angular框架下的tab切换,相比较于之前的纯js写的代码,有一个很大的特点就是以数据为驱动,基本上不用搜索dom元素就可以实现效果 基本使用 (1)导航部分使用的是的状态使用的是ng- ...

  7. 仿win8磁贴界面以及功能

    做移动产品界面占很大的一部分,同时也是决定一款产品好的的重要因素,最近看见有人放win8的界面效果,搜了两款,一款是只是仿界面没有特效,另一款是自定义组件能够实现反转效果,今天分析一下这两类界面. 仿 ...

  8. windows server2003 多用户登陆问题解决办法

    windows server2003 多用户登陆问题解决办法 Windows Server远程登陆默认情况下只允许同时有两个用户登陆,超过两个用户会提示"超出最大连接数". 要解决 ...

  9. Git 学习笔记(W,I,P)

    /*********************** 个人知识水平有限 有任何错误请尽情指出!!谢谢啦 我的Github 求粉 ミ ゚Д゚彡 ***********************/ 参考教程:廖 ...

  10. GitHub代码托管平台搭建

    GitHub代码托管平台搭建 注册账户以及创建仓库 要想使用github第一步当然是注册github账号了, github官网地址:https://github.com/. 之后就可以创建仓库了(免费 ...