New Year and Domino T16 D57
Problem - C - Codeforces (Unofficial mirror site, accelerated for Chinese users)
思路
\(l[i][j]\)表示第i行中到j列这一行横着摆放产生的可能性
\(r[i][j]\)表示第j列这一列竖着摆放产生的可能性
for(int i=x;i<=xx;++i) ans+=l[i][yy]-l[i][y];
for(int i=y;i<=yy;++i) ans+=r[xx][i]-r[x][i];
参考代码
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define si size()
using namespace std;
ll read(){ll x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline void Prin(ll x){if(x < 0){putchar('-');x = -x;}if(x > 9) Prin(x / 10);putchar(x % 10 + '0');}
const int qs=1e5+7;
int n,m,q,l[507][507],r[507][507];
char c[507][507];
int main(){
n=read(),m=read();
for(int i=1;i<=n;++i) cin>>c[i]+1;
int cnt=0,sum;
for(int i=1;i<=n;++i){
cnt=0,sum=0;
for(int j=1;j<=m;++j){
if(j==1||c[i][j-1]=='#'||c[i][j]=='#'){
l[i][j]=l[i][j-1];
sum+=cnt; cnt=0;
}
else cnt++,l[i][j]=sum+cnt;
}
}
for(int j=1;j<=m;++j){
cnt=0,sum=0;
for(int i=1;i<=n;++i){
if(i==1||c[i-1][j]=='#'||c[i][j]=='#'){
r[i][j]=r[i-1][j];
sum+=cnt;
cnt=0;
}
else cnt++,r[i][j]=cnt+sum;
}
}
q=read();
while(q--){
int x,y,yy,xx,ans=0;
cin>>x>>y>>xx>>yy;
for(int i=x;i<=xx;++i) ans+=l[i][yy]-l[i][y];
for(int i=y;i<=yy;++i) ans+=r[xx][i]-r[x][i];
cout<<ans<<"\n";
}
return 0;
}