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 题目概括 一个矩阵,一坨询问,问矩阵中一个特定方向的等腰直角三角 ...
随机推荐
- oracle 手动 备份 恢复
手工备份, 我只考虑全备, 即 control file, redo log file, datafile, password file, spfile(pfile), listener.ora, t ...
- .NET开发相关使用工具和框架
转自: http://www.cnblogs.com/NatureSex/archive/2011/04/21/2023265.html 开发类 visual_studio 2005-2010系列-- ...
- Android无线测试之—UiAutomator UiScrollable API介绍三
获取列表子元素 一.相关API介绍 返回值 API 描述 UiObject getChildByDescription(UiSelector childPattern, String text, bo ...
- android--WaveView(波浪形View) 的实现记录
背景 请假回家当伴郎,由于实在无聊,就写下了此篇博客!!按照惯例,先上动态图 怎么样!效果比较赞吧!!! 思路 当我第一次看见这个效果的时候,我的第一个想法是:如果是静态的时候是什么样子的!好,再来张 ...
- python之进制转换
Python中二进制是以0b开头的: 例如: 0b11 则表示十进制的3 8进制是以0开头的: 例如: 011则表示十进制的9 16进制是以0x开头的: 例如: 0x11则表示十进制 ...
- SDN开发过程中遇到的一些问题总结
我用的是ryu控制器,用Mininet作为网络系统平台. 当启动控制器的时候如果提示:unsupported version 0x1. if possible, set the switch to u ...
- * 和 ?在 shell 命令行中与在正则表达式中的区别
Linux 正则表达式 你有没有想过,在 shell 命令行中的 *,?和正则表达式中的*,?是否一样? 自打好多年前接触 DOS,就知道了* 和?这两个通配符(Wildcard),象 dir *.* ...
- scrapy工程创建及pycharm运行
1.通过命令行创建scrapy工程项目 scrapy startproject (工程名) scrapy startproject myxml 2.利用爬虫模板设置爬虫文件 在这个过程中我们可以先利用 ...
- 品友推广的投放原理 RTB:Real Time Bidding(实时竞价) DSP:Demand-Side Platform(需求方平台) 广告交易平台:AD Exchange
总结: 1.实时竞价 0.1秒出价各个广告主出价,投放价高者: RTB(Real Time Bidding)实时竞价,是一种利用第三方技术在数以百万计的网站或移动端针对每一个用户展示行为进行评估以及出 ...
- 开机启动/etc/rc.local失效无效怎么办?解决方法
开机启动/etc/rc.local失效问题的解决方法 第一种情况:当centOS 随机启动文件 /etc/rc.local 失效时请按如下修改1.echo /etc/rc.local >> ...