1 条题解
-
2$$\sum_{(x,y)} C_{x,y}\\ =\sum_{a\le x\le c} \sum_{b\le y\le d} \sum_{k=1}^n A_{x,k}\times B_{k,y}\\ =\sum_{k=1}^n \sum_{a\le x\le c} \sum_{b\le y\le d} A_{x,k}\times B_{k,y}\\ =\sum_{k=1}^n \sum_{a\le x\le c} A_{x,k}\times \left(\sum_{b\le y\le d} B_{k,y}\right)\\ =\sum_{k=1}^n \bigg(\left(\sum A\right)\left(\sum B\right)\bigg) $$
维护两个前缀和即可。极其卡常。
卡常前代码:
#include<iostream> #include<algorithm> #include<vector> #include<map> #define int long long using namespace std; int n,m,a[2012][2012],b[2012][2012]; signed main(){ ios::sync_with_stdio(0); cin.tie(0); cin>>n>>m; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>a[i][j],a[i][j]+=a[i-1][j]; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>b[i][j],b[i][j]+=b[i][j-1]; while(m--){ int x1,y1,x2,y2; cin>>x1>>y1>>x2>>y2; if(x1>x2) swap(x1,x2); if(y1>y2) swap(y1,y2); int res=0; for(int k=1;k<=n;k++) res+=(a[x2][k]-a[x1-1][k])*(b[k][y2]-b[k][y1-1]); cout<<res<<'\n'; } return 0; }卡常工作由 DeepSeek 完成(?)
#pragma GCC optimize("O3,unroll-loops") #include<bits/stdc++.h> #define int long long using namespace std; using ll=long long; const int MAXN=2012; int a[MAXN][MAXN],b[MAXN][MAXN]; namespace io{ const int S=1<<20; char buf[S],*p1,*p2; inline char gc(){ if(p1==p2){ p2=(p1=buf)+fread(buf,1,S,stdin); if(p1==p2) return EOF; } return *p1++; } template<class T>inline void read(T&x){ x=0;char c=gc();bool f=0; while(c<'0'||c>'9'){ if(c=='-')f=1; c=gc(); } while(c>='0'&&c<='9'){ x=x*10+(c-'0'); c=gc(); } if(f)x=-x; } char obuf[S],*op=obuf; inline void pc(char c){ if(op-obuf==S){ fwrite(obuf,1,S,stdout); op=obuf; } *op++=c; } inline void write(ll x){ if(x==0){ pc('0'); pc('\n'); return; } if(x<0){ pc('-'); x=-x; } char s[30]; int n=0; while(x){ s[n++]=char('0'+x%10); x/=10; } while(n--) pc(s[n]); pc('\n'); } struct Flusher{ ~Flusher(){ if(op!=obuf) fwrite(obuf,1,op-obuf,stdout); } }flusher; } signed main(){ ios::sync_with_stdio(0); cin.tie(0); int n,m; io::read(n); io::read(m); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ io::read(a[i][j]); a[i][j]+=a[i-1][j]; } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++){ io::read(b[i][j]); b[i][j]+=b[i][j-1]; } for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) swap(b[i][j],b[j][i]); while(m--){ int x1,y1,x2,y2; io::read(x1); io::read(y1); io::read(x2); io::read(y2); if(x1>x2) swap(x1,x2); if(y1>y2) swap(y1,y2); ll*A2=a[x2],*A1=a[x1-1],*B2=b[y2],*B1=b[y1-1]; ll r0=0,r1=0,r2=0,r3=0,r4=0,r5=0,r6=0,r7=0; int k=1; for(;k+7<=n;k+=8){ r0+=(A2[k]-A1[k])*(B2[k]-B1[k]); r1+=(A2[k+1]-A1[k+1])*(B2[k+1]-B1[k+1]); r2+=(A2[k+2]-A1[k+2])*(B2[k+2]-B1[k+2]); r3+=(A2[k+3]-A1[k+3])*(B2[k+3]-B1[k+3]); r4+=(A2[k+4]-A1[k+4])*(B2[k+4]-B1[k+4]); r5+=(A2[k+5]-A1[k+5])*(B2[k+5]-B1[k+5]); r6+=(A2[k+6]-A1[k+6])*(B2[k+6]-B1[k+6]); r7+=(A2[k+7]-A1[k+7])*(B2[k+7]-B1[k+7]); } ll res=r0+r1+r2+r3+r4+r5+r6+r7; for(;k<=n;k++) res+=(A2[k]-A1[k])*(B2[k]-B1[k]); io::write(res); } return 0; }
- 1
信息
- ID
- 858
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 9
- 标签
- (无)
- 递交数
- 52
- 已通过
- 3
- 上传者