每个格子记录其左下的45°直角梯形区域的和及左下矩形区域的和即可。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define N 3010 #define M 3000010 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int n,m,q; unsigned int A,B,C,ans,p[M],a[N][N],b[N][N]; inline unsigned int rng61() { A^=A<<16; A^=A>>5; A^=A<<1; unsigned int t=A; A=B; B=C; C^=t^A; return C; } int main() { #ifndef ONLINE_JUDGE freopen("bzoj4972.in","r",stdin); freopen("bzoj4972.out","w",stdout); const char LL[]="%I64d\n"; #else const char LL[]="%lld\n"; #endif scanf("%d%d%d%u%u%u",&n,&m,&q,&A,&B,&C); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) a[i][j]=rng61(); for (int i=n;i>=1;i--) for (int j=1;j<=m;j++) b[i][j]=a[i][j]+=a[i+1][j]; for (int i=n;i>=1;i--) for (int j=1;j<=m;j++) a[i][j]+=a[i+1][j-1],b[i][j]+=b[i][j-1]; p[0]=1;for (int i=1;i<=q;i++) p[i]=p[i-1]*233; for (int i=1;i<=q;i++) { int x=rng61()%n+1,y=rng61()%m+1,k=rng61()%min(x,y)+1; unsigned int tot=a[x-k+1][y]-a[x+1][y-k]-b[x+1][y]+b[x+1][y-k]; ans+=tot*p[q-i]; } cout<<ans; return 0; }