HDU 1264 Counting Squares(Hash)或者(线段树+线扫描)

http://acm.hdu.edu.cn/showproblem.php?pid=1264

题意:给你矩形的左下角和右上角两个坐标,让你求这些矩形覆盖的面积的大小!~

分析:一看就是线段树+线扫描的问题,其实如果你仔细看一下就会发现还有简单的方法解决它,因为题目所给的坐标值在0~100之间的整数,

   这样我们就可以用Hash[][]来表示1*1矩形的个数,Hash[i][j]表示以坐标值(i,j)为左下角1*1的矩形是否被覆盖,这样我们就可以用Hash二维数组表示平面上1*1矩形个数.

注意:我们要求输入要按照x1<x2&&y1<y2的形式输入,如果不是swap一下就可以。还有就是在标记Hash[][]数组的时候注意x2,y2这两个端点不能选取的!自己想一下就知道的.

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<string.h>
 4 using namespace std;
 5
 6 int Hash[111][111];
 7
 8 int main()
 9 {
10     int x1,y1,x2,y2;
11     int flag=1;
12     while(flag)
13     {
14         memset(Hash,0,sizeof(Hash));
15
16         while(~scanf("%d%d%d%d",&x1,&y1,&x2,&y2))
17         {
18             if(x1==-1&&y1==-1&&x2==-1&&y2==-1)
19                 break;
20             if(x1==-2&&y1==-2&&x2==-2&&y2==-2)
21             {
22                 flag=0;
23                 break;
24             }
25             if(x1>x2)
26                 swap(x1,x2);
27             if(y1>y2)
28                 swap(y1,y2);
29             for(int i=x1; i<x2; i++)///注意这里是没有取等号的
30                 for(int j=y1; j<y2; j++)///同上
31                     Hash[i][j]=1;
32         }
33         int ans=0;
34         for(int i=0; i<=100; i++)
35             for(int j=0; j<=100; j++)
36                 if(Hash[i][j])
37                     ans++;
38         printf("%d\n",ans);
39     }
40 }

原文地址:https://www.cnblogs.com/Y-Meng/p/8675665.html

时间: 2024-11-07 23:15:39

HDU 1264 Counting Squares(Hash)或者(线段树+线扫描)的相关文章

HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)

Problem A : Counting Squares From:HDU, 1264 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 t

HDU 1264 Counting Squares(线段树求面积的并)

Counting Squares Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1885    Accepted Submission(s): 946 Problem Description Your input is a series of rectangles, one per line. Each rectangle is sp

HDU 1264 Counting Squares(模拟)

题目链接 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 lin

HDU 2852 KiKi&#39;s K-Number(线段树+树状数组)

KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2603    Accepted Submission(s): 1202 Problem Description For the k-th number, we all should be very familiar with it. Of course,t

hdu 1394 Minimum Inversion Number(线段树)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10853    Accepted Submission(s): 6676 Problem Description The inversion number of a given number sequence a1, a2, ..., a

HDU 4902 (牛叉的线段树)

Nice boat Problem Description There is an old country and the king fell in love with a devil. The devil always asks the king to do some crazy things. Although the king used to be wise and beloved by his people. Now he is just like a boy in love and c

HDU Wow! 4893 Such Sequence!(线段树)

HDU 4893 Wow! Such Sequence! 题目链接 题意:给定一个序列,3种操作,单点添加值,查询区间和,把区间和变成最接近的婓波那契数 思路:线段树,就是第三个操作麻烦,就在结点添加一个值,标记它区间是不是都是婓波那契数了,然后修改区间的时候,如果区间是了就不用修改,如果不是就继续往后一层推即可 代码: #include <cstdio> #include <cstring> #include <cstdlib> #define lson(x) ((x

HDU 4893 Wow! Such Sequence! 水线段树

思路: 线段树走起.. 写完这题就退役T^T 单点更新的时候直接找到这个点的最近fib,然后维护当前和 和 fib的和 #include<stdio.h> #include<string.h> #include<iostream> #include<math.h> #include<algorithm> #include<queue> #include<map> #include<set> #include&l

HDU 1754 I Hate It (线段树 单点更新)

题目链接 中文题意,与上题类似. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <cstdlib> 6 #include <algorithm> 7 const int maxn = 200000+10; 8 using namespace std; 9 int a[maxn], n, m; 10