1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| #include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i,a,b) for(int i=a,i##end=b;i<=i##end;++i) #define drep(i,a,b) for(int i=a,i##end=b;i>=i##end;--i)
char IO; template <class T=int> T rd(){ T s=0; int f=0; while(!isdigit(IO=getchar())) if(IO=='-') f=1; do s=(s<<1)+(s<<3)+(IO^'0'); while(isdigit(IO=getchar())); return f?-s:s; }
const int N=1e3+10;
int n; char a[N][N]; int D[N][N],U[N][N]; int stk[N],c[N],top; int CRR[N][N]; int CLL[N][N]; int CLR[N][N]; int CRL[N][N]; ll SLL[N][N],SRL[N][N];
int main(){ n=rd(); rep(i,1,n) scanf("%s",a[i]+1); rep(i,1,n) rep(j,1,n) if(a[i][j]=='C') U[i][j]=U[i-1][j]+1; drep(i,n,1) rep(j,1,n) if(a[i][j]=='C') D[i][j]=D[i+1][j]+1; rep(i,1,n) { top=0; int now=0; rep(j,1,n) { int x=U[i][j],cnt=1; while(top && stk[top]>=x) cnt+=c[top],now-=c[top]*stk[top],top--; stk[++top]=x,c[top]=cnt; now+=x*cnt; CRR[i][j]=max(now-1,0); }
now=top=0; rep(j,1,n) { int x=D[i][j],cnt=1; while(top && stk[top]>=x) cnt+=c[top],now-=c[top]*stk[top],top--; stk[++top]=x,c[top]=cnt; now+=x*cnt; CLR[i][j]=max(now-1,0); }
now=top=0; drep(j,n,1) { int x=U[i][j],cnt=1; while(top && stk[top]>=x) cnt+=c[top],now-=c[top]*stk[top],top--; stk[++top]=x,c[top]=cnt; now+=x*cnt; CRL[i][j]=max(now-1,0); }
now=top=0; drep(j,n,1) { int x=D[i][j],cnt=1; while(top && stk[top]>=x) cnt+=c[top],now-=c[top]*stk[top],top--; stk[++top]=x,c[top]=cnt; now+=x*cnt; CLL[i][j]=max(now-1,0); } }
drep(i,n,1) drep(j,n,1) SLL[i][j]=SLL[i+1][j]+SLL[i][j+1]-SLL[i+1][j+1]+CLL[i][j]; rep(i,1,n) drep(j,n,1) SRL[i][j]=SRL[i-1][j]+SRL[i][j+1]-SRL[i-1][j+1]+CRL[i][j];
ll ans=0; rep(i,1,n) rep(j,1,n) if(CRR[i][j]) ans+=CRR[i][j]*(SLL[i+1][1]+SLL[1][j+1]-SLL[i+1][j+1]); rep(i,1,n) rep(j,1,n) ans-=CLR[i][j]*SRL[i-1][j+1]; printf("%lld\n",ans%10007); }
|