HDU-6336-构造
Problem E. Matrix from Arrays
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 419 Accepted Submission(s): 180
The procedure is given below in C/C++:
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;
}
}
Her friends don't believe that she has the ability to generate such a huge matrix, so they come up with a lot of queries about M, each of which focus the sum over some sub matrix. Kazari hates to spend time on these boring queries. She asks you, an excellent coder, to help her solve these queries.
Each test case starts with an integer L (1≤L≤10) denoting the length of A.
The second line contains L integers A0,A1,...,AL−1 (1≤Ai≤100).
The third line contains an integer Q (1≤Q≤100) denoting the number of queries.
Each of next Q lines consists of four integers x0,y0,x1,y1 (0≤x0≤x1≤108,0≤y0≤y1≤108) querying the sum over the sub matrix whose upper-leftmost cell is (x0,y0) and lower-rightest cell is (x1,y1).
3
1 10 100
5
3 3 3 3
2 3 3 3
2 3 5 8
5 1 10 10
9 99 999 1000
101
1068
2238
33076541
比赛的时候其实知道做法了,打表后可以发现这个矩阵的行列都是由循环节的而且行列的循环长度一致,
只不过一开始默认为是L,后来发现奇数是L,偶数是2*L,我们都按照2*L来做就好了,预处理出来行和列的
前缀和然后容斥的处理询问。
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define mp make_pair
#define LL long long
int cursor = ;
int M[][],A[],L,len;
LL pre1[][],pre2[][];
LL g[];
LL sum(int x,int y){
if(x<=||y<=) return ;
LL ans=;
for(int i=;i<=len;++i){
g[i]=g[i-]+pre1[i][len]*(y/len)+pre1[i][y%len];
}
return g[len]*(x/len)+g[x%len];
}
int main(){
int t,n,m,i,j,k,Q;
int x1,y1,x2,y2;
cin>>t;
while(t--){
scanf("%d",&L);
len=L*;
memset(pre1,,sizeof(pre1));
memset(pre2,,sizeof(pre2));
for(i=;i<L;++i) scanf("%d",A+i);
int cursor = ;
for (int i = ;i<=; ++i) {
for (int j = ; j <= i; ++j) {
M[j+][i - j+] = A[cursor];
cursor = (cursor + ) % L;
}
}
for(i=;i<=len;++i){
for(j=;j<=len;++j){
pre1[i][j]=M[i][j]+pre1[i][j-];
pre2[i][j]=M[j][i]+pre2[i][j-];
}
}
scanf("%d",&Q);
while(Q--){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
x1++,y1++,x2++,y2++;
printf("%lld\n",sum(x1-,y1-)+sum(x2,y2)-sum(x1-,y2)-sum(x2,y1-));
}
}
return ;
}
HDU-6336-构造的更多相关文章
- HDU 6336.Problem E. Matrix from Arrays-子矩阵求和+规律+二维前缀和 (2018 Multi-University Training Contest 4 1005)
6336.Problem E. Matrix from Arrays 不想解释了,直接官方题解: 队友写了博客,我是水的他的代码 ------>HDU 6336 子矩阵求和 至于为什么是4倍的, ...
- HDU 5667 构造矩阵快速幂
HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...
- HDU - 6150 构造题
最近的vj好垃圾,老崩,实名吐槽 HDU - 6150 题意:给出一个错误的求最小点覆盖的函数,需要来构造一组样例,使得那个函数跑出来的答案是正解的3倍以上. 很巧妙的构造技巧,首先想法就是弄一个二分 ...
- Number Sequence(HDU 1005 构造矩阵 )
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 6336 (规律 + 二维矩阵的前缀和妙用)
题目 给出长度为n 的A矩阵 , 按 int cursor = 0; for (int i = 0; ; ++i) { for (int j = 0; j <= i; ++j) { M[j][i ...
- hdu 3879 hdu 3917 构造最大权闭合图 俩经典题
hdu3879 base station : 各一个无向图,点的权是负的,边的权是正的.自己建一个子图,使得获利最大. 一看,就感觉按最大密度子图的构想:选了边那么连接的俩端点必需选,于是就以边做点 ...
- hdu 5444(构造二叉树然后遍历)
Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- A Simple Math Problem(HDU 1757 构造矩阵)
If x < 10 f(x) = x.If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-1 ...
- HDU 6336 Matrix from Arrays (杭电多校4E)
遇事不决先打表. 然后会发现(个屁)大的矩形是由一个2L*2L的矩形重复出现组成的然后我们就可以这个矩形分成四个点到(0, 0)点的矩形,这样问题就变成了求四个到顶点(0, 0)的矩形的面积,然后就先 ...
- HDU 6336 Matrix from Arrays
Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 ...
随机推荐
- Could not stop Cortex-M device! please check the JTAG cable的解决办法
今天程序烧录后,进行调试时keil提示:Could not stop Cortex-M device! please check the JTAG cable 如图: 于是我在网上搜了一下, ...
- 如何判断一个单向链表是否为回文链表(Palindrome Linked List)
题目:给定一个单向链表,判断它是不是回文链表(即从前往后读和从后往前读是一样的).原题见下图,还要求了O(n)的时间复杂度O(1)的空间复杂度. 我的思考: 1,一看到这个题目,大脑马上想到的解决方案 ...
- JaveWeb 公司项目(2)----- 类模态窗口显示DIV并将DIV放置在屏幕正中间
上一篇博客写的是通过隐藏显示进行div的替换,接下来需要在原有的div前添加一个div,进行表单的提交,需要将div放置在正中间,然后类似C#中的模态窗口,在进行完新弹出的div操作之后,才可以进行下 ...
- 9. spring项目中web.xml详解解读
引言:本篇博客的内容大部分都来自网上,有的是直接copy,有的是自己整理而来.既然网上已经有了,为啥还有自己copy呢? 感觉是因为网上的东西太散了或者是样式不够美观,所以自己又copy了一遍.如有侵 ...
- Django 日志
Django使用Python内置的logging模块实现它自己的日志系统. 如果你没有使用过logging模块,请参考Python教程中的相关章节. 直达链接<logging模块详解>. ...
- 使用openpyxl实现excel文件的读取操作
1.环境准备 python3环境.安装openpyxl模块 2.excel文件数据准备 3.为方便直接调用,本代码直接封装成类 from openpyxl import load_workbook c ...
- Eclipse搭建maven项目的流程,聚合所有的子模块项目
Eclipse搭建maven项目的流程 2018年03月01日 15:47:03 阅读数:22 1:搭建parent工程,用来聚合所有的子模块项目 2:搭建公共使用的模块common 这里你要点击空白 ...
- SpringBoot:Maven创建一个HelloWorld
先看一下百度百科的解释: Maven项目对象模型(POM:project object model),可以通过一小段描述信息来管理项目的构建,报告和文档的项目管理工具软件. Maven的核心功能便是合 ...
- 力扣 报错 runtime error: load of null pointer of type 'const int'
runtime error: load of null pointer of type 'const int' 要求返回的是int* 解决方案 1.指针使用malloc分配空间 用 int * p = ...
- CSRF攻击和防护
攻击模拟步骤: 防护方法: 在客户端向后端请求界面数据的时候,后端会往响应中的 cookie 中设置 csrf_token 的值 在 Form 表单中添加一个隐藏的的字段,值也是 csrf_token ...