标签:des style blog http color io os ar for
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<ctime> #include<cmath> #include<algorithm> #include<set> #include<map> #include<vector> #include<string> #include<queue> using namespace std; #ifdef WIN32 #define LL "%I64d" #else #define LL "%lld" #endif #define MAXN 2100 #define MAXV MAXN*2 #define MAXE MAXV*2 #define INF 0x3f3f3f3f #define INFL 0x3f3f3f3f3f3f3f3fLL typedef long long qword; inline int nextInt() { char ch; int x=0; bool flag=false; do ch=(char)getchar(),flag=(ch==‘-‘)?true:flag; while(ch<‘0‘||ch>‘9‘); do x=x*10+ch-‘0‘; while (ch=(char)getchar(),ch<=‘9‘ && ch>=‘0‘); return x*(flag?-1:1); } int n,m; typedef int arr_t[MAXN]; typedef int arr2[MAXN*2]; typedef arr_t map_t[MAXN]; map_t a,b; map_t sa,sb,tsb,tsa; void manacher(arr2 seq,int n,arr2 &res) { int id,mx; int i,j; id=mx=-1; int cnt=0; for (i=0;i<n;i++) { if (i<mx) { res[i]=min(mx-i,res[id*2-i]); }else res[i]=0; while (i-res[i]-1>=0 && i+res[i]+1<n && seq[i+res[i]+1]==seq[i-res[i]-1]) { res[i]++; //cnt++; } if (i+res[i]>mx)//Do not forget { mx=i+res[i]; id=i; } } return ; } void init_p(map_t mp,int n,int m,map_t &sl) { int i,j; arr2 seq,res; for (i=0;i<n;i++) { for (j=0;j<m;j++) { seq[j*2+1]=mp[i][j]; seq[j*2]=-INF; } seq[2*m]=-INF; manacher(seq,2*m+1,res); for (j=0;j<m;j++) { sl[i][j]=res[j*2+1]; } } } int pseq[MAXN]; int head,tail; map_t fa,fb,fc; void pm(map_t &a) { int i,j; for (i=0;i<n;i++) { for (j=0;j<m;j++) { printf("%d ",a[i][j]); } printf("\n"); } printf("\n"); } void work(map_t &sa,map_t &sb,map_t &fa,int n,int m) { int i,j; for (i=0;i<n;i++) for (j=0;j<m/2;j++) { swap(sb[i][j],sb[i][m-j-1]); swap(sa[i][j],sa[i][m-j-1]); } for (i=0;i<n;i++) { head=1,tail=1; pseq[0]=-1; pseq[1]=0; fa[i][m-1]=0; for (j=1;j<m;j++) { while (tail>=head && sb[i][j]<sb[i][pseq[tail]])tail--; pseq[++tail]=j; int l,r,mid; l=-1,r=tail; while (l+1<r) { int mid=(l+r)>>1; if (sb[i][pseq[mid]]>=j-pseq[mid]) r=mid; else l=mid; } fa[i][m-j-1]=min(j-(pseq[l]+1),min(sa[i][j],sb[i][pseq[r]])); } } for (i=0;i<n;i++) for (j=0;j<m/2;j++) { swap(sb[i][j],sb[i][m-j-1]); swap(sa[i][j],sa[i][m-j-1]); } for (i=0;i<n;i++) { head=1,tail=1; pseq[0]=-1; pseq[1]=0; fa[i][0]=0; for (j=1;j<m;j++) { while (tail>=head && sb[i][j]<sb[i][pseq[tail]])tail--; pseq[++tail]=j; int l,r,mid; l=-1,r=tail; while (l+1<r) { int mid=(l+r)>>1; if (sb[i][pseq[mid]]>=j-pseq[mid]) r=mid; else l=mid; } fa[i][j]=min(fa[i][j],min(j-(pseq[l]+1),min(sa[i][j],sb[i][pseq[r]]))); } } } int main() { //freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); int i,j,k; int x,y,z; int ans=0; scanf("%d%d",&n,&m); memset(a,INF,sizeof(a)); for (i=0;i<n;i++) { for (j=0;j<m;j++) { scanf("%d",&x); a[i*2+1][j*2+1]=x; b[j*2+1][i*2+1]=x; } } n*=2;m*=2;n++;m++; init_p(a,n,m,sa); init_p(b,m,n,tsb); for (i=0;i<n;i++) for (j=0;j<m;j++) sb[i][j]=tsb[j][i]; for (i=0;i<n;i++) { for (j=0;j<m;j++) { sa[i][j]/=2; sb[i][j]/=2; } } work(sa,sb,fa,n,m); for (i=0;i<n;i++) for (j=0;j<m;j++) tsa[i][j]=a[i][j]; for (i=0;i<n;i++) for (j=0;j<m;j++) b[i][m-j-1]=a[m-j-1][i]=tsa[i][j]; swap(n,m); init_p(a,n,m,sa); init_p(b,m,n,tsb); for (i=0;i<n;i++) for (j=0;j<m;j++) sb[i][j]=tsb[j][i]; for (i=0;i<n;i++) { for (j=0;j<m;j++) { sa[i][j]/=2; sb[i][j]/=2; } } work(sa,sb,fb,n,m); for (i=0;i<n;i++) for (j=0;j<m;j++) tsb[i][j]=fb[i][j]; swap(n,m); for (i=0;i<n;i++) for (j=0;j<m;j++) fb[i][j]=tsb[m-j-1][i]; //pm(fa); //pm(fb); for (i=0;i<n;i++) { for (j=0;j<m;j++) { fc[i][j]=max(0,min(fa[i][j]-(i%2==0),fb[i][j]-(j%2==0))); fc[i][j]=(fc[i][j]+1)/2; } } //pm(fc); for (i=0;i<n;i++) { for (j=0;j<m;j++) { if (i%2+j%2==1)continue; ans+=fc[i][j]; } } cout<<ans<<endl; }
bzoj 1414: [ZJOI2009]对称的正方形 manacher算法+單調隊列
标签:des style blog http color io os ar for
原文地址:http://www.cnblogs.com/mhy12345/p/4014716.html