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 ...
随机推荐
- BZOJ 4399 魔法少女LJJ(线段树合并)
题意 https://www.lydsy.com/JudgeOnline/problem.php?id=4399 思路 码农题,需要一定代码功底.方法很暴力,先将权值离散,表示在线段树里储存的位置,每 ...
- Google advertiser api开发概述
对象.方法和服务 AdWords API 主要供 AdWords 的高级用户使用.如果您是 AdWords 新手,或需要复习 AdWords 基本概念,请查看 AdWords 基础知识页面. 对象层级 ...
- Qt打包
先在Qt Creator里release一遍,打开Qt下载时自带的像命令控制台一样的东西,比如我在创建项目时选用的是MinGw编译器, 然后在文件管理器里找到release生成的exe,cd进exe所 ...
- Leetcode1 - A + B Problem - Easy
Write a function that add two numbers A and B. Example Example 1: Input: a = 1, b = 2 Output: 3 Expl ...
- springmvc后台接前端的参数,数组,集合,复杂对象等
springmvc后台接前端的参数,数组,集合,复杂对象等 参考地址:https://blog.csdn.net/feicongcong/article/details/54705933 常用的几种 ...
- Scrapy创建爬虫项目
1.打开cmd命令行工具,输入scrapy startproject 项目名称 2.使用pycharm打开项目,查看项目目录 3.创建爬虫,打开CMD,cd命令进入到爬虫项目文件夹,输入scrapy ...
- 关于Test类中不能使用Autowired注入bean的问题
在测试类中使用AutoWired注解一直不能获取到Bean,调用方法时一直报空指针异常,我有在其他类中使用AutoWired试了下,发现能够生效.问题应该就是处在Test类中,后面找了半天终于找到问题 ...
- SqlServer中exists和in的区别
1.in 2.exists
- 虚拟机镜像压缩(qcow2,raw)
●qcow2 格式的压缩首先,需要对虚拟机系统的剩余空间写零操作: $ dd if=/dev/zero of=/zero.dat 删除 zero.dat$ rm /zero.dat关闭虚拟机,进入虚拟 ...
- 用原生js来处理跨域的数据(jsonp)
说明总结: 1.ajax和jsonp其实本质上是不同的东西.ajax的核心是通过XmlHttpRequest获取非本页内容,而jsonp的核心则是动态添加<script>标签来调用服务器提 ...