思想 : 用堆维护最大值最小值即可 暴力实现 复杂度 N^2 * log(N^2)
代码:
#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef array<int,3> arr;
const int N = 1001;
const int mod=998244353;
int n,m,a,b;
int g[N][N];
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin>>n>>m>>a>>b;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>g[i][j];
}
}
priority_queue<arr,vector<arr>,greater<arr>> heap;
priority_queue<arr> q;
int ans=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
q.push({g[i][j],i,j});
heap.push({g[i][j],i,j});
if(i>=a&&j>=b){
auto x1=q.top();
auto x2=heap.top();
while(x1[1]<=i-a||x1[2]<=j-b){
q.pop(),x1=q.top();
}
while(x2[1]<=i-a||x2[2]<=j-b){
heap.pop(),x2=heap.top();
}
ans=(ans+1ll*(x1[0]*x2[0]%mod))%mod;
}
}
}
cout<<ans<<"\n";
return 0;
}