New Year and Domino T16 D57

2021/10/7 6:11:30

本文主要是介绍New Year and Domino T16 D57,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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;
}


这篇关于New Year and Domino T16 D57的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程