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.
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
- HDOJ 题目1264 Counting Squares(线段树+扫描线)
- HDH 1264 Counting Squares (线段树+扫描线|暴力)
- HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)
- HDU-1264 Counting Squares 矩形面积并 线段树+扫描线
- HDOJ 题目3265 Posters(线段树+扫描线)
- HDOJ 题目1828 Picture (线段树+扫描线)
- hdu 1264 Counting Squares(扫描线)
- POJ题目 1151|| HDOJ 题目1542Atlantis(线段树+离散化+扫描线)
- HDU1377_Counting Squares(扫描线/线段树)
- HDOJ 题目1255 覆盖的面积(线段树+扫描线)
- Counting Squares(1264)
- HDU 1264 Counting Squares(线段树+面积并)
- hdu1264 Counting Squares(线段树+离散化)
- 【HDOJ 5820】Lights(扫描线+线段树)
- 【线段树+扫描线】 HDOJ 1542 Atlantis
- HDOJ 题目4107 Gangster(线段树)
- HDOJ题目2952Counting Sheep (DFS)
- HDU 1264 Counting Squares
- MFC 扩展CStatic类绘图-双缓冲区避免闪屏
- nginx rewrite url
- 杭电1013——Digital Roots(简单题)
- Javascript 严格模式详解
- 旋转字符串
- HDOJ 题目1264 Counting Squares(线段树+扫描线)
- Android-->FlowRadioGroup(流式布局RadioGroup, 自定义View的简单使用)
- mysqldump中断的常见错误和解决措施
- Leetcode Next Permutation
- BCompare文件对比软件使用总结
- objc_msgSend arm64 崩溃问题
- 协议
- ALSA声卡驱动中的DAPM详解之一:kcontrol
- PHP中的错误和异常处理