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
 
Recommend
chendu
 
分析:就是个矩阵前缀和处理,水题~
#include <iostream>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#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[],M[][],L,t;
void init(){
cin>>L;
range(i,,L-)scanf("%lld",A+i);
LL cursor = ;
range(i,,L*-)
range(j,,i){
if(j<(L<<) and i-j<(L<<))M[j][i - j] = A[cursor];
cursor = (cursor + ) % L;
}
L<<=;
}
LL work(int n,int m){
if(n< or m<)return ;
return M[L-][L-]*(n/L)*(m/L)+M[n%L][L-]*(m/L)+M[L-][m%L]*(n/L)+M[n%L][m%L];
}
void solve(){
range(i,,L-)
range(j,,L-){
if(i)M[i][j]+=M[i-][j];
if(j)M[i][j]+=M[i][j-];
if(i and j)M[i][j]-=M[i-][j-];
}
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-,y2)-work(x2,y1-)+work(x1-,y1-));
}
}
int main() {
cin>>t;
while(t--) {
init();
solve();
}
return ;
}

HDU 6336 Matrix from Arrays的更多相关文章

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

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

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

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

  3. hdu多校第4场E. Matrix from Arrays HDU 二维前缀和

    Problem E. Matrix from Arrays Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total S ...

  4. 2018 Multi-University Training Contest 4 Problem E. Matrix from Arrays 【打表+二维前缀和】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6336 Problem E. Matrix from Arrays Time Limit: 4000/20 ...

  5. 杭电多校第四场 E Matrix from Arrays

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

  6. HDU 6336 子矩阵求和

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

  7. HDU 4920 Matrix multiplication(bitset)

    HDU 4920 Matrix multiplication 题目链接 题意:给定两个矩阵,求这两个矩阵相乘mod 3 思路:没什么好的想法,就把0的位置不考虑.结果就过了.然后看了官方题解,上面是用 ...

  8. HDU 2686 Matrix 3376 Matrix Again(费用流)

    HDU 2686 Matrix 题目链接 3376 Matrix Again 题目链接 题意:这两题是一样的,仅仅是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值 ...

  9. 杭电第四场 hdu6336 Problem E. Matrix from Arrays 打表找规律 矩阵前缀和(模板)

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

随机推荐

  1. bzoj1862/1056: [Zjoi2006]GameZ游戏排名系统

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1862 http://www.lydsy.com/JudgeOnline/problem.ph ...

  2. 【51NOD-0】1018 排序

    [算法]排序 #include<cstdio> #include<algorithm> using namespace std; ]; int main() { scanf(& ...

  3. 使用JQGrid 问题汇总 不定时更新

    jqgrid左下角的复杂搜索框显示为下拉框样式searchoptions: { value: ": 全部; 1: 在用; 2: 报废", sopt: ['eq'] } jqgrid ...

  4. Eclipse中SVN更改连接用户

    Eclipse中安装了SVN插件,当连接到SVN服务器后,便无法从客户端更改连接帐号 百度一下,也就知道 查看Eclipse中使用的是什么SVN Interface,位置在windows > p ...

  5. shell下在while循环中使用ssh命令的问题

    1 现象描述 最近使用ssh批量执行命令(已经做了密钥互信了),脚本读取配置文件中的主机列表(内容为每行一台主机IP地址),然后执行,可是每次只是执行第一台,就退出循环了. 2 排查思路 由于脚本比较 ...

  6. HTML 知识点总结

    HTML基本语法 HTML标签 单标签 <标签名>或<标签名 /> 双标签 <标签名>内容</标签名> 跟标签也叫元素(根元素) 属性 属性属于标签 一 ...

  7. 数据库简述(以MySQL为例)

    一.数据库中的概念 1.数据库是用户存放数据.访问数据.操作数据的存储仓库,用户的各种数据被有组织地存放在数据库中.可以随时被有权限的用户查询.统计.添加.删除和修改.可以说,数据库是长期存储在计算机 ...

  8. linux命令(9):route命令

    查看路由表:route –n //添加到主机的路由 # route add –host 192.168.168.110 dev eth0 # route add –host 192.168.168.1 ...

  9. HDU 3480 Division(斜率DP裸题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3480 题目大意:将n个数字分成m段,每段价值为(该段最大值-该段最小值)^2,求最小的总价值. 解题思 ...

  10. 利用getBoundingClientRect()来实现div容器滚动固定

    ele.getBoundingClientRect()的方法是可以获得一个元素在整个视图窗口的位置 可以return的值有width,height,top,left,x,y,right,bottom ...