题目
传送门
约翰带着奶牛去都市观光。在落日的余晖里,他们看到了一幢接一幢的摩天高楼的轮廓在地平线 上形成美丽的图案。以地平线为 X 轴,每幢高楼的轮廓是一个位于地平线上的矩形,彼此间可能有 重叠的部分。奶牛一共看到了 N 幢高楼,第 i 幢楼的高度是 Hi,两条边界轮廓在地平线上的坐标是 Ai 到 Bi。请帮助奶牛们计算一下,所有摩天高楼的轮廓覆盖的总面积是多少。
另一种理解方式:
有一个数列,初始值均为0,他进行N次操作,每次将数列[ai,bi)这个区间中所有比Hi小的数改为Hi,他想知道N次操作后数列中所有元素的和。
(个人认为前者更好)
题解
本题解提供两种解法
第一种就是简化版矩形面积求交,扫描线的经典题;具体讲解可以看这里
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define ll long long
using namespace std;
const int maxn=1000001;
ll n;
ll x1,x2,y1,y2;
struct Tree{
ll l,r,c;
ll cnt,fl,fr;//cnt 差值
}tree[maxn<<2];
struct Line{
ll x,y1,y2;
ll f;//+-1:矩形左边还是右边的边
}line[maxn<<1];
ll y[maxn];
bool cmp(Line a,Line b) {return a.x<b.x;}
void build_tree(ll l,ll r,ll rt)
{
tree[rt].l=l; tree[rt].r=r;
tree[rt].c=tree[rt].cnt=0;
tree[rt].fl=y[l];
tree[rt].fr=y[r];
if (l+1==r) return;//叶子结点
ll mid=(l+r)>>1;
build_tree(l,mid,rt<<1);
build_tree(mid,r,rt<<1|1);
}
void calc(ll rt)
{
if (tree[rt].c>0)
{
tree[rt].cnt=tree[rt].fr-tree[rt].fl;
return;
}
if (tree[rt].l+1==tree[rt].r) tree[rt].cnt=0;//叶子节点
else tree[rt].cnt=tree[rt<<1].cnt+tree[rt<<1|1].cnt;
}
void update(ll rt,Line e)
{
if (e.y1==tree[rt].fl && e.y2==tree[rt].fr)
{
tree[rt].c+=e.f;
calc(rt);
return;
}
if (e.y2<=tree[rt<<1].fr) update(rt<<1,e);
else if (e.y1>=tree[rt<<1|1].fl) update(rt<<1|1,e);
else
{
Line tmp=e;
tmp.y2=tree[rt<<1].fr;
update(rt<<1,tmp);
tmp=e;
tmp.y1=tree[rt<<1|1].fl;
update(rt<<1|1,tmp);
}
calc(rt);
}
int main()
{
// freopen("[USACO07OPEN]城市的地平线.in","r",stdin);
ll ans=0;
scanf("%lld",&n);
ll tot=0;
for (ll i=1; i<=n; i++)
{
y1=0;
scanf("%lld%lld%lld",&x1,&x2,&y2);
line[++tot].x=x1;
line[tot].y1=y1;
line[tot].y2=y2;
line[tot].f=1;
y[tot]=0;
line[++tot].x=x2;
line[tot].y1=y1;
line[tot].y2=y2;
line[tot].f=-1;
y[tot]=y2;
}
sort(y+1,y+1+tot);
sort(line+1,line+1+tot,cmp);
build_tree(1,tot,1);
// printf("rt l r c h cnt\n");
// for (ll rt=1; rt<=(n<<1); rt++) printf("%lld: %lld %lld %lld %.0lf %.0lf\n",rt,tree[rt].l,tree[rt].r,tree[rt].c,tree[rt].fr,tree[rt].cnt);
update(1,line[1]);
ll res=0;
for (ll i=2; i<=tot; i++)
{
res+=tree[1].cnt*(line[i].x-line[i-1].x);
update(1,line[i]);
}
printf("%lld",res);
return 0;
}
第二种也是扫描线的思想,具体用优先队列实现,参考了楼下的代码
以区间的左端点为优先级建立优先队列;不在范围内的区间就pop掉;新出现建筑物如果符合范围就入队 ;
最后像扫描线一样,每次取出一小段计算面积
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int maxn=100001;
int n,m,p[maxn];
struct Node{
int x,y,v;
bool operator < (Node a) const {
return this -> v < a.v;}
}a[maxn];
priority_queue <Node> q;
bool cmp(Node a,Node b) {return a.x<b.x;}
bool cmp1(int a,int b) {return a<b;}
int main()
{
scanf("%d",&n);
for (int i=1; i<=n; i++)
{
int x,y,h; scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].v);
p[i*2]=a[i].y;
p[i*2-1]=a[i].x;
}
sort(a+1,a+1+n,cmp);
sort(p+1,p+1+2*n,cmp1);
int tot=0;//已经放置的建筑物
long long ans=0;
for (int i=1; i<n*2; i++)
{
while (!q.empty() && q.top().y<=p[i]) q.pop();//不在范围内pop
while (p[i]<=a[tot+1].x && a[tot+1].x<p[i+1])//新出现的在范围内的入队
q.push(a[++tot]);
if (!q.empty())
ans+=(long long)(p[i+1]-p[i])*q.top().v;
}
printf("%lld",ans);
return 0;
}
总结
有许多细节需要注意。long long之类的;
昨天一天心神不宁;没有做出一道题。不要被周围环境所干扰,每天保持积极向上的心态;
要多码题啊