HDU6336-2018ACM暑假多校联合训练4-1005-Problem E. Matrix from Arrays-前缀和

题意是给了一种矩阵的生成方式

让你求两个左边之间的矩阵里面的数加起来的和(不是求矩阵的值)

没看标程之前硬撸写了160行

用了前缀和以后代码量缩短到原来的1/3

根据规律可以推导出这个矩阵是在不断重复一个2L*2L的矩阵

求值时,先求出原点到(x2,y2)的值,再减去原点到(x1-1,y2)和原点到(x2,y1-1)的值,再加上被重复减去的原点到(x1-1,y1-1)的值

Problem E. Matrix from Arrays

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1060    Accepted Submission(s): 478

Problem Description
Kazari has an array A length of L, she plans to generate an infinite matrix M using A.
The procedure is given below in C/C++:

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;
}
}

Her friends don't believe that she has the ability to generate such a huge matrix, so they come up with a lot of queries about M, each of which focus the sum over some sub matrix. Kazari hates to spend time on these boring queries. She asks you, an excellent coder, to help her solve these queries.

 
Input
The first line of the input contains an integer T (1≤T≤100) denoting the number of test cases.
Each test case starts with an integer L (1≤L≤10) denoting the length of A.
The second line contains L integers A0,A1,...,AL−1 (1≤Ai≤100).
The third line contains an integer Q (1≤Q≤100) denoting the number of queries.
Each of next Q lines consists of four integers x0,y0,x1,y1 (0≤x0≤x1≤108,0≤y0≤y1≤108) querying the sum over the sub matrix whose upper-leftmost cell is (x0,y0) and lower-rightest cell is (x1,y1).
 
Output
For each test case, print an integer representing the sum over the specific sub matrix for each query.
 
Sample Input
1
3
1 10 100
5
3 3 3 3
2 3 3 3
2 3 5 8
5 1 10 10
9 99 999 1000
 
Sample Output
1
101
1068
2238
33076541
 
 #include <iostream>

 using namespace std;

 int l;
int M[][];
int A[]; long long _plus(int x, int y)
{
if (x < || y < )
return ;
return
(
M[l - ][l - ] * 1LL * (x / l) * (y / l) + \
M[x % l][l - ] * 1LL * (y / l) + M[l - ][y % l] * 1LL * (x / l) + \
M[x % l][y % l] * 1LL
);
} int main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--)
{
cin >> l;
for (int i = ; i < l; i++)
cin >> A[i];
int cursor = ;
for (int i = ; i < * l; i++)
for (int j = ; j <= i; j++)
{
M[j][i - j] = A[cursor];
cursor = (cursor + ) % l;
}
l *= ;
for(int i = ; i < l; i++)
for (int j = ; j < l; j++)
{
if (i)
M[i][j] += M[i - ][j];
if (j)
M[i][j] += M[i][j - ];
if (i && j)
M[i][j] -= M[i - ][j - ];
} int n;
cin >> n;
while (n--)
{
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
cout << _plus(x2, y2) - _plus(x1 - , y2) - _plus(x2, y1 - ) + _plus(x1 - , y1 - ) << endl;
}
}
return ;
}
上一篇:HDU6400-2018ACM暑假多校联合训练1004-Parentheses Matrix-构造


下一篇:HDU6333-2018ACM暑假多校联合训练1002-Harvest of Apples-莫队+费马小定理