大意: 求矩形面积并.
枚举$x$坐标, 线段树维护$[y_1,y_2]$内的边是否被覆盖, 线段树维护边时需要将每条边挂在左端点上.
#include <iostream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include <string> #include <string.h> #include <bitset> #define REP(i,a,n) for(int i=a;i<=n;++i) #define PER(i,a,n) for(int i=n;i>=a;--i) #define hr putchar(10) #define pb push_back #define lc (o<<1) #define rc (lc|1) #define mid ((l+r)>>1) #define ls lc,l,mid #define rs rc,mid+1,r #define x first #define y second #define io std::ios::sync_with_stdio(false) #define endl '\n' #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;}) using namespace std; typedef long long ll; typedef pair<int,int> pii; const int P = 1e9+7, INF = 0x3f3f3f3f; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} //head const int N = 1e3+10; int n, cas, cnt, tot; struct _ {double l,r,h;int v;} e[N]; double b[N], sum[N<<2]; int tag[N<<2]; void pu(int o, int l, int r) { if (tag[o]) sum[o] = b[r+1]-b[l]; else sum[o] = sum[lc]+sum[rc]; } void update(int o, int l, int r, int ql, int qr, int v) { if (ql<=l&&r<=qr) return tag[o]+=v,pu(o,l,r); if (mid>=ql) update(ls,ql,qr,v); if (mid<qr) update(rs,ql,qr,v); pu(o,l,r); } void work() { cnt=tot=0; memset(sum,0,sizeof sum); memset(tag,0,sizeof tag); REP(i,1,n) { double x1, y1, x2, y2; scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2); e[++cnt]={y1,y2,x1,1}; e[++cnt]={y1,y2,x2,-1}; b[++tot]=y1,b[++tot]=y2; } sort(b+1,b+1+tot),tot=unique(b+1,b+1+tot)-b-1; sort(e+1,e+1+cnt,[](_ a,_ b){return a.h<b.h;}); double ans = 0; REP(i,1,cnt-1) { int l=lower_bound(b+1,b+1+tot,e[i].l)-b; int r=lower_bound(b+1,b+1+tot,e[i].r)-b-1; update(1,1,tot,l,r,e[i].v); ans += sum[1]*(e[i+1].h-e[i].h); } printf("Test case #%d\nTotal explored area: %.2lf\n\n",++cas,ans); } int main() { for (; scanf("%d", &n), n; ) work(); }