HDU 6336 Matrix from Arrays

Problem E. Matrix from Arrays

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

 

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 \le T \le 100)$ denoting the number of test cases.
Each test case starts with an integer $L$ $(1 \le L \le 10)$ denoting the length of $A$.
The second line contains $L$ integers $A_0, A_1, ..., A_{L - 1}$ $(1 \le A_i \le 100)$.
The third line contains an integer $Q$ $(1 \le Q \le 100)$ denoting the number of queries.
Each of next $Q$ lines consists of four integers $x_0, y_0, x_1, y_1$ $(0 \le x_0 \le x_1 \le 10 ^ 8, 0 \le y_0 \le y_1 \le 10 ^ 8)$ querying the sum over the sub matrix whose upper-leftmost cell is $(x_0, y_0)$ and lower-rightest cell is $(x_1, y_1)$.
 

 

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
 

 

Source
2018 Multi-University Training Contest 4
 

 

Recommend
chendu
 
分析:就是个矩阵前缀和处理,水题~
#include 
#include <string>
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define LL long long
#define elif else if
#define range(i,a,b) for(auto i=a;i<=b;++i)
#define rerange(i,a,b) for(auto i=a;i>=b;--i)
#define itrange(i,a,b) for(auto i=a;i!=b;++i)
#define fill(arr,tmp) memset(arr,tmp,sizeof(arr))
using namespace std;
LL A[10],M[400][400],L,t;
void init(){
    cin>>L;
    range(i,0,L-1)scanf("%lld",A+i);
    LL cursor = 0;
    range(i,0,L*4-1)
        range(j,0,i){
            if(j<(L<<1) and i-j<(L<<1))M[j][i - j] = A[cursor];
            cursor = (cursor + 1) % L;
        }
    L<<=1;
}
LL work(int n,int m){
    if(n<0 or m<0)return 0;
    return M[L-1][L-1]*(n/L)*(m/L)+M[n%L][L-1]*(m/L)+M[L-1][m%L]*(n/L)+M[n%L][m%L];
}
void solve(){
    range(i,0,L-1)
    range(j,0,L-1){
        if(i)M[i][j]+=M[i-1][j];
        if(j)M[i][j]+=M[i][j-1];
        if(i and j)M[i][j]-=M[i-1][j-1];
    }
    int Q,x1,x2,y1,y2;
    scanf("%d",&Q);
    while(Q--){
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        printf("%lld\n",work(x2,y2)-work(x1-1,y2)-work(x2,y1-1)+work(x1-1,y1-1));
    }
}
int main() {
    cin>>t;
    while(t--) {
        init();
        solve();
    }
    return 0;
}
View Code

 

你可能感兴趣的:(HDU 6336 Matrix from Arrays)