【LOJ #3266】「USACO 2020.2 Platinum」Equilateral Triangles(曼哈顿距离转切比雪夫距离)

传送门

转成切比雪夫距离后发现就是满足三个点中有两个在正方形端点
另一个在对边上的方案数
横竖分别记一下前缀和做一下即可

#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define y1 shinkle
#define tm shiki
#define data shiroha
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ib==ob)?EOF:*ib++;
}
inline int read(){
    char ch=gc();
    int res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
inline ll readll(){
    char ch=gc();
    ll res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
inline int readstring(char *s){
	int top=0;char ch=gc();
	while(isspace(ch))ch=gc();
	while(!isspace(ch)&&ch!=EOF)s[++top]=ch,ch=gc();
	s[top+1]='\0';return top;
}
template<typename tp>inline void chemx(tp &a,tp b){a=max(a,b);}
template<typename tp>inline void chemn(tp &a,tp b){a=min(a,b);}
cs int N=605;
int n,a[N][N],s[N][N];
char str[N];
int main(){
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	n=read();
	for(int i=1;i<=n;i++){
		readstring(str);
		for(int j=1;j<=n;j++)if(str[j]=='*')
		a[i+j][i-j+n]=1;
	}n=n+n;
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++)
	s[i][j]=s[i][j-1]+a[i][j];
	ll res=0;
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++)if(a[i][j])
	for(int k=j+1;k<=n;k++)if(a[i][k]){
		int d=(k-j);
		if(i>d)res+=s[i-d][k]-s[i-d][j-1];
		if(i+d<=n)res+=s[i+d][k]-s[i+d][j-1];
	}
	for(int j=1;j<=n;j++)
	for(int i=1;i<=n;i++)
	s[i][j]=s[i-1][j]+a[i][j];
	for(int j=1;j<=n;j++)
	for(int k=j+1;k<=n;k++)
	for(int i=1;i<=n;i++)if(a[k][i]&&a[j][i]){
		int d=(k-j);
		if(i>d)res+=s[k-1][i-d]-s[j][i-d];
		if(i+d<=n)res+=s[k-1][i+d]-s[j][i+d];
	}cout<<res<<'\n';return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42555009/article/details/105718624