题目

给出长度为n 的A矩阵 , 按

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;
}
}
构造出无限矩阵M , 然后给出l1 , r1 , l2, r2 ; 查询以(l1,r1)左上角 (l2,r2)右上角 的矩阵和 题意:用上面的转化规则十分容易的想到可能是有什么规律 , 所以我们打了个表出来发现 , n为奇数是相同矩阵的周期为n , n为偶数相同矩阵的周期为2*n ; 然后。。。。没有想到二维矩阵的前缀和,。。所以一直打不出来;
。。
我们先看一个图 ,如果对二维矩阵前缀和敏感这题目就是直接秒杀的事情

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll; ll a[];
ll map[][];
int L; void init(){
memset(map,,sizeof map);
int cursor=;
for (int i = ;i<L*; ++i) {
for (int j = ; j <= i; ++j) {
map[j][i-j] = a[cursor];
cursor=(cursor+)%(L);
}
}
for(int i=;i<*L;i++){
for(int j=;j<*L;j++){
if((i>)&&(j>))map[i][j]+=map[i-][j]+map[i][j-]-map[i-][j-];
if((i>)&&(j==))map[i][j]+=map[i-][j];
if((i==)&&(j>))map[i][j]+=map[i][j-];
}
}
}
ll f(int x,int y){
if(x<||y<)return ;
ll ans=;
ll xx=x/L;//这里不用long long就会wa
ll yy=y/L;
ll sx=x%L;
ll sy=y%L;
ans+=xx*yy*map[L-][L-];
ans+=yy*map[sx][L-];
ans+=xx*map[L-][sy];
ans+=map[sx][sy];
return ans;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&L);
for(int i=;i<L;i++){
scanf("%lld",&a[i]);
}
init();
int q;
scanf("%d",&q);
L=L*;
while(q--){
int x1,x2,y1,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("%lld\n",f(x2,y2)-f(x2,y1-)-f(x1-,y2)+f(x1-,y1-));
}
}
return ;
}
												

HDU 6336 (规律 + 二维矩阵的前缀和妙用)的更多相关文章

  1. LeetCode74.搜索二维矩阵

    74.搜索二维矩阵 描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 示 ...

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

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

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

  4. IT公司100题-35- 求一个矩阵中最大的二维矩阵(元素和最大)

    问题描述: 求一个矩阵中最大的二维矩阵(元素和最大).如: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 中最大的是: 4 5 9 10   分析: 2*2子数组的最大和.遍历求和,时 ...

  5. [CareerCup] 11.6 Search a 2D Matrix 搜索一个二维矩阵

    11.6 Given an M x N matrix in which each row and each column is sorted in ascending order, write a m ...

  6. lintcode:搜索二维矩阵II

    题目 搜索二维矩阵 II 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没 ...

  7. lintcode :搜索二维矩阵

    题目: 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每行的第一个数大于上一行的最后一个整数. 样例 考虑下列矩阵: [ [1 ...

  8. hdu 5517 Triple(二维树状数组)

    Triple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  9. Python小代码_5_二维矩阵转置

    使用列表推导式实现二维矩阵转置 matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] print(matrix) matrix_t = [[ro ...

随机推荐

  1. Ubuntu16 install Anaconda

    gbt@gbt-Precision-7720:~$ gbt@gbt-Precision-7720:~$ cd Anacondagbt@gbt-Precision-7720:~/Anaconda$ gb ...

  2. 跨平台的图形软件Dia

    一款非常不错的软件Dia,软件很小,免费.好用.跨平台(linux.windows.mac).可导出多种格式图片,除了流程图.UML建模图,还可以绘制其他很多图. ubuntu下可以直接通过命令行su ...

  3. 详解servlet的url-pattern匹配规则.RP

    首先需要明确几容易混淆的规则: servlet容器中的匹配规则既不是简单的通配,也不是正则表达式,而是特定的规则.所以不要用通配符或者正则表达式的匹配规则来看待servlet的url-pattern. ...

  4. 关于spring”通配符的匹配很全面, 但无法找到元素 'context:component-scan' 的声明“的错误

    关于spring配置的问题 近日学习spring时遇到了这个问题: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreExcept ...

  5. APUE(3)---文件I/O (1)

    一.引言 UNIX系统中的大多数文件对I/O只需用到5个函数:open/read/write/lseek和close,这些函数都是不带缓冲I/O(Unbuffered I/O).只要涉及到多个进程间共 ...

  6. html manifest 离线配置

    HTML5 引入了应用程序缓存,这意味着 web 应用可进行缓存,并可在没有因特网连接时进行访问. 应用程序缓存为应用带来三个优势: 离线浏览 - 用户可在应用离线时使用它们 速度 - 已缓存资源加载 ...

  7. 动态往 datagridview 追加行、列操作

    DataGridViewTextBoxColumn acCode = new DataGridViewTextBoxColumn(); acCode.Name = "acCode" ...

  8. CentOS 网络操作

    ifconfig:查看网卡信息 网卡配置文件位置: /etc/sysconfig/network-scripts/文件夹 nmtui:配置网卡 netstat -tlunp:查看端口信息 端口信息存储 ...

  9. golang 重构博客统计服务

    欢迎关注楼主与他的小伙伴们的小站,每周分享一些技术文章,让我们在技术上一起成长------> 戳这里,欢迎光临小站 -_- 作为一个后端开发,在docker,etcd,k8s等新技术不断涌现的今 ...

  10. 八、Node.js-http模块

    JS代码如下: /* 如果我们使用PHP来编写后端的代码时,需要Apache 或者 Nginx 的HTTP 服务器,并配上 mod_php5 模块和php-cgi,来处理客户端的请求相应. 不过对 N ...