hdu 1264 Counting Squares(扫描线)
来源:互联网 发布:河南软件开发公司 编辑:程序博客网 时间:2024/05/22 06:11
Counting Squares
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1175 Accepted Submission(s): 593
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#include <iostream>#include <cstdio>#include <vector>#include <cstring>#include <algorithm>using namespace std;const int maxn = 110;struct tree{ int l , r , mark , lz; tree(int a = 0 , int b = 0, int c = 0 , int d = 0){ l = a , r = b , mark = c , lz = d; }}a[4*maxn];struct segment{ int y1 , y2 , x , mark; segment(int a = 0, int b = 0 , int c = 0 , int m = 0){ y1 = a , y2 = b , x = c , mark = m; }};vector<segment> seg;int x1 , x2 , yy1 , y2;bool cmp(segment s1 , segment s2){ if(s1.x < s2.x) return true; return false;}void pushdown(int k){ if(a[k].l+1 < a[k].r && a[k].lz != 0){ a[2*k].mark += a[k].lz; a[2*k].lz += a[k].lz; a[1+2*k].mark += a[k].lz; a[1+2*k].lz += a[k].lz; a[k].lz = 0; }}void build(int l , int r , int k){ a[k].l = l; a[k].r = r; a[k].mark = 0; a[k].lz = 0; if(l+1 < r){ int mid = (l+r)/2; build(l , mid , 2*k); build(mid , r , 1+2*k); }}void add(int l , int r , int k , int c){ if(l <= a[k].l && a[k].r <= r){ a[k].mark += c; a[k].lz += c; }else{ int mid = (a[k].l+a[k].r)/2; if(mid >= r){ add(l , r , 2*k , c); }else if(l >= mid){ add(l , r , 1+2*k , c); }else{ add(l , mid , 2*k , c); add(mid , r , 1+2*k , c); } }}int querry(int l , int r , int k){ if(a[k].mark > 0){ return r-l; } if(a[k].l+1 >= a[k].r){ if(a[k].mark <= 0) return 0; return 1; } pushdown(k); int mid = (a[k].l+a[k].r)/2; if(mid>=r) return querry(l , r , 2*k); else if(mid<=l) return querry(l , r , 1+2*k); else return querry(l , mid , 2*k)+querry(mid , r , 1+2*k);}void initial(){ build(0 , 100 , 1); seg.clear();}void readcase(){ if(yy1 > y2) swap(yy1 , y2); if(x1 > x2) swap(x1 , x2); seg.push_back(segment(yy1 , y2 , x1 , 1)); seg.push_back(segment(yy1 , y2 , x2 , -1)); while(cin >> x1 >> yy1 >> x2 >> y2){ if(x1 == -1 || x1 == -2) break; if(yy1 > y2) swap(yy1 , y2); if(x1 > x2) swap(x1 , x2); seg.push_back(segment(yy1 , y2 , x1 , 1)); seg.push_back(segment(yy1 , y2 , x2 , -1)); }}void computing(){ sort(seg.begin() , seg.end() , cmp); int ans = 0; for(int i = 0; i < seg.size(); i++){ int h = querry(0 , 100 , 1); add(seg[i].y1 , seg[i].y2 , 1 , seg[i].mark); if(h > 0) ans += (seg[i].x-seg[i-1].x)*h; } cout << ans << endl;}int main(){ while(cin >> x1 >> yy1 >> x2 >> y2){ initial(); readcase(); computing(); if(x1 == -2) break; } return 0;}
0 0
- hdu 1264 Counting Squares(扫描线)
- HDU 1264 Counting Squares
- HDU 1264 Counting Squares
- HDU 1264 Counting Squares
- hdu 1264 Counting Squares
- HDU 1264 Counting Squares
- HDU 1264 Counting Squares
- hdu 1264 Counting Squares
- HDU-1264 Counting Squares 矩形面积并 线段树+扫描线
- HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)
- HDOJ 题目1264 Counting Squares(线段树+扫描线)
- HDH 1264 Counting Squares (线段树+扫描线|暴力)
- hdu 1264 counting the squares
- hdu 1256 Counting Squares
- Counting Squares(1264)
- 1264:Counting Squares
- HDU 1264 Counting Squares(哈希表||离散化)
- HDU-1264 Counting Squares(矩形面积并模板)
- 给技术人上的管理课:平衡和集中
- 任务栏信息栏中图标闪动
- “bash: fork: Resource temporarily unavailable”的解决方案
- TestStackOfInteger
- 安装mysql5.6.17遇到安装意外终止 1067
- hdu 1264 Counting Squares(扫描线)
- 老三篇原文
- UVa 446 - Kibbles "n" Bits "n" Bits "n" Bits
- Android-Popupwindow和Dialog做弹出窗口
- mybatis学习笔记之trim标签---小笔记
- AVR之BOOTLOADER技术详解
- KMP字符串匹配算法学习总结
- Eclipse配置SVN
- HTML5的Canvas画图模拟太阳系运转