HDU6336 Problem E. Matrix from Arrays

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

分析:这道题题目很容易理解,一开始的时候打了一个3和一个5的表出来,发现每次以l为循环,然后就直接敲了一发,一交挖了,然后发现没有用long long,修改成long long在交,还是挖了,然后从新敲了一个4的表,发现偶数的时候是以2l为循环节,然后就把l改成了2l就过了。比赛的时候为了方便,没有使用前缀和,直接复制粘贴的方法把所有的情况都敲出来,导致代码非常长。

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn = 1000;
typedef long long ll;
ll t, n, m, l;
ll M[maxn][maxn];
ll a[maxn];
ll x0, y0, x1, y1;
ll sum;

int main(){
    scanf("%lld", &t);
    while(t--){
        scanf("%lld", &l);
        for(ll i=0; i<l; i++){
            scanf("%lld", &a[i]);
        }
        sum = 0;
        ll cursor = 0;
        for (ll i = 0; ; ++i) {
            for (ll j = 0; j <= i; ++j) { 
                M[j][i - j] = a[cursor];
                cursor = (cursor + 1) % l;
                
            }
            if(i == 4*l) break;
        }
        l = 2*l;
        for(ll i=0; i<l; i++){
            for(ll j=0; j<l; j++) sum+= M[i][j];
        }
        scanf("%lld", &m);
        while(m--){
            scanf("%lld%lld%lld%lld", &x0, &y0, &x1, &y1);
            if(x0 > x1) swap(x0, x1);
            if(y0 > y1) swap(y0, y1);
//            x0 -= 1; y0 -= 1;
//            x1 -= 1; y1 -= 1;
            ll ans = 0;
            if((x1/l) > (x0/l+1)){
                if(y0/l ==  y1/l){
                    ll xx0 = x0%l;
                    ll yy0 = y0%l;
                    ll xx1 = x1%l;
                    ll yy1 = y1%l;
                    for(ll i=xx0; i<l ;i++){
                        for(ll j=yy0; j<=yy1; j++){
                            ans += M[i][j];
                        }
                    }
                    for(ll i=0; i<=xx1; i++){
                        for(ll j=yy0; j<=yy1; j++){
                            ans += M[i][j];
                        }
                    }
                    ll cnt = x1/l - x0/l - 1;
                    ll temp = 0;
                    for(ll i=0; i<l; i++){
                        for(ll j=yy0; j<=yy1; j++){
                            temp += M[i][j];
                        }
                    }
                    ans += temp*cnt;
                }
                else if((y0/l+1) == y1/l){
                    ll xx0 = x0%l;
                    ll yy0 = y0%l;
                    ll xx1 = x1%l;
                    ll yy1 = y1%l;
                    for(ll i=xx0; i<l; i++){
                        for(ll j=yy0; j<l; j++){
                            ans += M[i][j];
                        }
                    }
                    for(ll i=xx0; i<l; i++){
                        for(ll j=0; j<=yy1; j++){
                            ans += M[i][j];
                        }
                    }
                    for(ll i=0; i<=xx1; i++){
                        for(ll j=yy0; j<l; j++){
                            ans += M[i][j];
                        }
                    }
                    for(ll i=0; i<=xx1; i++){
                        for(ll j=0; j<=yy1; j++){
                            ans += M[i][j];
                        }
                    }
                    ll cnt = x1/l - x0/l - 1;
                    ll temp1 = 0, temp2 = 0;
                    for(ll i=0; i<l; i++){
                        for(ll j=yy0; j<l; j++){
                            temp1 += M[i][j];
                        }
                    }
                    for(ll i=0; i<l ;i++){
                        for(ll j=0; j<=yy1; j++){
                            temp2 += M[i][j];
                        }
                    }
                    ans += temp1*cnt + temp2*cnt;
                }
                else {
                    ll xx0 = x0%l;
                    ll yy0 = y0%l;
                    ll xx1 = x1%l;
                    ll yy1 = y1%l;
                    for(ll i=xx0; i<l; i++){
                        for(ll j=yy0; j<l; j++){
                            ans += M[i][j];
                        }
                    }
                    for(ll i=xx0; i<l; i++){
                        for(ll j=0; j<=yy1; j++){
                            ans += M[i][j];
                        }
                    }
                    for(ll i=0; i<=xx1; i++){
                        for(ll j=yy0; j<l; j++){
                            ans += M[i][j];
                        }
                    }
                    for(ll i=0; i<=xx1; i++){
                        for(ll j=0; j<=yy1; j++){
                            ans += M[i][j];
                        }
                    }
                    ll cnt1 = y1/l - y0/l-1;
                    ll temp1 = 0, temp2 = 0;
                    for(ll i=xx0; i<l; i++){
                        for(ll j=0; j<l; j++){
                            temp1 += M[i][j];
                        }
                    }
                    for(ll i=0; i<=xx1; i++){
                        for(ll j=0; j<l ;j++){
                            temp2 += M[i][j];
                        }
                    }
                    ans += temp1*cnt1 + temp2*cnt1;
                    ll cnt2 = x1/l - x0/l - 1;
                    ll temp3 = 0, temp4 = 0;
                    for(ll i=0; i<l; i++){
                        for(ll j=yy0; j<l; j++){
                            temp3 += M[i][j];
                        }
                    }
                    for(ll i=0; i<l ;i++){
                        for(ll j=0; j<=yy1; j++){
                            temp4 += M[i][j];
                        }
                    }
                    ans += temp3*cnt2 + temp4*cnt2;
                    ans += sum*cnt1*cnt2;
                }
            }
            else if(x1/l > x0/l){
                if(y0/l ==  y1/l){
                    ll xx0 = x0%l;
                    ll yy0 = y0%l;
                    ll xx1 = x1%l;
                    ll yy1 = y1%l;
                    for(ll i=xx0; i<l ;i++){
                        for(ll j=yy0; j<=yy1; j++){
                            ans += M[i][j];
                        }
                    }
                    for(ll i=0; i<=xx1; i++){
                        for(ll j=yy0; j<=yy1; j++){
                            ans += M[i][j];
                        }
                    }
                }
                else if((y0/l+1) == y1/l){
                    ll xx0 = x0%l;
                    ll yy0 = y0%l;
                    ll xx1 = x1%l;
                    ll yy1 = y1%l;
                    for(ll i=xx0; i<l; i++){
                        for(ll j=yy0; j<l; j++){
                            ans += M[i][j];
                        }
                    }
                    for(ll i=xx0; i<l; i++){
                        for(ll j=0; j<=yy1; j++){
                            ans += M[i][j];
                        }
                    }
                    for(ll i=0; i<=xx1; i++){
                        for(ll j=yy0; j<l; j++){
                            ans += M[i][j];
                        }
                    }
                    for(ll i=0; i<=xx1; i++){
                        for(ll j=0; j<=yy1; j++){
                            ans += M[i][j];
                        }
                    }
                }
                else {
                    ll xx0 = x0%l;
                    ll yy0 = y0%l;
                    ll xx1 = x1%l;
                    ll yy1 = y1%l;
                    for(ll i=xx0; i<l; i++){
                        for(ll j=yy0; j<l; j++){
                            ans += M[i][j];
                        }
                    }
                    for(ll i=xx0; i<l; i++){
                        for(ll j=0; j<=yy1; j++){
                            ans += M[i][j];
                        }
                    }
                    for(ll i=0; i<=xx1; i++){
                        for(ll j=yy0; j<l; j++){
                            ans += M[i][j];
                        }
                    }
                    for(ll i=0; i<=xx1; i++){
                        for(ll j=0; j<=yy1; j++){
                            ans += M[i][j];
                        }
                    }
                    ll cnt = y1/l - y0/l-1;
                    ll temp1 = 0, temp2 = 0;
                    for(ll i=xx0; i<l; i++){
                        for(ll j=0; j<l; j++){
                            temp1 += M[i][j];
                        }
                    }
                    for(ll i=0; i<=xx1; i++){
                        for(ll j=0; j<l ;j++){
                            temp2 += M[i][j];
                        }
                    }
                    ans += temp1*cnt + temp2*cnt;
                }
            }
            else if(x1/l == x0/l){
                if(y0/l ==  y1/l){
                    ll xx0 = x0%l;
                    ll yy0 = y0%l;
                    ll xx1 = x1%l;
                    ll yy1 = y1%l;
                    for(ll i=xx0; i<=xx1; i++){
                        for(ll j=yy0; j<=yy1; j++) 
                            ans+= M[i][j];
                    }
                }
                else if((y0/l+1) == y1/l){
                    ll xx0 = x0%l;
                    ll yy0 = y0%l;
                    ll xx1 = x1%l;
                    ll yy1 = y1%l;
                    for(ll i=xx0; i<=xx1; i++){
                        for(ll j=yy0; j<l; j++){
                            ans+= M[i][j];
                        }
                    }
                    for(ll i=xx0; i<=xx1; i++){
                        for(ll j=0; j<=yy1; j++){
                            ans += M[i][j];
                        }
                    }
                }
                else {
                    ll cnt = y1/l - y0/l - 1;
                    ll xx0 = x0%l;
                    ll yy0 = y0%l;
                    ll xx1 = x1%l;
                    ll yy1 = y1%l;
                    for(ll i=xx0; i<=xx1; i++){
                        for(ll j=yy0; j<l; j++){
                            ans+= M[i][j];
                        }
                    }
                    for(ll i=xx0; i<=xx1; i++){
                        for(ll j=0; j<=yy1; j++){
                            ans += M[i][j];
                        }
                    }
                    ll temp = 0;
                    for(ll i=xx0; i<=xx1; i++){
                        for(ll j=0; j<l ;j++){
                            temp += M[i][j];
                        }
                    }
                    ans += temp*cnt;
                }
            }
            printf("%lld\n", ans);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/m0_37611893/article/details/81348829