jzoj4791. 【NOIP2016提高A组模拟9.21】矩阵

考虑2种放置木牌的方案,前缀和即可

注意细节

#include<bits/stdc++.h>
using namespace std;
#define N 510 
int n,m,r[N][N],c[N][N],a[N][N],s[N][N],r1,c1,r2,c2,q;
int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        char c[N];
        scanf("%s",c+1);
        for(int j=1;j<=m;j++)
            if(c[j]=='#')a[i][j]=1;
    }
    for(int i=1;i<=n;i++)a[i][0]=1;
    for(int i=1;i<=m;i++)a[0][i]=1;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++){
            int dt=0;
            c[i][j]=c[i-1][j];r[i][j]=r[i][j-1];
            if(!a[i][j]&&!a[i-1][j])r[i][j]++,dt++;
            if(!a[i][j]&&!a[i][j-1])c[i][j]++,dt++;
            s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+dt;
        }
    scanf("%d",&q);
    while(q--){
        scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
        printf("%d\n",s[r2][c2]-s[r1-1][c2]-s[r2][c1-1]+s[r1-1][c1-1]-(r[r1][c2]-r[r1][c1-1])-(c[r2][c1]-c[r1-1][c1]));
    }
}

猜你喜欢

转载自www.cnblogs.com/rilisoft/p/10986641.html
今日推荐