Problem E. Matrix from ArraysTime Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1372 Accepted Submission(s): 623 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; 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.
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 | We have carefully selected several similar problems for you: 6373 6372 6371 6370 6369 |
#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<queue>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<vector>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define mod 1e9+7
#define ll unsigned long long
#define MAX 1000000000
#define ms memset
#define maxn 50
using namespace std;
int l,q,m;
int x1,y1,x2,y2;
int tb[maxn];
int mp[maxn][maxn],ub;
ll ans=0;
/*
题目大意:给定矩阵的生成方式,
然后有q个查询,求其查询的子矩阵的值和。
先打表找到循环节,(看 数字观察),
是以2l为周期。
容斥定理,对于一个xy轴坐标值,
在图纸上画图不难观察到,其中的求值规律,
附加简单的容斥定理即可。
而对于单个的求二维前缀和,
数学规律见封装的函数代码。
*/
ll compute(ll x,ll y)
{
ll ret=0;
ll tx=x/ub,ty=y/ub;
ret+=mp[x%ub][y%ub];
ret+=(tx)*mp[ub][y%ub];
ret+=(ty)*mp[x%ub][ub];
ret+=tx*ty*mp[ub][ub];
return ret;
}
int main()
{
int t;scanf("%d",&t);
while(t--)
{
scanf("%d",&l);
for(int i=0;i<l;i++) scanf("%d",&tb[i]);
///ub=((l&1)?l:2*l);
ub=2*l;
memset(mp,0,sizeof(mp));
for(int i=0;i<ub;i++)
for(int j=0;j<ub;j++)
{
int t=i+j;
t=t*(t+1)/2+i;
t%=l;
mp[i+1][j+1]=tb[t];
}
for(int i=1;i<=ub;i++)
for(int j=1;j<=ub;j++)
{
mp[i][j]+=(mp[i-1][j]+mp[i][j-1]);
mp[i][j]-=mp[i-1][j-1];
}
/*
for(int i=1;i<=ub;i++)
{
for(int j=1;j<=ub;j++) cout<<mp[i][j]<<" ";
puts("");
}*/
///cout<<compute(8,5)<<endl;///<<" "<<compute(3,1)*3<<" "<<compute(1,3)*3<<" "<<9*compute(3,3)<<endl;
scanf("%d",&m);
for(int i=0;i<m;i++)
{
ans=0;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x2++,y2++;
ans+=compute(x2,y2);
ans+=compute(x1,y1);
ans-=compute(x2,y1);
ans-=compute(x1,y2);
printf("%lld\n",ans);
}
}
return 0;
}