Problem E. Matrix from Arrays

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

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
 
Source
 

  比赛的时候其实知道做法了,打表后可以发现这个矩阵的行列都是由循环节的而且行列的循环长度一致,

只不过一开始默认为是L,后来发现奇数是L,偶数是2*L,我们都按照2*L来做就好了,预处理出来行和列的

前缀和然后容斥的处理询问。

  

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define mp make_pair
#define LL long long
int cursor = ;
int M[][],A[],L,len;
LL pre1[][],pre2[][];
LL g[];
LL sum(int x,int y){
if(x<=||y<=) return ;
LL ans=;
for(int i=;i<=len;++i){
g[i]=g[i-]+pre1[i][len]*(y/len)+pre1[i][y%len];
}
return g[len]*(x/len)+g[x%len];
}
int main(){
int t,n,m,i,j,k,Q;
int x1,y1,x2,y2;
cin>>t;
while(t--){
scanf("%d",&L);
len=L*;
memset(pre1,,sizeof(pre1));
memset(pre2,,sizeof(pre2));
for(i=;i<L;++i) scanf("%d",A+i);
int cursor = ;
for (int i = ;i<=; ++i) {
for (int j = ; j <= i; ++j) {
M[j+][i - j+] = A[cursor];
cursor = (cursor + ) % L;
}
}
for(i=;i<=len;++i){
for(j=;j<=len;++j){
pre1[i][j]=M[i][j]+pre1[i][j-];
pre2[i][j]=M[j][i]+pre2[i][j-];
}
}
scanf("%d",&Q);
while(Q--){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1++,y1++,x2++,y2++;
printf("%lld\n",sum(x1-,y1-)+sum(x2,y2)-sum(x1-,y2)-sum(x2,y1-));
}
}
return ;
}

HDU-6336-构造的更多相关文章

  1. HDU 6336.Problem E. Matrix from Arrays-子矩阵求和+规律+二维前缀和 (2018 Multi-University Training Contest 4 1005)

    6336.Problem E. Matrix from Arrays 不想解释了,直接官方题解: 队友写了博客,我是水的他的代码 ------>HDU 6336 子矩阵求和 至于为什么是4倍的, ...

  2. HDU 5667 构造矩阵快速幂

    HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...

  3. HDU - 6150 构造题

    最近的vj好垃圾,老崩,实名吐槽 HDU - 6150 题意:给出一个错误的求最小点覆盖的函数,需要来构造一组样例,使得那个函数跑出来的答案是正解的3倍以上. 很巧妙的构造技巧,首先想法就是弄一个二分 ...

  4. Number Sequence(HDU 1005 构造矩阵 )

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  5. HDU 6336 (规律 + 二维矩阵的前缀和妙用)

    题目 给出长度为n 的A矩阵 , 按 int cursor = 0; for (int i = 0; ; ++i) { for (int j = 0; j <= i; ++j) { M[j][i ...

  6. hdu 3879 hdu 3917 构造最大权闭合图 俩经典题

    hdu3879  base station : 各一个无向图,点的权是负的,边的权是正的.自己建一个子图,使得获利最大. 一看,就感觉按最大密度子图的构想:选了边那么连接的俩端点必需选,于是就以边做点 ...

  7. hdu 5444(构造二叉树然后遍历)

    Elven Postman Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  8. A Simple Math Problem(HDU 1757 构造矩阵)

    If x < 10 f(x) = x.If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-1 ...

  9. HDU 6336 Matrix from Arrays (杭电多校4E)

    遇事不决先打表. 然后会发现(个屁)大的矩形是由一个2L*2L的矩形重复出现组成的然后我们就可以这个矩形分成四个点到(0, 0)点的矩形,这样问题就变成了求四个到顶点(0, 0)的矩形的面积,然后就先 ...

  10. HDU 6336 Matrix from Arrays

    Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 ...

随机推荐

  1. Facebook ads_Business Manager

    xmind 下载链接

  2. 洛谷P2362 围栏木桩----dp思路

    在翻dp水题的时候找到的有趣的题0v0 原文>>https://www.luogu.org/problem/show?pid=2362<< 题目描述 某农场有一个由按编号排列的 ...

  3. js捕获错误

    文: http://www.jb51.net/article/78764.htm 用window.onerror捕获并上报Js错误的方法 前两天有个2048游戏的用户反馈说,打开游戏后不能玩儿,只有一 ...

  4. 中文字符串和UTF-8编码字符串相互转换

    中文字符串和UTF-8编码字符串相互转换 //UTF字符转换 var UTFTranslate = { Change: function(pValue) { ) { ).replace(/(%u)(\ ...

  5. 更新:在MAC上安装RN开发环境的步骤(全)

    总共分为三部: 1:按照官网(中文)上的步骤去安装jdk和android studio 2:配置SDK 3:安装虚拟机和模拟器 所以这里提出的是注意事项: 1:~/.bash_profile 文件里面 ...

  6. mac终端不好用?用brew神器代替

    一.概念 Brew是一款Mac OS平台下的软件包管理工具,拥有安装.卸载.更新.查看.搜索等很多实用的功能.简单的一条指令,就可以实现包管理,而不用你关心各种依赖和文件路径的情况,十分方便快捷. 官 ...

  7. ssh连接服务器

    1.命令行操作 第一步输入  :ssh 用户名@服务器外网ip 第二步:输入密码,回车 看到welcome提示信息即为登陆成功 输入:exit 退出 2.客户端操作 windows下载ssh软件,安装 ...

  8. ZZNU 正约数之和

    #include<stdio.h> #include<string.h> #include<math.h> #include<time.h> #incl ...

  9. 力扣(LeetCode)389. 找不同

    给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. 示例: 输入: s = "abcd&quo ...

  10. 在Windows下解决git ERROR: Permission to XXX.git denied to user

    这种情况一般都是由于登陆了不同的git仓库在本地记录了凭证导致的,比如登陆了两个不同的github账号. 1.控制面板 2.删除凭证再重新提交将会重新输入用户名和密码 以上.