题解

1 条题解

  • 0
    @ 2017-10-07 20:00:57

    //---------------------------------------------AC code---------------------------------------------//

    #include<cstdio>
    
    using namespace std;
    
    const int N = 2005;
    int n, m, q, node[N][N], hor[N][N], ver[N][N], x1, y1, x2, y2;
    char g[N][N];
    
    int main(){
        freopen("duty.in", "r", stdin);
        freopen("duty.out", "w", stdout);
        scanf("%d%d%d", &n, &m, &q);
        for(int i = 1; i <= n; i++)
            scanf("%s", g[i]+1);
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                node[i][j] = node[i-1][j] + node[i][j-1] - node[i-1][j-1];
                if(g[i][j] == '1')  node[i][j]++;
            }
        }
        for(int i = 1; i <= n; i++){
            for(int j = 1; j < m; j++){
                hor[i][j] = hor[i-1][j] + hor[i][j-1] - hor[i-1][j-1];
                if(g[i][j] == '1' && g[i][j+1] == '1')  hor[i][j]++;
            }
        }
        for(int i = 1; i < n; i++){
            for(int j = 1; j <= m; j++){
                ver[i][j] = ver[i-1][j] + ver[i][j-1] - ver[i-1][j-1];
                if(g[i][j] == '1' && g[i+1][j] == '1')  ver[i][j]++;
            }
        }
        while(q--){
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
            int node_num = node[x2][y2] - node[x2][y1-1] - node[x1-1][y2] + node[x1-1][y1-1];
            int hor_num = hor[x2][y2-1] - hor[x2][y1-1] - hor[x1-1][y2-1] + hor[x1-1][y1-1];
            int ver_num = ver[x2-1][y2] - ver[x2-1][y1-1] - ver[x1-1][y2] + ver[x1-1][y1-1];
            printf("%d\n", node_num - hor_num - ver_num);
        }
        return 0;
    }
    
  • 1

信息

难度
9
分类
(无)
标签
递交数
8
已通过
4
通过率
50%
上传者