P1904 天际线(离散化)

#include<iostream>
#include<set>
#include<map>
#include<algorithm>
#include<vector>
#include<cmath>
#include<climits>
#include<cstring>
#define int long long
const int N = 1e6+5;
using namespace std;
char* p1, * p2, buf[100000];
#define nc() (p1==p2 && (p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++)
int read()
{
	int x = 0, f = 1;
	char ch = nc();
	while (ch < 48 || ch>57)
	{
		if (ch == '-')
			f = -1;
		ch = nc();
	}
	while (ch >= 48 && ch <= 57)
		x = x * 10 + ch - 48, ch = nc();
	return x * f;
}
int a[N], b[N], c[N],f[N],h[N],ans[N];
signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int cnt = 0;
	int k = 0;
	while (cin >> a[++cnt] >> h[cnt] >> b[cnt]) {
		c[++k] = a[cnt];
		c[++k] = b[cnt];
	}
	sort(c + 1, c + 1 + k);
	for (int i = 1; i <= cnt; i++) {
		a[i] = lower_bound(c + 1, c + 1 + k, a[i])-c;
		b[i] = lower_bound(c + 1, c + 1 + k, b[i])-c;
		for (int j = a[i]; j < b[i]; j++)f[j] = max(f[j], h[i]);
	}
	int x = 1;
	ans[1] = c[1];
	for (int i = 2; i <= k; i++) {
		if (f[i] != f[i - 1]) {
			ans[++x] = f[i-1];
			ans[++x] = c[i];
		}
	}
	for (int i = 1; i <= x; i++)cout << ans[i] << " ";
	cout << 0;
	return 0;
}
posted @ 2025-02-12 14:33  郭轩均  阅读(11)  评论(0)    收藏  举报