hihoCoder

#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
");
		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
", ans);
	
	return 0;
}










原文地址:https://www.cnblogs.com/jhcelue/p/6908782.html