HDU 6336 子矩阵求和
Problem E. Matrix from Arrays
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1162 Accepted Submission(s): 522
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).
解析 矩阵是无限大的所以右下角没有被赋值的地方是不受影响的QAQ 长度为偶数循环矩阵是 2L*2L 所以直接按照2L算就是了 二位前缀和预处理一下 数一下完事了
AC代码
- #include <bits/stdc++.h>
- #define pb push_back
- #define mp make_pair
- #define fi first
- #define se second
- #define all(a) (a).begin(), (a).end()
- #define fillchar(a, x) memset(a, x, sizeof(a))
- #define huan printf("\n");
- #define debug(a,b) cout<<a<<" "<<b<<" ";
- using namespace std;
- typedef long long ll;
- const ll maxn=1e2+,inf=0x3f3f3f3f;
- const ll mod=1e9+;
- ll a[maxn],g[maxn][maxn],n;
- ll sum[maxn][maxn];
- void init()
- {
- int cnt=;
- for(int i=;i<*n;++i)
- {
- for(int j=;j<=i;++j)
- {
- g[j][i-j]=a[cnt];
- cnt=(cnt+)%n;
- }
- }
- sum[][]=g[][];
- for(int i=;i<*n;i++)
- {
- for(int j=;j<*n;j++)
- {
- if(i>&&j>)sum[i][j]=g[i][j]+sum[i-][j]+sum[i][j-]-sum[i-][j-];
- if(i==&&j>)sum[i][j]=g[i][j]+sum[i][j-];
- if(j==&&i>)sum[i][j]=g[i][j]+sum[i-][j];
- }
- }
- }
- ll solve(int x,int y)
- {
- ll ans=;
- ll xx=x/n;
- ll yy=y/n;
- ll yux=x%n;
- ll yuy=y%n;
- ans+=xx*yy*sum[n-][n-];
- ans+=yy*sum[yux][n-];
- ans+=xx*sum[n-][yuy];
- ans+=sum[yux][yuy];
- return ans;
- }
- int main()
- {
- int t,q;
- cin>>t;
- while(t--)
- {
- cin>>n;
- for(int i=;i<n;i++)
- cin>>a[i];
- init();n=n*;
- cin>>q;
- while(q--)
- {
- int x1,x2,y1,y2;
- cin>>x1>>y1>>x2>>y2;
- //cout<<solve(x1,y1)<<endl;
- cout<<solve(x2,y2)-solve(x1-,y2)-solve(x2,y1-)+solve(x1-,y1-)<<endl;
- }
- }
- 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倍的, ...
- hihoCoder [Offer收割]编程练习赛3 D子矩阵求和
子矩阵求和 http://hihocoder.com/discuss/question/3005 声明一下: n是和x一起的,m是和y一起的 x是横着的,y是纵着的,x往右为正,y往下为正 (非常反常 ...
- HDU 2011 多项式求和
http://acm.hdu.edu.cn/showproblem.php?pid=2011 Problem Description 多项式的描述如下:1 - 1/2 + 1/3 - 1/4 + 1/ ...
- HDU 2015 偶数求和
http://acm.hdu.edu.cn/showproblem.php?pid=2015 Problem Description 有一个长度为n(n<=100)的数列,该数列定义为从2开始的 ...
- 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 ...
- HDU - 6336 Problem E. Matrix from Arrays (规律+二维前缀和)
题意: for (int i = 0; ; ++i) { for (int j = 0; j <= i; ++j) { M[j][i - j] = A[cursor]; cursor = (cu ...
- HDU 6336 (规律 + 二维矩阵的前缀和妙用)
题目 给出长度为n 的A矩阵 , 按 int cursor = 0; for (int i = 0; ; ++i) { for (int j = 0; j <= i; ++j) { M[j][i ...
- 【hiho一下 第146周】子矩阵求和
[题目链接]:http://hihocoder.com/contest/hiho146/problem/1 [题意] [题解] 设s[i][j]表示左上角的坐标为(i,j)的n*m的矩阵的和; 有s[ ...
随机推荐
- laravel5.5路由
目录 1. routes/web.php 2. routes/api.php 3. 重定向路由 4. 路由参数 5. 约束 6. 命名路由 7. 路由组 8 路由模型绑定 9 表单方法伪造 10 访问 ...
- Tensorflor实现文本分类
Tensorflor实现文本分类 下面我们使用CNN做文本分类 cnn实现文本分类的原理 下图展示了如何使用cnn进行句子分类.输入是一个句子,为了使其可以进行卷积,首先需要将其转化为向量表示,通常使 ...
- Python 推导式推导序列
推导式是从一个或多个迭代器快速创建序列的方法.它可以将循环和条件判断结合,从而避免冗长的代码. 一.列表推导式 语法: [表达式 for item in 可迭代对象] [表达式 for item in ...
- 哲学家就餐-同步问题解析-python
五个哲学家吃五盘通心粉,由于通心粉很滑,所以必须要拿起左右两边的叉子才能吃到. 叉子的摆放如图所示. 那么问题来了:能为每一个哲学家写一段描述其行为的程序,保证不会出现死锁. 解法1:让他等待能够使用 ...
- echarts 柱状图下钻功能
var drillDown = { getOption : function () { var option = null; option = { title: { text: ' ...
- 系统编程--高级IO
1.非阻塞I/O 非阻塞I/O使我们可以调用不会永远阻塞的I/O操作,例如open,read和write.如果这种操作不能完成,则立即出错返回,表示该操作如继续执行将继续阻塞下去.对于一个给定的描述符 ...
- 轻松精通awk数组企业问题案例
考试题1:处理以下文件内容,将域名取出并根据域名进行计数排序处理:(百度和sohu面试题) oldboy.log http://www.etiantian.org/index.html http:// ...
- nginx禁止访问目录中可执行文件
某些网站系统需要用户上传图片等文件到某些目录下,难免程序有些漏洞,导致用户上传了php.cgi等等可执行的文件,导致网站陷入非常为难的境地. 此时我们可以通过nginx来禁止用户访问这些目录下的可执行 ...
- 用PHP写一个双向队列
PHP写一个双向队列,其实是在考察PHP几个内置数组的函数 用PHP写一个双向队列 <?php class Deque{ public $queue = array(); /** * 尾部入对 ...
- 【bzoj3029】守卫者的挑战 概率dp
题目描述 给出一个数$m$和$n$次操作,第$i$操作有$p_i$的概率成功,成功后会使$m$加上$a_i$($a_i$为正整数或$-1$),求$n$次操作以后成功的操作次数不少于$l$且$m\ge ...