HDU 6336 Matrix from Arrays
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
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.
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)$.
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
101
1068
2238
33076541
#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的更多相关文章
- HDU 6336 Matrix from Arrays (杭电多校4E)
遇事不决先打表. 然后会发现(个屁)大的矩形是由一个2L*2L的矩形重复出现组成的然后我们就可以这个矩形分成四个点到(0, 0)点的矩形,这样问题就变成了求四个到顶点(0, 0)的矩形的面积,然后就先 ...
- HDU 6336.Problem E. Matrix from Arrays-子矩阵求和+规律+二维前缀和 (2018 Multi-University Training Contest 4 1005)
6336.Problem E. Matrix from Arrays 不想解释了,直接官方题解: 队友写了博客,我是水的他的代码 ------>HDU 6336 子矩阵求和 至于为什么是4倍的, ...
- hdu多校第4场E. Matrix from Arrays HDU 二维前缀和
Problem E. Matrix from Arrays Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total S ...
- 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 ...
- 杭电多校第四场 E Matrix from Arrays
Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 ...
- HDU 6336 子矩阵求和
Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 ...
- HDU 4920 Matrix multiplication(bitset)
HDU 4920 Matrix multiplication 题目链接 题意:给定两个矩阵,求这两个矩阵相乘mod 3 思路:没什么好的想法,就把0的位置不考虑.结果就过了.然后看了官方题解,上面是用 ...
- HDU 2686 Matrix 3376 Matrix Again(费用流)
HDU 2686 Matrix 题目链接 3376 Matrix Again 题目链接 题意:这两题是一样的,仅仅是数据范围不一样,都是一个矩阵,从左上角走到右下角在从右下角走到左上角能得到最大价值 ...
- 杭电第四场 hdu6336 Problem E. Matrix from Arrays 打表找规律 矩阵前缀和(模板)
Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 ...
随机推荐
- 【BZOJ1975】【SDOI2010】魔法猪学院 [A*搜索]
魔法猪学院 Time Limit: 10 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description iPig在假期来到了传说中的魔法猪 ...
- {转}用ADMM求解大型机器学习问题
[本文链接:http://www.cnblogs.com/breezedeus/p/3496819.html] 从等式约束的最小化问题说起: ...
- TensorFlow 模型保存和导入、加载
在TensorFlow中,保存模型与加载模型所用到的是tf.train.Saver()这个类.我们一般的想法就是,保存模型之后,在另外的文件中重新将模型导入,我可以利用模型中的operation和va ...
- shell 文件内容替换 sed用法
sed 's/要被替换的字符串/新的字符串/g' sed 's/test/mytest/g' example-----在整行范围内把test替换为mytest. 如果没有g标记,则只有每行第一个匹配的 ...
- JS阶段测试
JS阶段测试 一.选择题 1.表单中的数据要提交到的处理文件由表单的( c )属性指定. A. method B. name C. action D. 以上都不对 2.在CSS样式 ...
- python基础===python os.path模块
os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...
- C中级 MariaDB Connector/C API 编程教程
引言 - 环境搭建 首先开始环境搭建. 主要在Window 10 + Visual Studio 2015 上构建使用 mariadb connector/c api 进行数据操作开发. 为什么选择在 ...
- servlet为什么要配置web.xml
(1).为Servlet命名: <servlet> <servlet-name>servlet1</servlet-name> <- 这是用于,在serv ...
- 简述MapReduce计算框架原理
1. MapReduce基本编程模型和框架 1.1 MapReduce抽象模型 大数据计算的核心思想是:分而治之.如下图所示.把大量的数据划分开来,分配给各个子任务来完成.再将结果合并到一起输出.注: ...
- textarea在浏览器中固定大小
HTML 标签 textarea 在大部分浏览器中只要指定行(rows)和列(cols)属性,就可以规定 textarea 的尺寸,大小就不会改变,不过更好的办法是使用 CSS 的 height 和 ...