您的位置:首页 > 其它

Technocup 2017 — Elimination Round 2 (and Codeforces Round 380) Editorial B. Spotlights

2016-11-22 19:27 519 查看
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:#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
const int inf=0x3f3f3f3f;
int a[1010][1010];
int main()
{
int n,m,i,j;
while(~scanf("%d%d",&n,&m))
{
int ans=0;
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
scanf("%d",&a[i][j]);
}
}
int sum=0;
for(i=1; i<=n; i++)
{
int maxn=-1,minn=inf,l;
ans=0;
for(j=1; j<=m; j++)
{
if(a[i][j]==1)
{
ans++;
l=j;
if(l>maxn)
maxn=l;
if(l<minn)
minn=l;
}
}
if(ans==1)
sum+=m-1;
else if(ans>=2)
{
sum+=minn-1;
sum+=m-maxn;
sum+=2*((m-ans)-(minn-1)-(m-maxn));
}
}
for(i=1; i<=m; i++)
{
int maxn=-1,minn=inf,l;
ans=0;
for(j=1; j<=n; j++)
{
if(a[j][i]==1)
{
ans++;
l=j;
if(l>maxn)
maxn=l;
if(l<minn)
minn=l;
}
}
if(ans==1)
{sum+=n-1; }
else if(ans>=2)
{
sum+=minn-1;
sum+=n-maxn;
sum+=2*((n-ans)-(minn-1)-(n-maxn));
}
}
printf("%d\n",sum);
}
return 0;
}


the (1, 1) cell and right direction;

the (1, 1) cell and down direction;

the (1, 3) cell and left direction;

the (1, 3) cell and down direction;

the (1, 4) cell and left direction;

the (2, 2) cell and left direction;

the (2, 2) cell and up direction;

the (2, 2) and right direction;

the (2, 4) cell and left direction.

Therefore, there are 9 good positions in this example.

这道题,我比赛做的超时可知,我的水平很low 啊,真伤心啊,于是,我就想我一定要自己优化出来,现在就出来了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐