YACS2022年6月月赛甲组
作者:互联网
T1:矩形覆盖
用 std::set
或线段树来维护扫描线即可
代码实现
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 1; i <= (n); ++i)
using std::cin;
using std::cout;
using std::multiset;
using ll = long long;
struct Node {
ll x, h;
int type;
bool operator<(const Node &o) const {
return x < o.x;
}
} a[600005];
int main() {
int n;
cin >> n;
rep(i, n) {
int s, t, h;
cin >> s >> t >> h;
a[i*2-1] = {s, h, 0}; // 0 表示左边界
a[i*2] = {t, h, 1}; // 1 表示右边界
}
int n2 = n*2;
std::sort(a+1, a+n2+1);
ll ans = 0;
ll last = 0; // 扫描线上一次扫过的矩形高度的位置
multiset<ll> st; // 维护当前存活着的的矩形高度
st.insert(0);
for (int i = 1; i <= n2;) {
ll x = a[i].x;
ans += *st.rbegin()*(a[i].x-last);
last = a[i].x;
while (i <= n2 and a[i].x == last) {
if (a[i].type) st.erase(st.find(a[i].h));
else st.insert(a[i].h);
++i;
}
}
cout << ans << '\n';
return 0;
}
标签:std,int,rep,st,甲组,扫描线,矩形,YACS2022 来源: https://www.cnblogs.com/Melville/p/16410547.html