题意:

for (int i = 0; ; ++i) {
for (int j = 0; j <= i; ++j) {
M[j][i - j] = A[cursor];
cursor = (cursor + 1) % L;
}
}

给定序列A[1..L],二维数组M的规律由以上代码给出。Q个查询,每次给x0,y0,x1,y1 (0≤x0≤x1≤1e8,0≤y0≤y1≤1e8)四个数,求以(x0,y0)和(x1,y1)两个点为端点的矩形中数的和。

分析:根据推导可得,M[i][j] = M[i+2L][j] + M[i][j+2L]。当L为奇数时,每个L*L的块都是一个循环节;L为偶数时,每个2*L*2*L是一个循环节,可以打表验证。

预处理出M[i][j]一个循环节的的二维前缀和,复杂度O(L^2)。每次计算答案可以由前缀和相加减所得:S(x1,y1)  - S(x1,y0-1) - S(x0-1, y1) + S(x0-1,y0-1)。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 2e2+;
LL M[maxn][maxn];
int A[maxn];
int L; LL area(int x,int y){
LL block = M[L-][L-]; //一个块的和
LL res=block *(x/L)*(y/L);
res+=M[x%L][y%L];
res+=(x/L)*M[L-][y%L];
res+=(y/L)*M[x%L][L-];
return res;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T,N,Q,u,v,tmp,K;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
for(int i=;i<N;++i)
scanf("%d",&A[i]);
if(N&) L =N*;
else L = (N<<);
int cur = ;
for(int i=;i<L;++i){
for(int j=;j<=i;++j){
M[j][i-j] = A[cur];
cur = (cur+) %N;
}
}
L/=;
for(int i=;i<L;++i){
for(int j=;j<L;++j){
M[i][j] +=M[i][j-];
M[i][j] +=M[i-][j];
M[i][j] -=M[i-][j-];
}
}
scanf("%d",&Q);
while(Q--){
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
LL res = area(c,d)+area(a-,b-)-area(c,b-)-area(a-,d);
printf("%lld\n",res);
}
}
return ;
}

HDU - 6336 Problem E. Matrix from Arrays (规律+二维前缀和)的更多相关文章

  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. 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 ...

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

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

  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 ...

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

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

  6. [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  7. [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  8. Problem E. Matrix from Arrays(杭电2018年多校第四场+思维+打表找循环节)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6336 题目: 题意:给你一个l个元素的数组a,用题目中的程序构造一个新的矩阵,询问q次,问以(x1,y ...

  9. BZOJ4972 八月月赛 Problem B 小Q的方格纸 二维前缀和

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4972 八月月赛Problem B 题目概括 一个矩阵,一坨询问,问矩阵中一个特定方向的等腰直角三角 ...

随机推荐

  1. CI 框架购物车问题

    因为CI 是外国的框架.购物逻辑和中国的不一样.所以需要改进ci 框架的 cart 类: (1)先把 cart类拷贝一份到application/libaries/下 (2)因为cart中购车中的商品 ...

  2. Tensorflow之改变tensor形状

    https://www.tensorflow.org/versions/r0.12/api_docs/python/array_ops.html#reshape 例子: # tensor 't' is ...

  3. 嵌入式驱动开发之解码器tvp5150---tvp5150am1基于8148vpss的添加调试

    (1)i2c (2)注册设备 (3)寄存器 --------------author:pkf ------------------------time:2015-4-5 --------------- ...

  4. XV Open Cup named after E.V. Pankratiev Stage 6, Grand Prix of Japan Problem J. Hyperrectangle

    题目大意: 给出一个$d$维矩形,第i维的范围是$[0, l_i]$. 求满足$x_1 + x_2 + ...x_d \leq s$ 的点构成的单纯形体积. $d, l_i \leq 300$ 题解: ...

  5. scheme 中的宏使用

    #lang scheme ( define-syntax my-when     ( syntax-rules ()       [ ( _ pred body ... )         ( if ...

  6. SR领域文献资源汇总(链接地址)

    DRCN http://www.drcn.org/   The International Workshop on Design of Reliable Communication Networks ...

  7. Faster R-CNN利用新的网络结构来训练

    前言 最近利用Faster R-CNN训练数据,使用ZF模型,效果无法有效提高.就想尝试对ZF的网络结构进行改造,记录下具体操作. 一.更改网络,训练初始化模型 这里为了方便,我们假设更换的网络名为L ...

  8. servlet ; basepath ; sendredirected ;

    Eclipse 新建 jsp页面里自动生成以下代码: <%String path = request.getContextPath();String basePath = request.get ...

  9. grunt 相关知识

    /** * Created by lee on 2014.07.02 002. */ module.exports = function (grunt) { // 项目配置 grunt.initCon ...

  10. 机器被感染病毒文件zigw的处理流程

    1.现象 服务器CPU报警,查看时,已接近100%. 2.查找 使用top查看是哪个进程在占用CPU,此时zigw立刻出现,记录下进程的PID,假如为12345. (1) 如果在不知道程序的路径前,就 ...