解题报告
题意:
给定的矩形里面有镂空的矩阵,求矩阵面积并。
思路:
直接把一个图形拆成4个矩形,进行面积并。
扫描线+线段树
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#define LL __int64
using namespace std;
struct Seg
{
int lx,rx,h,v;
friend bool operator < (Seg a,Seg b)
{
return a.h<b.h;
}
} seg[500000];
int lz[201000],sum[201000];
void push_up(int rt,int l,int r)
{
if(lz[rt])
sum[rt]=r+1-l;
else if(l==r)sum[rt]=0;
else sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void update(int rt,int l,int r,int ql,int qr,int v)
{
if(ql>r||qr<l)return ;
if(ql<=l&&r<=qr)
{
lz[rt]+=v;
push_up(rt,l,r);
return ;
}
int mid=(l+r)>>1;
update(rt<<1,l,mid,ql,qr,v);
update(rt<<1|1,mid+1,r,ql,qr,v);
push_up(rt,l,r);
}
int main()
{
int n,i,j,x1,y1,x2,y2,x3,y3,x4,y4;
while(~scanf("%d",&n))
{
if(!n)break;
int m=0;
memset(lz,0,sizeof(lz));
memset(sum,0,sizeof(sum));
for(i=0; i<n; i++)
{
scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
seg[m].lx=x1;
seg[m].rx=x3;
seg[m].v=1;
seg[m++].h=y1;
seg[m].lx=x1;
seg[m].rx=x3;
seg[m].v=-1;
seg[m++].h=y2;
seg[m].lx=x3;
seg[m].rx=x4;
seg[m].v=1;
seg[m++].h=y1;
seg[m].lx=x3;
seg[m].rx=x4;
seg[m].v=-1;
seg[m++].h=y3;
seg[m].lx=x3;
seg[m].rx=x4;
seg[m].v=1;
seg[m++].h=y4;
seg[m].lx=x3;
seg[m].rx=x4;
seg[m].v=-1;
seg[m++].h=y2;
seg[m].lx=x4;
seg[m].rx=x2;
seg[m].v=1;
seg[m++].h=y1;
seg[m].lx=x4;
seg[m].rx=x2;
seg[m].v=-1;
seg[m++].h=y2;
}
sort(seg,seg+m);
LL ans=0;
for(i=0; i<m-1; i++)
{
update(1,0,50000-1,seg[i].lx,seg[i].rx-1,seg[i].v);
ans+=(LL)sum[1]*(seg[i+1].h-seg[i].h);
}
printf("%I64d\n",ans);
}
return 0;
}
Posters
Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4585 Accepted Submission(s): 1029
Problem Description
Ted has a new house with a huge window. In this big summer, Ted decides to decorate the window with some posters to prevent the glare outside. All things that Ted can find are rectangle posters.
However, Ted is such a picky guy that in every poster he finds something ugly. So before he pastes a poster on the window, he cuts a rectangular hole on that poster to remove the ugly part. Ted is also a careless guy so that some of the pasted posters may overlap
when he pastes them on the window.
Ted wants to know the total area of the window covered by posters. Now it is your job to figure it out.
To make your job easier, we assume that the window is a rectangle located in a rectangular coordinate system. The window’s bottom-left corner is at position (0, 0) and top-right corner is at position (50000, 50000). The edges of the window, the edges of the
posters and the edges of the holes on the posters are all parallel with the coordinate axes.
Input
The input contains several test cases. For each test case, the first line contains a single integer N (0<N<=50000), representing the total number of posters. Each of the following N lines contains 8 integers x1, y1, x2, y2, x3, y3, x4, y4, showing details about
one poster. (x1, y1) is the coordinates of the poster’s bottom-left corner, and (x2, y2) is the coordinates of the poster’s top-right corner. (x3, y3) is the coordinates of the hole’s bottom-left corner, while (x4, y4) is the coordinates of the hole’s top-right
corner. It is guaranteed that 0<=xi, yi<=50000(i=1…4) and x1<=x3<x4<=x2, y1<=y3<y4<=y2.
The input ends with a line of single zero.
Output
For each test case, output a single line with the total area of window covered by posters.
Sample Input
2
0 0 10 10 1 1 9 9
2 2 8 8 3 3 7 7
0
Sample Output
Source
HDU3265_Posters(扫描线/线段树),布布扣,bubuko.com
HDU3265_Posters(扫描线/线段树)
原文:http://blog.csdn.net/juncoder/article/details/38616097