首页 > 代码库 > HDU1377_Counting Squares(扫描线/线段树)
HDU1377_Counting Squares(扫描线/线段树)
解题报告
题意:
矩形面积并。
思路:
扫描线+线段树
#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> 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%d%d%d",&x1,&y1,&x2,&y2)) { int m=0; if(x1==y1&&x2==y2&&x1==x2&&x1==-2)break; memset(lz,0,sizeof(lz)); memset(sum,0,sizeof(sum)); seg[m].lx=min(x1,x2);seg[m].rx=max(x1,x2);seg[m].v=1;seg[m++].h=min(y1,y2); seg[m].lx=min(x1,x2);seg[m].rx=max(x1,x2);seg[m].v=-1;seg[m++].h=max(y1,y2); while(~scanf("%d%d%d%d",&x1,&y1,&x2,&y2)) { if(x1==y1&&x2==y2&&x1==x2&&x1==-2)break; if(x1==y1&&x2==y2&&x1==x2&&x1==-1)break; seg[m].lx=min(x1,x2);seg[m].rx=max(x1,x2);seg[m].v=1;seg[m++].h=min(y1,y2); seg[m].lx=min(x1,x2);seg[m].rx=max(x1,x2);seg[m].v=-1;seg[m++].h=max(y1,y2); } sort(seg,seg+m); int ans=0; for(i=0; i<m-1; i++) { update(1,0,100-1,seg[i].lx,seg[i].rx-1,seg[i].v); ans+=sum[1]*(seg[i+1].h-seg[i].h); } printf("%d\n",ans); } return 0; }
Counting Squares
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1318 Accepted Submission(s): 671
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 10 6 9 7 8 6 8 8 11 -1 -1 -1 -1 0 0 100 100 50 75 12 90 39 42 57 73 -2 -2 -2 -2
Sample Output
8 10000
Source
浙江工业大学第四届大学生程序设计竞赛
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。