设为首页 加入收藏

TOP

HDU1377_Counting Squares(扫描线/线段树)
2015-07-20 17:53:31 来源: 作者: 【 】 浏览:1
Tags:HDU1377_Counting Squares 扫描 线段

解题报告

题意:

矩形面积并。

思路:

扫描线+线段树

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       using namespace std; struct Seg { int lx,rx,h,v; friend bool operator < (Seg a,Seg b) { return a.h
      
       r||qr
       
        >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
        
         

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.

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 浙江工业大学第四届大学生程序设计竞赛

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇POJ 2536 Gopher II(二分图的最.. 下一篇T - Can you answer these querie..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: