程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU1377_Counting Squares(掃描線/線段樹)

HDU1377_Counting Squares(掃描線/線段樹)

編輯:C++入門知識

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.hr||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 浙江工業大學第四屆大學生程序設計競賽

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved