Ted has a new house
2015-08-26 12:40
465 查看
F - F
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice HDU
3265
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
56
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cstdio>
using namespace std;
#define T 55555
typedef long long ll;
struct node
{
ll sum,flag;
}tr[T<<3];
struct rec
{
int x1,x2,y,flag;
rec(int a,int b,int c,int d){
x1=a,x2=b,y=c,flag=d;
}
bool operator<(const rec& a)const{
return y<a.y;
}
};
void pushup(int n,int ln,int rn)
{
if(tr
.flag)tr
.sum=rn-ln+1;
else if(ln==rn) tr
.sum=0;
else tr
.sum=tr[n<<1].sum+tr[n<<1|1].sum;
}
void update(int x1,int x2,int flag,int ln,int rn,int n)
{
if(x1<=ln&&x2>=rn){
tr
.flag+=flag;
pushup(n,ln,rn);
return;
}
int m=(ln+rn)>>1;
if(x1<=m)update(x1,x2,flag,ln,m,n<<1);
if(x2>m)update(x1,x2,flag,m+1,rn,n<<1|1);
pushup(n,ln,rn);
}
int main()
{
/*freopen("input.txt","r",stdin);*/
int n,x1,x2,x3,x4,y1,y2,y3,y4;
while(~scanf("%d",&n),n)
{
vector<rec> v;
long long sum=0;
while(n--)
{
scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
if(x1<x3)//拆成四个矩形
{
v.push_back(rec(x1,x3,y1,1));//底边给这段涂上颜色,
v.push_back(rec(x1,x3,y2,-1));//顶边把颜色去掉
}
if(x4<x2)
{
v.push_back(rec(x4,x2,y1,1));
v.push_back(rec(x4,x2,y2,-1));
}
if(y1<y3)
{
v.push_back(rec(x3,x4,y1,1));
v.push_back(rec(x3,x4,y3,-1));
}
if(y4<y2)
{
v.push_back(rec(x3,x4,y4,1));
v.push_back(rec(x3,x4,y2,-1));
}
}
sort(v.begin(),v.end());
memset(tr,0,sizeof(tr));
int end = v.size();
for(int i=0;i<end-1;++i){
if(v[i].x2>v[i].x1)
update(v[i].x1,v[i].x2-1,v[i].flag,0,T,1);
sum+=tr[1].sum*(v[i+1].y-v[i].y);
}
printf("%lld\n",sum);
}
return 0;
}
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d
& %I64u
Submit Status Practice HDU
3265
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
56
AC代码:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cstdio>
using namespace std;
#define T 55555
typedef long long ll;
struct node
{
ll sum,flag;
}tr[T<<3];
struct rec
{
int x1,x2,y,flag;
rec(int a,int b,int c,int d){
x1=a,x2=b,y=c,flag=d;
}
bool operator<(const rec& a)const{
return y<a.y;
}
};
void pushup(int n,int ln,int rn)
{
if(tr
.flag)tr
.sum=rn-ln+1;
else if(ln==rn) tr
.sum=0;
else tr
.sum=tr[n<<1].sum+tr[n<<1|1].sum;
}
void update(int x1,int x2,int flag,int ln,int rn,int n)
{
if(x1<=ln&&x2>=rn){
tr
.flag+=flag;
pushup(n,ln,rn);
return;
}
int m=(ln+rn)>>1;
if(x1<=m)update(x1,x2,flag,ln,m,n<<1);
if(x2>m)update(x1,x2,flag,m+1,rn,n<<1|1);
pushup(n,ln,rn);
}
int main()
{
/*freopen("input.txt","r",stdin);*/
int n,x1,x2,x3,x4,y1,y2,y3,y4;
while(~scanf("%d",&n),n)
{
vector<rec> v;
long long sum=0;
while(n--)
{
scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
if(x1<x3)//拆成四个矩形
{
v.push_back(rec(x1,x3,y1,1));//底边给这段涂上颜色,
v.push_back(rec(x1,x3,y2,-1));//顶边把颜色去掉
}
if(x4<x2)
{
v.push_back(rec(x4,x2,y1,1));
v.push_back(rec(x4,x2,y2,-1));
}
if(y1<y3)
{
v.push_back(rec(x3,x4,y1,1));
v.push_back(rec(x3,x4,y3,-1));
}
if(y4<y2)
{
v.push_back(rec(x3,x4,y4,1));
v.push_back(rec(x3,x4,y2,-1));
}
}
sort(v.begin(),v.end());
memset(tr,0,sizeof(tr));
int end = v.size();
for(int i=0;i<end-1;++i){
if(v[i].x2>v[i].x1)
update(v[i].x1,v[i].x2-1,v[i].flag,0,T,1);
sum+=tr[1].sum*(v[i+1].y-v[i].y);
}
printf("%lld\n",sum);
}
return 0;
}
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- C++ Custom Control控件向父窗体发送对应的消息
- C++中拷贝构造函数的应用详解