前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Codeforces 738B】Spotlights

【Codeforces 738B】Spotlights

作者头像
饶文津
发布2020-06-02 11:12:05
3750
发布2020-06-02 11:12:05
举报
文章被收录于专栏:饶文津的专栏饶文津的专栏

Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which actors will follow. For each cell it is stated in the plan if there would be an actor in this cell or not.

You are to place a spotlight on the stage in some good position. The spotlight will project light in one of the four directions (if you look at the stage from above) — left, right, up or down. Thus, the spotlight's position is a cell it is placed to and a direction it shines.

A position is good if two conditions hold:

  • there is no actor in the cell the spotlight is placed to;
  • there is at least one actor in the direction the spotlight projects.

Count the number of good positions for placing the spotlight. Two positions of spotlight are considered to be different if the location cells or projection direction differ.

Input

The first line contains two positive integers n and m (1 ≤ n, m ≤ 1000) — the number of rows and the number of columns in the plan.

The next n lines contain m integers, 0 or 1 each — the description of the plan. Integer 1, means there will be an actor in the corresponding cell, while 0 means the cell will remain empty. It is guaranteed that there is at least one actor in the plan.

Output

Print one integer — the number of good positions for placing the spotlight.

Examples

input

2 4
0 1 0 0
1 0 1 0

output

9

input

4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0

output

20

Note

In the first example the following positions are good:

  1. the (1, 1) cell and right direction;
  2. the (1, 1) cell and down direction;
  3. the (1, 3) cell and left direction;
  4. the (1, 3) cell and down direction;
  5. the (1, 4) cell and left direction;
  6. the (2, 2) cell and left direction;
  7. the (2, 2) cell and up direction;
  8. the (2, 2) and right direction;
  9. the (2, 4) cell and left direction.

Therefore, there are 9 good positions in this example.

求所有0的所有有1的方向共几个。

类似求前缀和,把四个方向都求一下,然后累加就好了。

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using  namespace std;
int r,c,ans;
int a[1005][1004],s[1005][1004][5];
int main(){
    scanf("%d%d",&r,&c);
    for(int i=1;i<=r;i++)
        for(int j=1;j<=c;j++){
            scanf("%d",&a[i][j]);
            s[i][j][0]=s[i][j-1][0]|a[i][j];
            s[i][j][1]=s[i-1][j][1]|a[i][j];
        }
    for(int i=r;i;i--)
        for(int j=c;j;j--){
            s[i][j][2]=s[i][j+1][2]|a[i][j];
            s[i][j][3]=s[i+1][j][3]|a[i][j];
        }

    for(int i=1;i<=r;i++)
    for(int j=1;j<=c;j++)
    if(a[i][j]==0){
        ans+=s[i][j][0]+s[i][j][1]+s[i][j][2]+s[i][j][3];
    }
    printf("%d\n",ans);
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-11-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档