题目

给出长度为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. 2-chrome无法添加扩展程序

    1.更多工具->拓展程序->打开开发者模式->重启浏览器 2.将拓展程序拖入,确认安装

  2. 数字图像处理实验(9):PROJECT 04-05,Correlation in the Frequency Domain 标签: 图像处理MATLAB 2017-05-25 10:14

    实验要求: Objective: To know how to implement correlation of 2 functions in the frequency domain and, us ...

  3. Vue.js 安装及其环境搭建

    For me or other first studying vue.js. For Windows PC: 1.先安装node.js 安装官网最新的即可 版本应该要大于6.0版本 nodejs的官网 ...

  4. 用js获取cookie

    //获取cookiefunction cookie_email(cookie_name){ var cookiestr = document.cookie; if (cookiestr.length ...

  5. HttpSession解决表单的重复提交

    1). 重复提交的情况: ①. 在表单提交到一个 Servlet, 而 Servlet 又通过请求转发的方式响应一个 JSP(HTML) 页面, 此时地址栏还保留着 Serlvet 的那个路径, 在响 ...

  6. Win7怎么进入安全模式 三种轻松进入Win7安全模式方法

    发布时间:2013-05-27 11:23 作者:电脑百事网原创 来源:www.pc841.com 13783次阅读 win7的安全模式和XP如出一辙,在安全模式里我们可以删除顽固文件.查杀病毒.解除 ...

  7. 在UIWebView中添加自定义编辑菜单

    如何在UIWebView中添加自定义的编辑菜单困扰了很久.没想到意外的简单! 现在很多的内容提供类应用中,长按内容页会选中按的单词并且显示一个编辑菜单.如图: 独乐乐不如众乐乐.一篇好文章是需要大家一 ...

  8. 最近的一些零碎知识点,jquery遍历

    1.使按钮无法点击 $(“#btn”).attr("disable",true); 2.返回上一个页面 history.back(-1); 3.$(this).siblings() ...

  9. Boosting and Its Application in LTR

    1 Boosting概述 2 Classification and Regression Tree 3 AdaBoost 3.1 算法框架 3.2 原理:Additive Modeling 4 Gra ...

  10. TSQL--删除正在运行的数据库

    ); SET @dbName='DB1_SNAP' BEGIN TRY --===================================== --查找当前数据库所有连接并删除 DECLARE ...