HDOJ 题目1264 Counting Squares(线段树+扫描线)

来源:互联网 发布:拜占庭算法的基础 编辑:程序博客网 时间:2024/05/14 01:59

Counting Squares

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1549    Accepted Submission(s): 781


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 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
 

Source
浙江工业大学第四届大学生程序设计竞赛
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1280 1543 3303 2600 1156 
ac带吗
#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#define N 100010#define ll long longusing namespace std;struct s{    int x1,x2,y;    int flag;}a[N<<2];int __hash[N<<3],k,col[N<<3];ll sum[N<<3];int cmp(s a ,s b){    return a.y<b.y;}void add(int x1,int y1,int x2,int y2){    a[k].x1=x1;    a[k].x2=x2;    a[k].y=y1;    a[k].flag=1;    __hash[k++]=x1;    a[k].x1=x1;    a[k].x2=x2;    a[k].y=y2;    a[k].flag=-1;    __hash[k++]=x2;}int bseach(int key,int n){    int l=1,r=n;    while(l<=r)    {        int mid=(l+r)>>1;        if(__hash[mid]==key)            return mid;        if(__hash[mid]<key)            l=mid+1;        else            r=mid-1;    }    return l;}void pushup(int tr,int l,int r){    if(col[tr])    {        sum[tr]=__hash[r+1]-__hash[l];    }    else        if(l==r)            sum[tr]=0;        else            sum[tr]=sum[tr<<1]+sum[tr<<1|1];}void update(int L,int R,int l,int r,int tr,int flag){    if(L<=l&&r<=R)    {        col[tr]+=flag;        pushup(tr,l,r);        return ;    }    int mid=(l+r)>>1;    if(L<=mid)        update(L,R,l,mid,tr<<1,flag);    if(R>mid)        update(L,R,mid+1,r,tr<<1|1,flag);    pushup(tr,l,r);}int main(){    int x1,x2;    int y1,y2;    k=1;    int w=0;    while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF)    {        int i,j;        if(x1>x2)swap(x1,x2);        if(y1>y2)swap(y1,y2);        if(x1==-2&&x2==-2&&y1==-2&&y2==-2)            break;        if(x1==-1&&x2==-1&&y1==-1&&y2==-1)        {            k=1;            continue;        }        memset(col,0,sizeof(col));        memset(sum,0,sizeof(sum));        add(x1,y1,x2,y2);        while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2)!=EOF)        {            if(x1>x2)swap(x1,x2);            if(y1>y2)swap(y1,y2);            if(x1==-1&&x2==-1&&y1==-1&&y2==-1)                break;            if(x1==-2&&x2==-2&&y1==-2&&y2==-2)            {                w=1;                break;            }            add(x1,y1,x2,y2);        }        sort(a+1,a+k,cmp);        sort(__hash+1,__hash+k);        ll ans=0;        for(i=1;i<k-1;i++)        {            int x,y;            x=bseach(a[i].x1,k-1);            y=bseach(a[i].x2,k-1)-1;            if(x<=y)                update(x,y,1,k-1,1,a[i].flag);            ans+=sum[1]*(ll)(a[i+1].y-a[i].y);        }        printf("%lld\n",ans);        k=1;        if(w)            break;    }}


 
0 0
原创粉丝点击