HDU 6336 (规律 + 二维矩阵的前缀和妙用)
给出长度为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 (规律 + 二维矩阵的前缀和妙用)的更多相关文章
- LeetCode74.搜索二维矩阵
74.搜索二维矩阵 描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的最后一个整数. 示例 示 ...
- [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] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- IT公司100题-35- 求一个矩阵中最大的二维矩阵(元素和最大)
问题描述: 求一个矩阵中最大的二维矩阵(元素和最大).如: 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 中最大的是: 4 5 9 10 分析: 2*2子数组的最大和.遍历求和,时 ...
- [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 ...
- lintcode:搜索二维矩阵II
题目 搜索二维矩阵 II 写出一个高效的算法来搜索m×n矩阵中的值,返回这个值出现的次数. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每一列的整数从上到下是排序的. 在每一行或每一列中没 ...
- lintcode :搜索二维矩阵
题目: 搜索二维矩阵 写出一个高效的算法来搜索 m × n矩阵中的值. 这个矩阵具有以下特性: 每行中的整数从左到右是排序的. 每行的第一个数大于上一行的最后一个整数. 样例 考虑下列矩阵: [ [1 ...
- hdu 5517 Triple(二维树状数组)
Triple Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- Python小代码_5_二维矩阵转置
使用列表推导式实现二维矩阵转置 matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] print(matrix) matrix_t = [[ro ...
随机推荐
- Centos里没有lsb_release
查看Centos操作系统版本,输入指令 lsb_release -a 报无此命令 解决办法,安装lsb_release 1.执行指令:yum install -y redhat-lsb 2.安装完毕后 ...
- 30.MIN() 函数
MIN() 函数 MIN 函数返回一列中的最小值.NULL 值不包括在计算中. SQL MIN() 语法 SELECT MIN(column_name) FROM table_name 注释:MIN ...
- 10.IN 操作符
IN 操作符 IN 操作符允许我们在 WHERE 子句中规定多个值. SQL IN 语法 SELECT column_name(s) FROM table_name WHERE column_name ...
- 安全、结构良好的jQuery结构模板
安全.结构良好的jQuery结构模板 ;(function($,window,document,undefined){ //我们的代码- })(jQuery,window,document); 参 ...
- Asp.NET中把DataTable导出为Excel ,中文有乱码现象解决办法
//DataTable为要导出的数据表 DataGrid dg = new DataGrid(); dg.DataSource = DataTable; ...
- Http中的身份传递
IIS默认的身份验证方式 身份传递策略包括使用操作系统的委派功能或在应用程序级传递票证和/或凭证 为了阻止IIS的身份验证委派,可以在web.config加入如下设置, <system.web& ...
- Position Independent Code (PIC) in shared libraries on x64
E原文地址:http://eli.thegreenplace.net/2011/11/11/position-independent-code-pic-in-shared-libraries-on-x ...
- MySQL性能调优与架构设计——第7章 MySQL数据库锁定机制
第7章 MySQL数据库锁定机制 前言: 为了保证数据的一致完整性,任何一个数据库都存在锁定机制.锁定机制的优劣直接应想到一个数据库系统的并发处理能力和性能,所以锁定机制的实现也就成为了各种数据库的核 ...
- delphi7和XE下 获取路径
XE里没有Application.Exename所以用paramstr(0); Str:=ParamStr(0); //自己全路径c:\555\qq.exe,可用于控制台Str:=ExpandFile ...
- Redis缓存穿透、缓存雪崩
缓存穿透 缓存穿透是指查询一个一定不存在的数据,由于缓存是不命中时被动写的,并且出于容错考虑,如果从存储层查不到数据则不写入缓存,这将导致这个不存在的数据每次请求都要到存储层去查询,失去了缓存的意义. ...