首页 > 代码库 > CodeForces 738B Spotlights

CodeForces 738B Spotlights

前缀和。

对于每个位置,求出四个方向的前缀和即可。

#pragma comment(linker, "/STACK:1024000000,1024000000")#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<ctime>#include<iostream>using namespace std;typedef long long LL;const double pi=acos(-1.0);void File(){    freopen("D:\\in.txt","r",stdin);    freopen("D:\\out.txt","w",stdout);}template <class T>inline void read(T &x){    char c = getchar();    x = 0;    while(!isdigit(c)) c = getchar();    while(isdigit(c))    {        x = x * 10 + c - 0;        c = getchar();    }}int t[1010][1010],s[1010][1010][4];int n,m;int main(){    cin>>n>>m;    for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++) cin>>t[i][j];    for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++) s[i][j][0]=s[i][j-1][0]+t[i][j];    for(int i=1;i<=n;i++)        for(int j=m;j>=1;j--) s[i][j][1]=s[i][j+1][1]+t[i][j];    for(int j=1;j<=m;j++)        for(int i=1;i<=n;i++) s[i][j][2]=s[i-1][j][2]+t[i][j];    for(int j=1;j<=m;j++)        for(int i=n;i>=1;i--) s[i][j][3]=s[i+1][j][3]+t[i][j];    int ans=0;    for(int i=1;i<=n;i++)        for(int j=1;j<=m;j++)            if(t[i][j]==0)                for(int k=0;k<4;k++)                    if(s[i][j][k]) ans++;    cout<<ans<<endl;    return 0;}

 

CodeForces 738B Spotlights