POJ 2029 Get Many Persimmon Trees 【 二维树状数组 】

题意:给出一个h*w的矩形,再给出n个坐标,在这n个坐标种树,再给出一个s*t大小的矩形,问在这个s*t的矩形里面最多能够得到多少棵树

二维的树状数组,求最多能够得到的树的时候,因为h,w都不超过500,直接暴力

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include <cmath>
 5 #include<stack>
 6 #include<vector>
 7 #include<map>
 8 #include<set>
 9 #include<queue>
10 #include<algorithm>
11 using namespace std;
12
13 typedef long long LL;
14 const int INF = (1<<30)-1;
15 const int mod=1000000007;
16 const int maxn=1005;
17
18 int c[maxn][maxn];
19
20 int lowbit(int x){ return x & (-x);}
21
22 int sum(int x,int y){
23     int ret=0;
24     int y1;
25     while(x > 0){
26         y1=y;
27         while(y1 > 0){
28             ret += c[x][y1];y1-=lowbit(y1);
29         }
30         x-=lowbit(x);
31     }
32     return ret;
33 }
34
35 void add(int x,int y,int d){
36     int y1;
37     while(x < maxn){
38         y1=y;
39         while(y1 < maxn){
40             c[x][y1] += d;y1+=lowbit(y1);
41         }
42         x+=lowbit(x);
43     }
44 }
45
46 int main(){
47     int n;
48     while(scanf("%d",&n)!=EOF && n){
49         memset(c,0,sizeof(c));
50         int w,h;
51         scanf("%d %d",&w,&h);
52         while(n--){
53             int u,v;
54             scanf("%d %d",&u,&v);
55             add(v,u,1);
56         }
57         int x,y;
58         scanf("%d %d",&x,&y);swap(x,y);
59
60         int ans=-1;
61         int tmp=0;
62         for(int i=1;i <=h;i++){
63             for(int j=1;j<=w;j++){
64                 tmp = sum(i+x-1,j+y-1)-sum(i-1,j+y-1)-sum(i+x-1,j-1)+sum(i-1,j-1);
65                 ans = max(ans,tmp);
66             }
67         }
68         printf("%d\n",ans);
69     }
70     return 0;
71 }

时间: 2024-10-05 04:58:38

POJ 2029 Get Many Persimmon Trees 【 二维树状数组 】的相关文章

POJ 2029 Get Many Persimmon Trees (二维树状数组 or DP)

题意:一个H * W的大矩形,里面的某些格子种有树.现在要你找出一个h * w的小矩形,使得里面树的数量最多,问最多有多少棵树 是二维树状数组基础用法,边输入边更新有树的点,建完树后就可以查询每个(1,1)到(x,y)为对顶点的矩形中共有多少棵柿子树. 算法复杂度 O(H*W*lgH*lgW) 但是由于这题的柿子树一旦确定位置后就没有更新位置,所以不需要用树状数组也可,直接用dp统计每个(1,1)到(x,y)为对顶点的矩形中共有多少棵柿子树. 统计的状态转移方程是: for(int i=1;i<

POJ2029:Get Many Persimmon Trees(二维树状数组)

Description Seiji Hayashi had been a professor of the Nisshinkan Samurai School in the domain of Aizu for a long time in the 18th century. In order to reward him for his meritorious career in education, Katanobu Matsudaira, the lord of the domain of

poj 2155 二进制0 1反转---二维树状数组

http://poj.org/problem?id=2155 上午自己搞了很久胡思乱想了很久,然后没思路-----看了论文<浅谈信息学竞赛中的"0"和"1"--二进制思想在信息学竞赛中的应用>,豁然开朗啊,,马上A掉---PE了一次o(╯□╰)o 通过论文学到的两点: 1.多维不会的时候,从一维尝试类比: 2.想法的证明,情况数不多的时候,分类讨论证明很好 #include <cstdio> #include <cstring>

[poj2155]Matrix(二维树状数组)

Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 25004   Accepted: 9261 Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1

POJ 2029 Get Many Persimmon Trees (二维树状数组)

Get Many Persimmon Trees Time Limit:1000MS    Memory Limit:30000KB    64bit IO Format:%I64d & %I64u SubmitStatusPracticePOJ 2029 Description Seiji Hayashi had been a professor of the Nisshinkan Samurai School in the domain of Aizu for a long time in

Get Many Persimmon Trees_枚举&amp;&amp;二维树状数组

Description Seiji Hayashi had been a professor of the Nisshinkan Samurai School in the domain of Aizu for a long time in the 18th century. In order to reward him for his meritorious career in education, Katanobu Matsudaira, the lord of the domain of

POJ 1195 Mobile phones(二维树状数组)

题目链接:POJ 1195 题意: 给出一个S*S的矩阵(行.列号从1开始),每个元素初始值为0,有两种操作:一种是第X行第Y列元素值加A:另一种是查询给定范围矩阵的所有元素之和(L<=X<=R,B<=Y<=T). 分析: 查询给定范围矩阵的所有元素之和是二维区间和,可以转换为二维前缀和求值.类比一维前缀和求法,二维区间和S(L, B, R, T) = S(1, 1, R, T) - S(1 ,1, L-1, T) - S(1, 1, R, B-1) + S(1, 1, L-1,

POJ 2155 Matrix(二维树状数组,绝对具体)

Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20599   Accepted: 7673 Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1

POJ 2155 Matrix(二维树状数组,绝对详细)

Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20599   Accepted: 7673 Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] = 0 (1

poj 2155 二维树状数组

http://poj.org/problem?id=2155 Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17721   Accepted: 6653 Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. I