poj 1151 (未完成) 扫描线 线段树 离散化
作者:互联网
#include<iostream> #include<vector> #include<cmath> #include<algorithm> using namespace std; #define y1 y11 #define ls rt<<1,l,m #define rs rt<<1|1,m+1,r const int maxn=1e3+10; double x1[maxn]; double y1[maxn]; double x2[maxn]; double y2[maxn]; vector<double> vx; vector<double> vy; double x[maxn]; double y[maxn]; struct node { int yl,yr,k; }pp[maxn]; vector<node> vxxx[maxn]; double tree[4*maxn]; double ans[4*maxn]; int lazy[4*maxn]; int tot=0; void push_up(int rt) { tree[rt]=tree[rt<<1]+tree[rt<<1|1]; } //void push_down(int rt,int len) { lazy[rt<<1]+=lazy[rt]; lazy[rt<<1|1]+=lazy[rt]; lazy[rt]=0; } void build(int rt,int l,int r) { if(l==r) { tot++; tree[l]=vy[tot]-vy[tot-1]; return ; } int m=(l+r)>>1; build(ls); build(rs);push_up(rt); } void update(int p,int delta,int rt,int l,int r) { if(l==r) { tree[rt]+=delta; return ; } int m=(l+r)>>1; if(p<=m)update(p,delta,ls); else update(p,delta,rs); push_up(rt); } void Update(int L,int R,int delta,int rt,int l,int r) { if(L<=l&&r<=R) { lazy[rt]+=delta; return ; } if(lazy[rt]) push_down(rt,r-l+1); int m=(l+r)>>1; if(L<=m)Update(L,R,delta,ls); if(R>m)Update(L,R,delta,rs); push_up(rt); } int main() { int cnt=0; int n; while(1) { scanf("%d",&n); if(n==0) break; for(int i=1;i<=n;i++) scanf("%lf %lf %lf %lf",&x1[i],&y1[i],&x2[i],&y2[i]); for(int i=1;i<=n;i++) { vx.push_back(x1[i]); vx.push_back(x2[i]); vy.push_back(y1[i]); vy.push_back(y2[i]); } sort(vx.begin(),vx.end()); vx.erase(unique(vx.begin(),vx.end()),vx.end()); for(int i=0;i<vx.size();i++) x[i]=vx[i]; sort(vy.begin(),vy.end()); vy.erase(unique(vy.begin(),vy.end()),vy.end()); for(int i=0;i<vy.size();i++) y[i]=vy[i]; for(int i=1;i<=n;i++) { int k=lower_bound(vx.begin(),vx.end(),x1[i])-vx.begin(); node p; p.yl=y1[i]; p.yr=y2[i]; p.k=1; vxxx[k].push_back(p); k=lower_bound(vx.begin(),vx.end(),x2[i])-vx.begin(); p.k=-1; vxxx[k].push_back(p); } int m=vy.size()-1; build(1,1,m); double ans=0; for(int i=0;i<vx.size();i++) { if(i==0) { for(int j=0;j<=vxx[i].size();j++) { node p=vxxx[i][j]; int l=lower_bound(vy.begin(),vy.end(),p.yl)-vy.begin(); int r=lower_bound(vy.begin(),vy.end(),p.yr)-vy.begin(); update(l,r,p.k,1,1,m); } } } } }
标签:rt,int,double,1151,tree,poj,扫描线,maxn,include 来源: https://www.cnblogs.com/Andromeda-Galaxy/p/10574798.html