HDU6336-2018ACM暑假多校联合训练4-1005-Problem E. Matrix from Arrays-前缀和
题意是给了一种矩阵的生成方式
让你求两个左边之间的矩阵里面的数加起来的和(不是求矩阵的值)
没看标程之前硬撸写了160行
用了前缀和以后代码量缩短到原来的1/3
根据规律可以推导出这个矩阵是在不断重复一个2L*2L的矩阵
求值时,先求出原点到(x2,y2)的值,再减去原点到(x1-1,y2)和原点到(x2,y1-1)的值,再加上被重复减去的原点到(x1-1,y1-1)的值
Problem E. Matrix from Arrays
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1060 Accepted Submission(s): 478
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
#include <iostream> using namespace std; int l;
int M[][];
int A[]; long long _plus(int x, int y)
{
if (x < || y < )
return ;
return
(
M[l - ][l - ] * 1LL * (x / l) * (y / l) + \
M[x % l][l - ] * 1LL * (y / l) + M[l - ][y % l] * 1LL * (x / l) + \
M[x % l][y % l] * 1LL
);
} int main()
{
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--)
{
cin >> l;
for (int i = ; i < l; i++)
cin >> A[i];
int cursor = ;
for (int i = ; i < * l; i++)
for (int j = ; j <= i; j++)
{
M[j][i - j] = A[cursor];
cursor = (cursor + ) % l;
}
l *= ;
for(int i = ; i < l; i++)
for (int j = ; j < l; j++)
{
if (i)
M[i][j] += M[i - ][j];
if (j)
M[i][j] += M[i][j - ];
if (i && j)
M[i][j] -= M[i - ][j - ];
} int n;
cin >> n;
while (n--)
{
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
cout << _plus(x2, y2) - _plus(x1 - , y2) - _plus(x2, y1 - ) + _plus(x1 - , y1 - ) << endl;
}
}
return ;
}
HDU6336-2018ACM暑假多校联合训练4-1005-Problem E. Matrix from Arrays-前缀和的更多相关文章
- HDU6333-2018ACM暑假多校联合训练1002-Harvest of Apples-莫队+费马小定理
题意很简单啦,求S(n,m)的值 通过打表我们可以知道 S(n + 1, m) = S(n, m) * 2 - C(n, m); S(n - 1, m) = (S(n, m) + C(n - 1, m ...
- HDU6400-2018ACM暑假多校联合训练1004-Parentheses Matrix-构造
Parentheses Matrix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Oth ...
- HDU6342-2018ACM暑假多校联合训练4-1011-Problem K. Expression in Memories
Problem K. Expression in Memories Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262 ...
- HDU6330-2018ACM暑假多校联合训练Problem L. Visual Cube
就是画个图啦 分三个平面去画orz #include <iostream> #include <cmath> #include <cstring> #include ...
- HDU6318-2018ACM暑假多校联合训练2-1010-Swaps and Inversions-树状数组
本题题意是,给你一个长度为n的序列,使用最少的操作把序列转换为从小到大的顺序,并输出操作数*min(x,y) 实质上是算出该序列中有多少逆序对,有归并排序和树状数组两种算法,由于数据之间的差值有点大, ...
- HDU6299-2018ACM暑假多校联合训练1002-Balanced Sequence
这个题的题意是给你n个字符串,认定()是一种平衡的串,两个以上连续的()()也是一种平衡的串,如果一对括号里面包含一个平衡的串,这个括号也被算在这个平衡的串之内, 如(()(()))是一个长度为8的平 ...
- HDU6298-2018ACM暑假多校联合训练1001-Maximum Multiple
题意大致是给你一个整数n,让你确定是否有三个正整数x,y,z既能被n整除,又能x+y+z=n,并使xyz最大 从中根据规律可以看出,只有被3或被4整除的数才能满足题目要求 被3整除的最大值为n^3/3 ...
- HDU6301-2018ACM暑假多校联合训练1004-Distinct Values
题意是一个长度为n的序列,给你m组区间(l,r),在这个区间里不能填入重复的数字,同时使整个序列字典序最小 同学用的优先队列,标程里使用的是贪心同时使用set维护答案序列 贪心是先采用pre数组来确定 ...
- HDU6308-2018ACM暑假多校联合训练1011-Time Zone
题目大意就是给你UTC-8时区的时间 让你求对应时区的时间 哇 这个题 看似简单,但是一开始怎么都过不了啊 同学用自己写的read过了,后来看了一下各位大佬说改成分钟随便过,就随便过了 Problem ...
随机推荐
- flask系列三之Jinja2模板
1.如何渲染模板 模板在‘templates’文件夹下(htnl页面) 从flask中导入render_template函数---渲染html模板 在视图函数中,使用render_template 函 ...
- Java对网络图片/本地图片转换成Base64编码和解码
一.将本地图片转换成Base64编码字符串 /** * 将本地图片转换成Base64编码字符串 * * @param imgFile 图片目录路径 * @return */ public static ...
- 不需要SDK调用图灵机器人的方法
图灵机器人的调用其实就是你给服务器发一个文字消息过去,他回你一个,看起来模仿人类对话一样. 不知道为什么要弄个SDK这么麻烦的方法,以前的接口官网上已经没有了,但是还是可以用的.返回的是JSON但也懒 ...
- DDD学习笔录——提炼问题域之知识提炼与协作的基本原则
1.通过通用语言达成共识 通用语言,已经强调过好多遍了,在DDD再怎么重视都不为过,后面可能还会讲. 知识提炼的输出以及共识的构建就是常见的通用语言(UL). 当与业务相关人员和主题专家进行建模时,每 ...
- Delphi IOS 上架
http://docwiki.embarcadero.com/RADStudio/Seattle/en/IOS_Mobile_Application_Development http://docwik ...
- 设备控制接口ioctl详解
[转]Linux设备控制接口 序言设备驱动程序的一个基本功能就是管理和控制设备,同时为用户应用程序提供管理和控制设备的接口.我们前面的“Hello World”驱动程序已经可以提供读写功能了,在这里我 ...
- elasticsearch2.x插件之一:kopf
kopf kopf is a simple web administration tool for elasticsearch written in JavaScript + AngularJS + ...
- Swing自定义JScrollPane的滚动条设置,重写BasicScrollBarUI方法
Swing自定义JScrollPane的滚动条设置,重写BasicScrollBarUI方法 摘自:https://blog.csdn.net/qq_31635851/article/details/ ...
- Maven——继承和聚合
实际项目中,可能正要构建一个大型的系统,但又不想一遍又一遍的重复同样的依赖元素,这种情况是经常出现的.不过还好,maven提供了继承机制,项目可以通过parent元素使用继承,可以避免这种重复.当一个 ...
- docker搭建gitbook服务
Gitbook Gitbook简介 GitBook 是一个基于 Node.js 的命令行工具,可使用 Github/Git 和 Markdown 来制作精美的电子书,GitBook 并非关于 Git ...