HDU - 6336 Problem E. Matrix from Arrays (规律+二维前缀和)
题意:
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 (规律+二维前缀和)的更多相关文章
- HDU 6336.Problem E. Matrix from Arrays-子矩阵求和+规律+二维前缀和 (2018 Multi-University Training Contest 4 1005)
6336.Problem E. Matrix from Arrays 不想解释了,直接官方题解: 队友写了博客,我是水的他的代码 ------>HDU 6336 子矩阵求和 至于为什么是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 ...
- 杭电第四场 hdu6336 Problem E. Matrix from Arrays 打表找规律 矩阵前缀和(模板)
Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 ...
- hdu多校第4场E. Matrix from Arrays HDU 二维前缀和
Problem E. Matrix from Arrays Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total S ...
- HDU 6336 (规律 + 二维矩阵的前缀和妙用)
题目 给出长度为n 的A矩阵 , 按 int cursor = 0; for (int i = 0; ; ++i) { for (int j = 0; j <= i; ++j) { M[j][i ...
- [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 ...
- [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 ...
- Problem E. Matrix from Arrays(杭电2018年多校第四场+思维+打表找循环节)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6336 题目: 题意:给你一个l个元素的数组a,用题目中的程序构造一个新的矩阵,询问q次,问以(x1,y ...
- BZOJ4972 八月月赛 Problem B 小Q的方格纸 二维前缀和
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4972 八月月赛Problem B 题目概括 一个矩阵,一坨询问,问矩阵中一个特定方向的等腰直角三角 ...
随机推荐
- 第一百六十一节,封装库--JavaScript,完整封装库文件
封装库--JavaScript,完整封装库文件 /** *feng_zhuang_ku_1.0版本,js封装库,2016/12/29日:林贵秀 **/ /** 前台调用 * 每次调用$()创建库对象, ...
- Install EPEL repo on CentOS 7 / RHEL 7
On CentOS 7, we have found without downloading the epel-release RPM package(as we used to do on prev ...
- QT软件初次使用中遇到的若干问题及思考
1.QT窗口名称汉字设置问题? 答案:在MSVC中试用诸多方法,亲测都不行. 解决方案1:创建项目时,选择使用MSGW方案,用gcc进行编译,即可解决 2.QT中ui文件中组件不能再.cpp文件中显示 ...
- Eclipse中的build path详解
http://blog.csdn.net/qqqqqq654/article/details/53043742
- webpack 从入门到工程实践
from:https://www.jianshu.com/p/9349c30a6b3e?utm_campaign=maleskine&utm_content=note&utm_medi ...
- iOS-如何读取Plist文件
解决办法: // 1) 找到Plist文件的路径 "path" NSString *path = [[NSBundle mainBundle]pathForResource:@&q ...
- hdu3535(AreYouBusy)
题目链接:传送门 题目大意:有 n 组任务,m 个体力,每组任务有 k 个,分类为 f,每个任务花费 x 体力,得到 y 开心值,求最大开心值,若不能完成输出-1 分类为 0:这一组中的 k 个任务至 ...
- 8782:乘积最大(划分dp)
8782:乘积最大 同洛谷 P1018 乘积最大 查看 提交 统计 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 今年是国际数学联盟确定的“2000——世界数学年”,又恰逢我 ...
- git GUI 入门
一:安装一个git 及gui 二:配置gui及线上的git链接 在Git Gui中,选择Remote->add添加远程服务器,远程服务器信息有两种填写方式,填写https地址或ssh地址,对应g ...
- 习惯养成和目标追踪APP推荐
一.习惯和目标的不同 习惯:贵在坚持,每天任务一定,而完成总量不定.坚持时间越久越好. 目标:贵在按时完成,任务总量一定,但是每天完成量不做限制.有一个完成期限,但是越早越好. 上面的差别导致了相关A ...