Topic Link: C. 01 Matrix of the Pain
The main topic: The original question is very clear, do not need to simplify _ (: З"∠) _
The puzzle: Set \ (r_i\) for the number of the I\ line 0, \ (c_j\) is the number of 0 in the column \ (j\), \ (f_{i,j}\) to indicate whether the corresponding lattice is 0, then there is a \ (Cost (I,J) =r_i+c_j-f_{i,j}\), \ (Cost (I, j)) ^2=r_i^2+c_j^2+f_{i,j}+2r_ic_j-2f_{i,j} (R_i+c_j) \)
$$\sum_{i=1}^n \sum_{j=1}^n \left (Cost (i,j) \right) ^2 = \sum_{i=1}^n (r_i^2+c_i^2) +\sum_{i=1}^n \sum_{j=1}^nf_{i,j}+2 (\sum_{i=1}^nr_i) (\sum_{j=1}^nc_j) -2f_{i,j}\sum_{i=1}^n \sum_{j=1}^n (R_i+c_j) $$
In the initial state, \ (ans=n^2* (2n-1) ^2, r_i=c_i=n\), give \ (k\) A 1 square can be considered as a \ (k\) reverse operation, then Bashi allele each item one by one corresponding to the modification is good
#include <bits/stdc++.h>using namespacestd;#defineN 200001#defineLL Long Long#defineMOD 1000000007LL N,k,q,x,y,u,v,r[n],sr[n],c[n],sc[n],ans;Set<LL>s;voidAdd (LL x,ll y) {s.insert (x*n+y); R[X]--, c[y]--; Ans+=mod-n* (2ll*r[x]+1)%mod,ans%=MOD; Ans+=mod-n* (2ll*c[y]+1)%mod,ans%=MOD; Ans+=mod-1, ans%=MOD; Ans+=2ll* (mod-sc[n]+mod-sr[n]+1), ans%=MOD; Ans+=2ll* (r[x]+1+c[y]+1)%mod,ans%=MOD; Ans+=2ll*r[x]%mod+2ll*c[y]%mod,ans%=MOD; Sc[n]--, sr[n]--;}voiddel (LL x,ll y) {sc[n]++,sr[n]++; Ans+=mod-(2ll*r[x]%mod+2ll*c[y]%mod)%mod,ans%=MOD; Ans+=mod-(2ll* (r[x]+1+c[y]+1)%mod)%mod,ans%=MOD; Ans+=2ll* (sc[n]+sr[n]-1)%mod,ans%=MOD; Ans++,ans%=MOD; Ans+=n* (2ll*c[y]+1)%mod,ans%=MOD; Ans+=n* (2ll*r[x]+1)%mod,ans%=MOD; R[X]++,c[y]++; S.erase (x*n+y);}intMain () {scanf ("%lld%lld%lld",&n,&k,&q); for(LL i=1; i<=n;i++) {R[i]=c[i]=N; Sr[i]= (sr[i-1]+r[i])%MOD; Sc[i]= (sc[i-1]+c[i])%MOD; } ans=4ll*n*n-4ll*n+1, ans%=MOD; Ans*=n*n%mod,ans%=MOD; for(LL i=1; i<=k;i++) scanf ("%lld%lld",&x,&y), add (x, y); printf ("%lld\n", ans); for(LL i=1; i<=q;i++) {scanf ("%lld%lld",&u,&v); if(S.count (u*n+v)) Del (U,V); ElseAdd (u,v); printf ("%lld\n", ans); } return 0;}
View Code
[Eoj Monthly 2018.10] [C. 01 matrix of Pain]