矩形面积并 HDU - 1542
#include #include #include #include #include #include #include #include #include #include #define lowbit(x) x&(-x) #define debug cout<<"****************"<>T;for(int kase=0;kase pii; typedef long long ll; const int maxn = 10000+7; double y[maxn]; struct Line { double l,r,x; int flag; bool operator < (const Line& rhs) const { return x < rhs.x; } }lines[maxn]; struct Node { double l,r; int cover; double len; }tree[maxn]; void build(int rt,int l,int r){ tree[rt].l = y[l]; tree[rt].r = y[r]; tree[rt].cover = 0; tree[rt].len = 0; if(l==r-1) return; int mid = (l+r) >> 1; build(rt<<1, l, mid); build(rt<<1|1,mid,r); } void pushup(int rt,int l,int r){ if(tree[rt].cover > 0){ tree[rt].len = tree[rt].r - tree[rt].l; }else if(l==r-1){ tree[rt].len = 0; }else{ tree[rt].len = tree[rt<<1].len + tree[rt<<1|1].len; } } void update(int rt,int l,int r,Line e){ if(e.l>=y[r]||e.r<=y[l]) return; if(l==r-1) tree[rt].cover += e.flag; else{ int mid = (l+r) >> 1; update(rt<<1, l, mid, e); update(rt<<1|1, mid, r, e); } pushup(rt,l,r); } int main() { //FRER(); //FREW(); int n,k=0; while(~scanf("%d",&n)&&n){ int cnt = 0; for(int i=1;i<=n;i++){ double x1,y1,x2,y2; scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); lines[cnt].x = x1; lines[cnt].flag = 1; lines[cnt].l = y1; lines[cnt].r = y2; y[cnt++] = y1; lines[cnt].x = x2; lines[cnt].flag = -1; lines[cnt].l = y1; lines[cnt].r = y2; y[cnt++] = y2; } sort(lines,lines+cnt); sort(y, y+cnt); build(1,0,cnt-1); double ans = 0; for(int i=0;i
矩形面积交 HDU - 1255
#include #include #include #include #include #include #include #include #include #include #define lowbit(x) x&(-x) #define debug cout<<"****************"<>T;for(int kase=0;kase pii; typedef long long ll; const int maxn = 10000+7; double y[maxn]; struct Line { double l,r,x; int flag; bool operator < (const Line& rhs) const { return x < rhs.x; } }lines[maxn]; struct Node { double l,r; int cover; double len; }tree[maxn]; void build(int rt,int l,int r){ tree[rt].l = y[l]; tree[rt].r = y[r]; tree[rt].cover = -1; tree[rt].len = 0; if(l==r-1) return; int mid = (l+r) >> 1; build(rt<<1, l, mid); build(rt<<1|1,mid,r); } void pushup(int rt,int l,int r){ if(tree[rt].cover > 0){ tree[rt].len = tree[rt].r - tree[rt].l; }else if(l==r-1){ tree[rt].len = 0; }else{ tree[rt].len = tree[rt<<1].len + tree[rt<<1|1].len; } } void update(int rt,int l,int r,Line e){ if(e.l>=y[r]||e.r<=y[l]) return; if(l==r-1) tree[rt].cover += e.flag; else{ int mid = (l+r) >> 1; update(rt<<1, l, mid, e); update(rt<<1|1, mid, r, e); } pushup(rt,l,r); } int main() { //FRER(); //FREW(); int n; go{ scanf("%d",&n); int cnt = 0; for(int i=1;i<=n;i++){ double x1,y1,x2,y2; scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2); lines[cnt].x = x1; lines[cnt].flag = 1; lines[cnt].l = y1; lines[cnt].r = y2; y[cnt++] = y1; lines[cnt].x = x2; lines[cnt].flag = -1; lines[cnt].l = y1; lines[cnt].r = y2; y[cnt++] = y2; } sort(lines,lines+cnt); sort(y, y+cnt); build(1,0,cnt-1); double ans = 0; for(int i=0;i
两者的不同在于cover值的初值,一个为0,一个为-1
矩形周长并 POJ - 1177
#include #include #include #include #include #include #include #include #include #include #define lowbit(x) x&(-x) #define debug cout<<"****************"<>T;for(int kase=0;kase pii; typedef long long ll; const int maxn = 100000+7; double xy[2][maxn]; struct Line { double l,r,idx; int flag; bool operator < (const Line& rhs) const { return idx < rhs.idx; } }lines[2][maxn]; struct Node { double l,r; int cover; double len; }tree[2][maxn]; void build(int rt,int l,int r,int idx){ tree[idx][rt].len = 0; tree[idx][rt].cover = 0; tree[idx][rt].l = xy[idx][l]; tree[idx][rt].r = xy[idx][r]; if(l==r-1) return; int mid = (l+r) >> 1; build(rt<<1,l,mid,idx); build(rt<<1|1,mid,r,idx); } void pushup(int rt,int l,int r,int idx){ if(tree[idx][rt].cover > 0) tree[idx][rt].len = tree[idx][rt].r - tree[idx][rt].l; else if(l==r-1) tree[idx][rt].len = 0; else { tree[idx][rt].len = tree[idx][rt<<1].len + tree[idx][rt<<1|1].len; } } void update(int rt,int l,int r,Line e,int idx){ if(e.l>=xy[idx][r]||e.r<=xy[idx][l]) return; if(l==r-1) tree[idx][rt].cover += e.flag; else{ int mid = (l+r) >> 1; update(rt<<1, l, mid, e, idx); update(rt<<1|1, mid, r, e, idx); } pushup(rt,l,r,idx); } int main() { //FRER(); //FREW(); int n; scanf("%d",&n); int cnt = 0; for(int i=0;i