Spotlights
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

分析:预处理前缀和,模拟;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, ls[rt]
#define Rson mid+1, R, rs[rt]
#define sys system("pause")
#define intxt freopen("in.txt","r",stdin)
const int maxn=1e3+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,a[maxn][maxn],b[maxn][maxn];
ll ans;
int main()
{
int i,j;
scanf("%d%d",&n,&m);
rep(i,,n)rep(j,,m)scanf("%d",&a[i][j]);
rep(i,,n)rep(j,,m)b[i][j]=a[i][j];
rep(i,,n)
{
rep(j,,m)
{
b[i][j]+=b[i][j-];
if(b[i][j]&&!a[i][j])ans++;
}
}
rep(i,,n)rep(j,,m)b[i][j]=a[i][j];
rep(i,,n)
{
for(j=m;j>=;j--)
{
b[i][j]+=b[i][j+];
if(b[i][j]&&!a[i][j])ans++;
}
}
rep(i,,n)rep(j,,m)b[i][j]=a[i][j];
rep(j,,m)
{
for(i=n;i>=;i--)
{
b[i][j]+=b[i+][j];
if(b[i][j]&&!a[i][j])ans++;
}
}
rep(i,,n)rep(j,,m)b[i][j]=a[i][j];
rep(j,,m)
{
rep(i,,n)
{
b[i][j]+=b[i-][j];
if(b[i][j]&&!a[i][j])ans++;
}
}
printf("%lld\n",ans);
//system("Pause");
return ;
}

Spotlights的更多相关文章

  1. Codeforces #380 div2 B(729B) Spotlights

    B. Spotlights time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  2. 【Codeforces 738B】Spotlights

    Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which act ...

  3. [CF738B]Spotlights(前缀和,模拟)

    题目链接:http://codeforces.com/contest/738/problem/B 题意:问多少个0的方向,使得方向上至少有一个1. 四个方向统计一遍前缀和,向上向左正着记,向下向右倒着 ...

  4. Codeforces Round #380 (Div. 2)/729B Spotlights 水题

    Theater stage is a rectangular field of size n × m. The director gave you the stage's plan which act ...

  5. 【47.76%】【Round #380B】Spotlights

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. Spotlights【思维+前缀和优化】

    https://blog.csdn.net/mengxiang000000/article/details/53291883   原博客地址 http://codeforces.com/group/1 ...

  7. Unity 5 中的全局光照技术详解

    貌似是某位好人翻译的 https://unity3d.com/cn/learn/tutorials/topics/graphics/unity-5-lighting-and-rendering#rd? ...

  8. Codeforces Round #380 (Div. 2) 总结分享

    B. Spotlights 题意 有n×m个格子的矩形舞台,每个格子里面可以安排一个演员或聚光灯,聚光灯仅可照射一个方向(俯视,上下左右).若聚光灯能照到演员,则称为"good positi ...

  9. Unity 用户手册用户指南二维纹理 (Texture 2D)

    http://www.58player.com/blog-2327-953.html 二维纹理 (Texture 2D) 纹理 (Textures) 使您的 网格 (Meshes).粒子 (Parti ...

随机推荐

  1. java中的日期处理

    学习Java日期处理,看见这一篇比较详细,转载之. 转自:http://www.cnblogs.com/hqr9313/archive/2012/04/19/2458221.html   时间日期1) ...

  2. hdu_5810_Balls and Boxes(打表推公式)

    题目链接:hdu_5810_Balls and Boxes 题意: 如题,让你求那个公式的期望 题解: 打表找规律,然后推公式.这项技能必须得学会 #include<cstdio> #in ...

  3. 判断一个数是否为2的n次幂

    参考:http://bbs.csdn.net/topics/370058619 如题,如何判断一个整数是否是2的N次方,我能想到的方法有两个 1.一直除2,看最后是否等于1.(最笨的方法) 2.转换成 ...

  4. 应用 Valgrind 发现 Linux 程序的内存问题(转)

    Valgrind 概述 体系结构 Valgrind 是一套Linux下,开放源代码(GPL V2)的仿真调试工具的集合.Valgrind由内核(core)以及基于内核的其他调试工具组成.内核类似于一个 ...

  5. 一步一步学EF系列3【升级篇 实体与数据库的映射】

    之前的三张为基础篇,如果不考虑架构问题,做一般的小程序,以足够用了.基本的增删改查也都有了.但是作为学习显然是不够的.通过之前三章的学习,有没有发现这样写有什么问题,有没有觉得繁琐的?可能有人会说,之 ...

  6. 201312月CCF-2,ISBN号码分析

    明天要考CCF啦,偶还是很紧张的.最近看了数据结构,今天才开始上机练习,对,我就是这么懒..废话不多说,我写这篇文章主要是分析CCF编程的小窍门,因为在网上没找到,所以我决定自力更生丰衣足食.!!!! ...

  7. Merge Into 用法

    从一个数据库的一张表同步数据到另外一个数据库的一张表,同步的数据不是insert就是update. 一般做法是先判断当前数据在另外一张表存不存在,存在则更新,不存在则插入.需要一次查询判断:exist ...

  8. angularJS 系列(七)---指令

    ----------------------------------------------------------------------------------- 原文:https://www.s ...

  9. iOS使用Swift语言检查并提示更新

    项目要上线了,产品要求跟安卓一样,一进去有一个提示框提示更新.虽然苹果在 Settings/iTunes Store & App Store 选项中有自动更新这一选项,而且添加版本号等等有被拒 ...

  10. smartGit注意点

    1.xcode工程默认会创建一个.git文件,去掉勾选 2.文件夹不显示,点击smartGit导航栏上的view-->Files from Subdirectories