B. 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.

一道水题,比赛时没看复杂度,终测时TLE了,醉人。。。

#include<iostream>
#include<cstdio>
using namespace std;
#define N 1005 int gra[N][N];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=; i<n; i++)
for(int j=; j<m; j++)
scanf("%d",&gra[i][j]);
int res=;
for(int i=; i<n; i++)
{
int st=-,en=-;
for(int j=; j<m; j++)
{
if(gra[i][j]==)
{
if(st==-)
st=j;
en=j;
}
}
if(st>=)
{
res+=st;
for(int k=;k<st;k++)
if(gra[i][k]==)
res--;
if(en>st)
{
res+=(en-st-)*;
for(int k=st+;k<en;k++)
if(gra[i][k]==)
res-=;
}
res+=(m--en);
for(int k=en+;k<m;k++)
if(gra[i][k]==)
res--;
}
} for(int j=; j<m; j++)
{
int st=-,en=-;
for(int i=; i<n; i++)
{
if(gra[i][j]==)
{
if(st==-)
st=i;
en=i;
}
}
if(st>=)
{
res+=st;
for(int k=;k<st;k++)
if(gra[k][j]==)
res--;
if(en>st)
{
res+=(en-st-)*;
for(int k=st+;k<en;k++)
if(gra[k][j]==)
res-=;
}
res+=(n--en);
for(int k=en+;k<n;k++)
if(gra[k][j]==)
res--;
}
}
printf("%d\n",res);
return ;
}

Codeforces_738B的更多相关文章

随机推荐

  1. 马悦:《Linux内核分析》MOOC课程

    http://www.cnblogs.com/20135235my/p/5237267.html

  2. jq页面提示或者页面牵引浏览--页面的指引向导插件

    1.看看插件效果吧 2. html 文件 :index.html <!DOCTYPE html> <html lang="en"> <head> ...

  3. ci get_instance()

    你随便下个CI框架的源码都会看到很多的get_instance() 函数,这个函数是用来获取CI 的全局超级对象,CI 是单例模式的框架,所有全局有一个超级对象.因为只有一个实例,所以无论这个函数使用 ...

  4. ORA-01925:maximum of 80 enabled roles exceeded

    ORA-01925:maximum of 80 enabled roles exceeded max_enabled_roles 9i的參数,10g及以后都不用了. 指定用户session的最大ena ...

  5. webservice Connection timed out

    webservice Connection timed out,当发生webservice的链接超时错误时.我想原因无非就是webclient到webservice之间的链接通路发生了异常,那么该怎样 ...

  6. effective C++ 读书笔记 条款14 以对象管理资源

    如果我们使用一个投资行为的程序库: #include "stdafx.h" #include <iostream> #include <memory> us ...

  7. Android 应用按返回键异常退出的问题

    开发过程中遇到按返回键异常退出的问题,log显示为空指针异常,进一步产看是由于onActivityResult得到的Intent为空. 按返回键复写代码例如以下: @Override public v ...

  8. 从一个小demo开始,体验“API经济”的大魅力

    写在前面 “API经济”这个词是越来越火了,但是"API经济"具体指的是什么,相信很多人还没有个明确的认识.不过今天我可不打算长篇大论的去讲解一些概念,我们就以“电话号码归属地查询 ...

  9. SpringBoot之Web开发——webjars&静态资源映射规则

    在webjars中找到需要引入的Maven依赖,添加到pom.xml中,即可自动导入相关依赖.

  10. react 使用

    我的有道云笔记 React 事件: 1.不能使用 return false; 来阻止元素的默认行为.需要在方法的最前面使用 e.preventDefault() 来阻止元素的默认行为(例如:a 标签的 ...