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. OScached页面缓存的概念和安装

    一.基本概述 OSCache标记库由OpenSymphony设计,它是一种开创性的JSP定制标记应用,提供了在现有JSP页面之内实现快速内存缓冲的功能.OSCache是个一个广泛采用的高性能的J2EE ...

  2. Bzoj3481 DZY Loves Math III

    Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 310  Solved: 65 Description Input Output Sample Input ...

  3. 【BZOJ】1577: [Usaco2009 Feb]庙会捷运Fair Shuttle

    [题意]公车从1开到n,有k群牛想从一个点到达另一个点,公车最多乘坐c个人,牛群可以拆散,问最多载多少牛到达目的地. [算法]贪心+堆 [题解]线段和点的贪心,一般有按左端点排序和按右端点排序两种方法 ...

  4. 使用abp的 redis cache

    top 使用abp的 redis cache -1. 在微软维护的github项目的release里找到redis的windows版本 64位 大约5M,安装,安装,然后在安装目录找到redis.wi ...

  5. 深入浅出MyBatis:JDBC和MyBatis介绍

    JDBC相关概念 Java程序都是通过JDBC连接数据库的,通过SQL对数据库编程,JDBC是由SUN公司提出的一些列规范,只定义了接口规范,具体实现由各个数据库厂商去实现,它是一种典型的桥接模式. ...

  6. 小程序_RSA加密功能

    这是开发的第三个小程序,基于一个物流系统,简化功能开发下单流程.登录的时候,系统是使用RSA进行加解密的. 流程:第一个接口获取到后端传过来的密匙共钥(publicKey),通过公钥使用RSA加密密码 ...

  7. python基础===函数的几个要点

    函数 可接受任意数量参数的函数 位置参数   和  关键字参数 为了能让一个函数接受任意数量的位置参数,可以使用一个*参数. def avg(first, *r): return (first + s ...

  8. udpserver.pl 和 udpclient.pl

    udpserver.pl #!use/bin/perl -w use Socket; #导入Socket库 ,INADDR_ANY);#压入sockaddr_in模式,利用了全局当地压缩地点INADD ...

  9. MongoDB的安装配置、基本操作及Perl操作MongoDB

    MongoDB的安装配置.基本操作及Perl操作MongoDB http://www.myhack58.com/Article/60/63/2014/42353.htm

  10. 2017多校第7场 HDU 6129 Just do it 找规律

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6129 题意:求a序列后m次xor前缀和 解法: 手动对1位置对每个位置的贡献打表发现 第一次 贡献为 ...