hdu 1264 Counting Squares
来源:互联网 发布:淘宝旺铺有必要买吗 编辑:程序博客网 时间:2024/06/05 02:13
Counting Squares
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2198 Accepted Submission(s): 1102
Problem Description
Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) that specify the opposite corners of a rectangle. All coordinates will be integers in the range 0 to 100. For example, the line
5 8 7 10
specifies the rectangle who's corners are(5,8),(7,8),(7,10),(5,10).
If drawn on graph paper, that rectangle would cover four squares. Your job is to count the number of unit(i.e.,1*1) squares that are covered by any one of the rectangles given as input. Any square covered by more than one rectangle should only be counted once.
5 8 7 10
specifies the rectangle who's corners are(5,8),(7,8),(7,10),(5,10).
If drawn on graph paper, that rectangle would cover four squares. Your job is to count the number of unit(i.e.,1*1) squares that are covered by any one of the rectangles given as input. Any square covered by more than one rectangle should only be counted once.
Input
The input format is a series of lines, each containing 4 integers. Four -1's are used to separate problems, and four -2's are used to end the last problem. Otherwise, the numbers are the x-ycoordinates of two points that are opposite corners of a rectangle.
Output
Your output should be the number of squares covered by each set of rectangles. Each number should be printed on a separate line.
Sample Input
5 8 7 106 9 7 86 8 8 11-1 -1 -1 -10 0 100 10050 75 12 9039 42 57 73-2 -2 -2 -2
Sample Output
810000思路:数据较小有两种方法可以做,一种暴力。当然我们可以使用线段树扫描线来做,用线段树的话要离散处理。代码:#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;struct node{ double l,r,h; int z;}a[210];bool cmp(node a,node b){ return a.h<b.h;}double hh[210];int mark[210*4],sum[210*4];void upfather(int n,int L,int R){ if(mark[n])sum[n]=hh[R+1]-hh[L]; else if(L==R)sum[n]=0; else sum[n]=sum[n*2]+sum[n*2+1];}void update(int l,int r,int d,int n,int L,int R){ if(l<=L&&R<=r) { mark[n]+=d; upfather(n,L,R); return ; } int mid=L+R>>1; if(l<=mid)update(l,r,d,n*2,L,mid); if(r>mid)update(l,r,d,n*2+1,mid+1,R); upfather(n,L,R);}int findx(double i,double *b,int n){ int l=0; int r=n-1; while(l<=r) { int mid=l+r>>1; if(b[mid]==i)return mid; else if(i<b[mid])r=mid-1; else l=mid+1; } return -1;}int main(){ double x0=0,x1=0,y0=0,y1=0; while(x0!=-2) { int k=0; while(cin>>x0>>y0>>x1>>y1,x0>=0) { if(x0>x1)swap(x0,x1); if(y0>y1)swap(y0,y1); hh[k]=x0; a[k].l=x0; a[k].r=x1; a[k].h=y0; a[k++].z=1; hh[k]=x1; a[k].l=x0; a[k].r=x1; a[k].h=y1; a[k++].z=-1; } sort(hh,hh+k); int i; int z=1; for(i=1;i<k;++i) { if(hh[i]!=hh[i-1])hh[z++]=hh[i]; } sort(a,a+k,cmp); int ans=0; for(i=0;i<k;++i) { int L=findx(a[i].l,hh,z); int R=findx(a[i].r,hh,z)-1; update(L,R,a[i].z,1,0,z-1); ans+=sum[1]*(a[i+1].h-a[i].h); } cout<<ans<<endl; } return 0;}代码二:#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int m[105][105];int main(){ int x1,y1,x2,y2; int cnt=0; memset(m,0,sizeof(m)); while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF) { if(x1==-1) { printf("%d\n",cnt); cnt=0; memset(m,0,sizeof(m)); continue; } if(x1==-2) { printf("%d\n",cnt); break; } if(x1>x2)swap(x1,x2); if(y1>y2)swap(y1,y2); int i,j; for(i=x1;i<x2;i++) { for(j=y1;j<y2;j++) { if(m[i][j]==0) { cnt++; m[i][j]=1; } } } } return 0;}
阅读全文
0 0
- HDU 1264 Counting Squares
- HDU 1264 Counting Squares
- HDU 1264 Counting Squares
- hdu 1264 Counting Squares
- HDU 1264 Counting Squares
- HDU 1264 Counting Squares
- hdu 1264 Counting Squares
- hdu 1264 counting the squares
- hdu 1264 Counting Squares(扫描线)
- hdu 1256 Counting Squares
- Counting Squares(1264)
- 1264:Counting Squares
- HDU 1264 Counting Squares(哈希表||离散化)
- HDU-1264 Counting Squares(矩形面积并模板)
- HDU 1264 Counting Squares(线段树+面积并)
- Counting Squares
- HDU-1264 Counting Squares 矩形面积并 线段树+扫描线
- HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)
- 一道很经典的多线程面试题
- 一些汇编练习
- Linux防火墙Iptable如何设置只允许某个ip访问80端口,只允许特定ip访问某端口
- 最短路径
- Quartz任务调度(1)概念例析快速入门
- hdu 1264 Counting Squares
- 【Scikit-Learn 中文文档】流形学习
- django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法
- 关于C/C++和Java中的j=j++问题
- 51nod 1179 最大的最大公约数 (无耻的打表计数法)
- JVM系列一:2)JVM内存组成
- 项目
- 私有云落地解决方案之openstack高可用(pike版本)-消息队列
- CentOS7上MySQL返回 Too many connections