题目:http://poj.org/problem?id=2029
二维树状数组,0(n^2)枚举矩形右下角的坐标即可。
#include <cmath>
#include <ctime>
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
#include <stack>
#include <deque>
using namespace std;
typedef long long LL;
#define EPS 10e-9
#define INF 0x3f3f3f3f
#define REP(i,n) for(int i=0; i<(n); i++)
const int maxn = 110;
int c[maxn][maxn];
int lowbit(int x){ return x&-x;}
void add(int x,int y){
while(x<maxn){
int y1=y;
while(y1<maxn){
c[x][y1]+=1; y1+=lowbit(y1);
}
x+=lowbit(x);
}
}
int sum(int x,int y){
int ret=0;
while(x>0){
int y1=y;
while(y1>0){
ret+=c[x][y1]; y1-=lowbit(y1);
}
x-=lowbit(x);
}
return ret;
}
int main(){
int n,w,h,s,t,ans,x,y;
while(scanf("%d",&n)!=EOF){
if(n==0) break; ans=0;
memset(c,0,sizeof(c));
scanf("%d%d",&w,&h);
for(int i=0;i<n;i++){
scanf("%d %d",&x,&y);
add(x,y);
}
scanf("%d %d",&s,&t);
for(int i=s;i<=w;i++){
for(int j=t;j<=h;j++){
int temp=sum(i,j);
int x1=i-s; int y1=j-t;
if(x1>=0){
temp-=sum(x1,j);
}
if(y1>=0){
temp-=sum(i,y1);
}
if(x1>=0&&y1>=0){
temp+=sum(x1,y1);
}
if(temp>ans) {
ans=temp;
// printf("this %d %d %d\n",ans,i,j);
}
}
}
printf("%d\n",ans);
}
return 0;
}