码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 6336 子矩阵求和

时间:2018-08-03 19:44:55      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:represent   tom   other   矩阵   chmod   msu   php   col   The   

Problem E. Matrix from Arrays

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


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 (1T100) denoting the number of test cases.
Each test case starts with an integer L (1L10) denoting the length of A.
The second line contains L integers A0,A1,...,AL?1 (1Ai100).
The third line contains an integer Q (1Q100) denoting the number of queries.
Each of next Q lines consists of four integers x0,y0,x1,y1 (0x0x1108,0y0y1108) 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
 
Source

解析   矩阵是无限大的所以右下角没有被赋值的地方是不受影响的QAQ   长度为偶数循环矩阵是 2L*2L  所以直接按照2L算就是了  二位前缀和预处理一下  数一下完事了   

AC代码

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n");
#define debug(a,b) cout<<a<<" "<<b<<" ";
using namespace std;
typedef long long ll;
const ll maxn=1e2+10,inf=0x3f3f3f3f;
const ll mod=1e9+7;
ll a[maxn],g[maxn][maxn],n;
ll sum[maxn][maxn];
void init()
{
    int cnt=0;
    for(int i=0;i<4*n;++i)
    {
        for(int j=0;j<=i;++j) 
        {
            g[j][i-j]=a[cnt];
            cnt=(cnt+1)%n;
        }
        
    }
    sum[0][0]=g[0][0];
    for(int i=0;i<2*n;i++)
    {
        for(int j=0;j<2*n;j++)
        {
            if(i>0&&j>0)sum[i][j]=g[i][j]+sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
            if(i==0&&j>0)sum[i][j]=g[i][j]+sum[i][j-1];
            if(j==0&&i>0)sum[i][j]=g[i][j]+sum[i-1][j];
        }
    }
}
ll solve(int x,int y)
{
    ll ans=0;
    ll xx=x/n;
    ll yy=y/n;
    ll yux=x%n;
    ll yuy=y%n;
    ans+=xx*yy*sum[n-1][n-1];
    ans+=yy*sum[yux][n-1];
    ans+=xx*sum[n-1][yuy];
    ans+=sum[yux][yuy];
    return ans;
}
int main()
{
    int t,q;
    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=0;i<n;i++)
            cin>>a[i];
        init();n=n*2;
        cin>>q;
        while(q--)
        {
            int x1,x2,y1,y2;
            cin>>x1>>y1>>x2>>y2;
            //cout<<solve(x1,y1)<<endl;
            cout<<solve(x2,y2)-solve(x1-1,y2)-solve(x2,y1-1)+solve(x1-1,y1-1)<<endl;
        }
    }
    return 0;
}

 

HDU 6336 子矩阵求和

标签:represent   tom   other   矩阵   chmod   msu   php   col   The   

原文地址:https://www.cnblogs.com/stranger-/p/9415806.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!