标签:mes code bit end signed str const sign 矩形面积并
给定 \(N\leq 1000\) 个矩形,坐标绝对值 \(\leq 10^8\),求矩形面积并。
难度:L3
离散化后用二维前缀和处理出每个小方格被覆盖的次数,最后扫一遍所有小方格,如果覆盖次数 \(\geq 1\),就算上这个小方格的面积
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 4005;
struct rect {
int x1,y1,x2,y2;
} r[N];
int n,px[N],py[N],s[N][N],idx,idy;
map <int,int> mx,my;
signed main() {
ios::sync_with_stdio(false);
cin>>n;
for(int i=1;i<=n;i++) {
cin>>r[i].x1>>r[i].y2>>r[i].x2>>r[i].y1;
mx[r[i].x1]++;
mx[r[i].x2]++;
my[r[i].y1]++;
my[r[i].y2]++;
}
for(auto i=mx.begin();i!=mx.end();i++) i->second=++idx, px[idx]=i->first;
for(auto i=my.begin();i!=my.end();i++) i->second=++idy, py[idy]=i->first;
++idx;
++idy;
for(int i=1;i<=n;i++) {
r[i].x1=mx[r[i].x1]+1;
r[i].x2=mx[r[i].x2]+1;
r[i].y1=my[r[i].y1]+1;
r[i].y2=my[r[i].y2]+1;
s[r[i].x1][r[i].y1]++;
s[r[i].x2][r[i].y2]++;
s[r[i].x1][r[i].y2]--;
s[r[i].x2][r[i].y1]--;
}
for(int i=1;i<=idx;i++) {
for(int j=1;j<=idy;j++) {
s[i][j]+=s[i-1][j]+s[i][j-1]-s[i-1][j-1];
}
}
int ans=0;
for(int i=1;i<=idx;i++) {
for(int j=1;j<=idy;j++) {
if(s[i][j]>0) ans+=(px[i]-px[i-1])*(py[j]-py[j-1]);
}
}
cout<<ans;
}
[USACO12FEB] Overplanting S - 二维差分,离散化
标签:mes code bit end signed str const sign 矩形面积并
原文地址:https://www.cnblogs.com/mollnn/p/12490665.html