Spotlights
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.
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.
Print one integer — the number of good positions for placing the spotlight.
2 4
0 1 0 0
1 0 1 0
9
4 4
0 0 0 0
1 0 0 1
0 1 1 0
0 1 0 0
20
In the first example the following positions are good:
- 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.
分析:预处理前缀和,模拟;
代码:
#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+10; using namespace std; ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} inline ll read() { ll x=0;int f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';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,1,n)rep(j,1,m)scanf("%d",&a[i][j]); rep(i,1,n)rep(j,1,m)b[i][j]=a[i][j]; rep(i,1,n) { rep(j,1,m) { b[i][j]+=b[i][j-1]; if(b[i][j]&&!a[i][j])ans++; } } rep(i,1,n)rep(j,1,m)b[i][j]=a[i][j]; rep(i,1,n) { for(j=m;j>=1;j--) { b[i][j]+=b[i][j+1]; if(b[i][j]&&!a[i][j])ans++; } } rep(i,1,n)rep(j,1,m)b[i][j]=a[i][j]; rep(j,1,m) { for(i=n;i>=1;i--) { b[i][j]+=b[i+1][j]; if(b[i][j]&&!a[i][j])ans++; } } rep(i,1,n)rep(j,1,m)b[i][j]=a[i][j]; rep(j,1,m) { rep(i,1,n) { b[i][j]+=b[i-1][j]; if(b[i][j]&&!a[i][j])ans++; } } printf("%lld\n",ans); //system("Pause"); return 0; }