链接:https://ac.nowcoder.com/acm/contest/945/F
来源:牛客网
题目描述
Farmer John has taken his cows on a trip to the city! As the sun sets, the cows gaze at the city horizon and observe the beautiful silhouettes formed by the rectangular buildings.
The entire horizon is represented by a number line with N (1 ≤ N ≤ 40,000) buildings. Building i's silhouette has a base that spans locations Ai through Bi along the horizon (1 ≤ Ai < Bi ≤ 1,000,000,000) and has height Hi (1 ≤ Hi ≤ 1,000,000,000). Determine the area, in square units, of the aggregate silhouette formed by all N buildings.
输入描述:
Line 1: A single integer: N Lines 2..N+1: Input line i+1 describes building i with three space-separated integers: Ai, Bi, and Hi
输出描述:
Line 1: The total area, in square units, of the silhouettes formed by all N buildings
示例1
输入
复制
4 2 5 1 9 10 4 6 8 2 4 6 3
输出
复制
16
说明
The first building overlaps with the fourth building for an area of 1 square unit, so the total area is just 31 + 14 + 22 + 23 - 1 = 16.
主要是学习一个set的用法
#include <bits/stdc++.h>
using namespace std;
const int N = 4e4+5;
struct Node
{
int p,h,id,flag;
bool operator <(const Node &tmp)const{
if(p==tmp.p) return h>tmp.h;
return p<tmp.p;
}
}a[N<<1];
set<pair<int, int> >s;
set<pair<int ,int> >::iterator it;
int main(){
int n,l,r,h,cnt=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d %d %d",&l,&r,&h);
a[cnt++]={l,h,i,0};
a[cnt++]={r,h,i,1};
}
sort(a,a+cnt);
int last=a[0].p;
s.insert({a[0].h,a[0].id});
long long sum=0;
for(int i=1;i<cnt;i++)
{
if(s.begin()!=s.end())
{
it=s.end();--it;
sum+=1ll*it->first*(a[i].p-last);
}
if(a[i].flag) it=s.find({a[i].h,a[i].id}),s.erase(it);
else s.insert({a[i].h,a[i].id});
last=a[i].p;
}
printf("%lld\n",sum);
return 0;
}