BZOJ1218 (prefix and two-dimensional)

1218: [HNOI2003] laser bombs
analysis: the classic two-dimensional prefix and problems. In two dimensions, according to inclusion and exclusion, the two-dimensional prefix from (1,1) to (x, y) and \ (S (x, y) = S (x-1, y) + S (x, . 1-Y) -S (. 1-X, Y) + A (X-, the Y) \) . Similarly, according to the principle of inclusion and exclusion, two-dimensional interval \ ((X_1, Y_1) \ ) to \ ((X_2, Y_2) \ ) prefix and is \ (S (X_2, Y_2) -S (X_1, Y_2) - S (X_2, Y_1) + S (X_1, Y_1) \)

#include "bits/stdc++.h"
using namespace std;
const int maxn=5e3+10;
int n,a[maxn][maxn],R;
int main()
{
    //freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&R);
    for(int i=1;i<=n;i++){
        int x,y,v;
        scanf("%d%d%d",&x,&y,&v);
        a[x+1][y+1]+=v;
    }
    for(int i=1;i<=5001;i++)
        for(int j=1;j<=5001;j++)
            a[i][j]=a[i-1][j]+a[i][j-1]-a[i-1][j-1]+a[i][j];
    int mx=0;
    for(int i=R;i<=5001;i++)
        for(int j=R;j<=5001;j++)
            mx=max(mx,a[i][j]-a[i-R][j]-a[i][j-R]+a[i-R][j-R]);
    printf("%d\n",mx);
    return 0;
}

Guess you like

Origin www.cnblogs.com/gzgywh/p/12081853.html