题目链接:
http://poj.org/problem?id=2029
题意:
N个星,在W*H的网格中,下面给出N个星的位置,给出S,T,要找出S*T大小的矩形,使得星星最多
题解:
http://blog.csdn.net/zxy_snow/article/details/6260895
二维树状数组和一维差不多,维护的是两个方向x,y。一块面积?
代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 using namespace std; 6 typedef long long ll; 7 #define MS(a) memset(a,0,sizeof(a)) 8 #define MP make_pair 9 #define PB push_back 10 const int INF = 0x3f3f3f3f; 11 const ll INFLL = 0x3f3f3f3f3f3f3f3fLL; 12 inline ll read(){ 13 ll x=0,f=1;char ch=getchar(); 14 while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} 15 while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} 16 return x*f; 17 } 18 ////////////////////////////////////////////////////////////////////////// 19 const int maxn = 500+10; 20 21 int bit[maxn][maxn]; 22 23 void add(int x,int y,int v){ 24 for(int i=x; i<maxn; i+=i&-i) 25 for(int j=y; j<maxn; j+=j&-j) 26 bit[i][j] += v; 27 } 28 29 int sum(int x,int y){ 30 int res = 0; 31 for(int i=x; i>0; i-=i&-i) 32 for(int j=y; j>0; j-=j&-j) 33 res += bit[i][j]; 34 return res; 35 } 36 37 int main(){ 38 int n; 39 while(cin>>n && n){ 40 MS(bit); 41 int W,H; cin>>W>>H; 42 for(int i=0; i<n; i++){ 43 int x,y; cin>>x>>y; 44 add(x,y,1); 45 } 46 int s,t; cin>>s>>t; 47 int ans = 0; 48 for(int w=s; w<=W; w++) 49 for(int h=t; h<=H; h++){ 50 int tmp = sum(w,h)-sum(w-s,h)-sum(w,h-t)+sum(w-s,h-t); 51 if(tmp > ans) ans = tmp; 52 } 53 cout << ans << endl; 54 } 55 56 return 0; 57 }