【题目链接】

int cursor = 0;
for (int i = 0; ; ++i) {
for (int j = 0; j <= i; ++j) {
M[j][i - j] = A[cursor];
cursor = (cursor + 1) % L;
}
}

题目意思

给你一个T,表示案例数量,给次给一个n,n个数字,要求按上面公式打表,每次询问,给两个点,要求计算以两个点位顶点的矩阵中元素的和。

多打几次表,可以发现当n为奇数时,a[i][j]=a[i-2n][j]=a[i][j-2n],所以对每个点分块化,其中query函数是求(0,0)与(i,j)构成的矩阵中的元素和,具体看AC代码。

#include <iostream>
using namespace std;
int map[100][100], sum[100][100];
int n;
long long query(int x, int y)
{
    if (x < 0 || y < 0)
        return 0;
    x++;
    y++;
    int t = 2 * n;
    long long int ans = 0;
    ans += (long long)sum[t - 1][t - 1] * (x/t)*(y/t);
    if (x % t != 0)
        ans += (long long)sum[x%t - 1][t - 1] * (y / t);
    if (y % t != 0)
        ans += (long long)sum[t - 1][y%t - 1] * (x / t);
    if (x % t != 0 && y % t != 0)
        ans += (long long)sum[x%t - 1][y%t - 1];
    return ans;
}
int main()
{
    int t, q, lx, ly, rx, ry;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d", &n);
        int array[11];
        for (int i = 0; i < n; i++)
            scanf("%d", &array[i]);

        int cursor = 0;
        for (int i = 0; i < 4 * n; ++i) 
        {
            for (int j = 0; j <= i; ++j) 
            {
                map[j][i - j] = array[cursor];
                cursor = (cursor + 1) % n;
            }
        }
        /*
        for (int i = 0; i < 4 * n; ++i)
        {
            for (int j = 0; j < 4 * n; ++j)
                printf("%5d", map[i][j]);
            printf("\n");
        }
        */
        sum[0][0] = map[0][0];
        for (int i = 1; i <= 2 * n; i++)
            sum[0][i] = sum[0][i - 1] + map[0][i];
        for (int i = 1; i <= 2 * n; i++)
        {
            sum[i][0] = sum[i - 1][0] + map[i][0];
            for (int j = 1; j <= 2 * n; j++)
                sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + map[i][j];
        }
        /*
        for (int i = 0; i <= 2 * n; ++i)
        {
            for (int j = 0; j <= 2 * n; ++j)
                printf("%5d", sum[i][j]);
            printf("\n");
        }
        */
        scanf("%d", &q);
        while (q--)
        {
            scanf("%d%d%d%d", &lx, &ly, &rx, &ry);
            long long ans = query(rx, ry) - query(lx - 1, ry) - query(rx, ly - 1) + query(lx - 1, ly - 1);
            printf("%lld\n", ans);
        }

    }
}