Through observation, we can find that if the length of the given array is l, if l is odd, then ll's matrix appears periodically, if l is even, then 2L 2l's matrix appears periodically, so we can store 2l* 2l's matrix and write a function f, which accepts a point, returns the sum of the point and all the sub-elements in the matrix represented by 0 and 0 points, and then follows. All we need to do is to throw four points into this function to evaluate by using the exclusive relation.
#include <iostream> #include <math.h> #include <iomanip> #include <string> #include <cstdio> #include <stdio.h> #include <cstring> #include <algorithm> #include <queue> #include <vector> #define INF 0x3f3f3f3f #define N 17 #define M 998244353 #define ll long long using namespace std; ll A[10]; ll map[42][42]; ll t,l,q; ll cnt; ll pre[21][21]; ll g[21]; ll sum(ll x,ll y){ if(x == 0 || y == 0){ return 0; } for(int i=1;i<=2*l;i++){ g[i] = g[i-1] + pre[i][2*l] * (y/(2*l)) + pre[i][y%(2*l)]; } return g[2*l] * (x/(2*l)) + g[x%(2*l)]; } int main() { ll i,j; scanf("%lld",&t); while (t > 0) { t--; ll x1,y1,x2,y2; scanf("%lld",&l); for(i=0;i<l;i++){ scanf("%lld",&A[i]);//Starting from 0 } cnt = -1; for(i = 1;i<= 4*l;i++){ for(j=1;j<=i;j++){ cnt = (cnt+1)%l; map[j][i+1-j] = A[cnt]; } } for(i=1;i<=2*l;i++){ for(j=1;j<=2*l;j++){ pre[i][j] = pre[i][j-1] + map[i][j]; } } scanf("%lld",&q); for(i=1;i<=q;i++){ scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2); //cout<<sum(x1-1,y1-1)<<endl; //cout<<sum(x2,y2)<<endl; y1++,x1++,y2++,x2++; ll ans = sum(x1-1,y1-1) + sum(x2,y2) - sum(x2,y1-1) - sum(x1-1,y2); printf("%lld\n",ans); } } return 0; }