【模板】掃描線
阿新 • • 發佈:2018-11-17
#include<cstdio> #include<algorithm> using namespace std; typedef long long ll; #define lc o<<1 #define rc o<<1|1 const int N=1e5+10; int hx[N<<1],num; ll ans,len[N<<2]; int dat[N<<2]; struct Edge{ int l,r,h,f; Edge(){} Edge(int _l,int _r,int _h,int _f):l(_l),r(_r),h(_h),f(_f){} bool operator <(const Edge&rhs)const{ return h<rhs.h;} }edge[N<<1]; void push_up(int o,int l,int r) { if(dat[o])len[o]=hx[r+1]-hx[l]; else if(l==r)len[o]=0; else len[o]=len[lc]+len[rc]; } void update(int o,int l,int r,int nl,int nr,int val) { if(nl<=l&&r<=nr) { dat[o]+=val; push_up(o,l,r); return; } int mid=l+r>>1; if(nl<=mid)update(lc,l,mid,nl,nr,val); if(nr>mid)update(rc,mid+1,r,nl,nr,val); push_up(o,l,r); } int main() { //freopen("in.txt","r",stdin); int x1=0,x2=0,y1=0,y2=0; while(x1!=-2) { num=0,ans=0; while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)&&x1>=0) { if(x1>x2)swap(x1,x2); if(y1>y2)swap(y1,y2);//漏了 hx[num]=x1; edge[num++]=Edge(x1,x2,y1,1); hx[num]=x2; edge[num++]=Edge(x1,x2,y2,-1); } sort(hx,hx+num);sort(edge,edge+num); int m=unique(hx,hx+num)-hx; for(int i=0;i<num;i++) { int l=lower_bound(hx,hx+m,edge[i].l)-hx; int r=lower_bound(hx,hx+m,edge[i].r)-hx-1; update(1,0,m,l,r,edge[i].f); ans+=len[1]*(edge[i+1].h-edge[i].h); } printf("%lld\n",ans); } return 0; }