题意:

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. php+mysqli实现批量执行插入、更新及删除数据的方法

    本文实例讲述了php+mysqli实现批量执行插入.更新及删除数据的方法.分享给大家供大家参考.具体如下: mysqli批量执行插入/更新/删除数据,函数为 multi_query(). 下面的代码只 ...

  2. Swift-8-枚举

    // Playground - noun: a place where people can play import UIKit // 枚举语法 enum SomeEnumeration { // e ...

  3. C++ 函数模板一(函数模板定义)

    //函数模板定义--数据类型做参数 #include<iostream> using namespace std; /* 函数模板声明 1.函数模板定义由模板说明和函数定义组成,并且一个模 ...

  4. canvas图片base64编码后,ajax提交到后台

    如题,直接上JS var imgData = canvas.toDataURL("image/png"); var data = imgData.substr(22); $.pos ...

  5. cobbler setting dnsmasq

    Currently cobbler can help generate a ISC DHCP configuration (package name: dhcpd) it can also alter ...

  6. 客户端用JavaScript填充DropDownList控件 服务器端读不到值

    填充没有任何问题,但是在服务器端却取不出来下拉表中的内容.页面代码如下. <form id="form1" runat="server"> < ...

  7. 【ask】ghost分区还原win7出现蓝屏,试图加载CLASSPNP驱动时出现

    1.最近由于需要把整个c盘备份了,然后还原在另一台机器上. 2.结果当然时出事了,开始一切如旧,但是机器重启后,蓝屏了. 3.哦,开始立马觉得可能时因为有些软件的驱动是装载d盘了. 4.赶紧把d盘也备 ...

  8. IOS-应用之间调用

    本文转载至 http://blog.csdn.net/likendsl/article/details/7553605 原文作者的博客值得关注. ios应用之间的调用步骤: 一.调用自己开发的应用 1 ...

  9. c++调用python函数时,使用PyArray_SimpleNewFromData(nd, dims, typenum, data)函数时出现内存错误的问题

    示例程序: int main(int argc, char *argv[]){ PyObject *pName, *pModule, *pDict, *pFunc, *pValue, *pArgs,* ...

  10. Software Defined Networking For Dummies, Cisco Special Edition

    从接触SDN开始,不论是硬件还是软件,一直都是从具体的点开始,慢慢的勾勒出自己认为的SDN的样子,相信读完这本思科出的关于SDN的书会对其有新的认识 这本书的名字就是Software Defined ...