POJ-2029 Get Many Persimmon Trees---二维树状数组+枚举

题目链接:

https://vjudge.net/problem/POJ-2029

题目大意:

有N棵树在一个n*m的田里,给出每颗树的坐标

用一个s*t的矩形去围,最多能围几棵树
思路:
用二维树状数组求区域和,也可以直接用二维前缀和求解
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<map>
 6 #include<set>
 7 #include<cmath>
 8 #include<algorithm>
 9 #define lowbot(i) (i&(-i))
10 using namespace std;
11 typedef long long ll;
12 const int maxn = 500 + 10;
13 int n, m, T, k, cases;
14 int tree[maxn][maxn];
15 void add(int x, int y, int d)
16 {
17     for(int i = x; i <= n; i += lowbot(i))
18     {
19         for(int j = y; j <= m; j += lowbot(j))
20         {
21             tree[i][j] += d;
22         }
23     }
24 }
25 int sum(int x, int y)
26 {
27     int ans = 0;
28     for(int i = x; i > 0; i -= lowbot(i))
29     {
30         for(int j = y; j > 0; j -= lowbot(j))
31         {
32             ans += tree[i][j];
33         }
34     }
35     return ans;
36 }
37 int Sum(int x1, int y1, int x2, int y2)
38 {
39     return sum(x2, y2) + sum(x1 - 1, y1 - 1) - sum(x1 - 1, y2) - sum(x2, y1 - 1);
40 }
41 int main()
42 {
43     while(cin >> k && k)
44     {
45         int x, y, s, t;
46         memset(tree, 0, sizeof(tree));
47         cin >> n >> m;
48         while(k--)
49         {
50             scanf("%d%d", &x, &y);
51             add(x, y, 1);
52         }
53         cin >> s >> t;
54         int ans = 0;
55         for(int i = 1; i + s - 1 <= n; i++)
56         {
57             for(int j = 1; j + t - 1 <= m; j++)
58             {
59                 int tot = Sum(i, j, i + s - 1, j + t - 1);
60                 ans = max(ans, tot);
61             }
62         }
63         cout<<ans<<endl;
64     }
65     return 0;
66 }

 

posted @ 2018-04-25 20:24  _努力努力再努力x  阅读(183)  评论(0编辑  收藏  举报