题意是给了一种矩阵的生成方式

让你求两个左边之间的矩阵里面的数加起来的和(不是求矩阵的值)

没看标程之前硬撸写了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

Problem Description
Kazari has an array A length of L, she plans to generate an infinite matrix M using A.
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.

 
Input
The first line of the input contains an integer T (1≤T≤100) denoting the number of test cases.
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).
 
Output
For each test case, print an integer representing the sum over the specific sub matrix for each query.
 
Sample Input
1
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
 
Sample Output
1
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-前缀和的更多相关文章

  1. 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 ...

  2. HDU6400-2018ACM暑假多校联合训练1004-Parentheses Matrix-构造

    Parentheses Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Oth ...

  3. 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 ...

  4. HDU6330-2018ACM暑假多校联合训练Problem L. Visual Cube

    就是画个图啦 分三个平面去画orz #include <iostream> #include <cmath> #include <cstring> #include ...

  5. HDU6318-2018ACM暑假多校联合训练2-1010-Swaps and Inversions-树状数组

    本题题意是,给你一个长度为n的序列,使用最少的操作把序列转换为从小到大的顺序,并输出操作数*min(x,y) 实质上是算出该序列中有多少逆序对,有归并排序和树状数组两种算法,由于数据之间的差值有点大, ...

  6. HDU6299-2018ACM暑假多校联合训练1002-Balanced Sequence

    这个题的题意是给你n个字符串,认定()是一种平衡的串,两个以上连续的()()也是一种平衡的串,如果一对括号里面包含一个平衡的串,这个括号也被算在这个平衡的串之内, 如(()(()))是一个长度为8的平 ...

  7. HDU6298-2018ACM暑假多校联合训练1001-Maximum Multiple

    题意大致是给你一个整数n,让你确定是否有三个正整数x,y,z既能被n整除,又能x+y+z=n,并使xyz最大 从中根据规律可以看出,只有被3或被4整除的数才能满足题目要求 被3整除的最大值为n^3/3 ...

  8. HDU6301-2018ACM暑假多校联合训练1004-Distinct Values

    题意是一个长度为n的序列,给你m组区间(l,r),在这个区间里不能填入重复的数字,同时使整个序列字典序最小 同学用的优先队列,标程里使用的是贪心同时使用set维护答案序列 贪心是先采用pre数组来确定 ...

  9. HDU6308-2018ACM暑假多校联合训练1011-Time Zone

    题目大意就是给你UTC-8时区的时间 让你求对应时区的时间 哇 这个题 看似简单,但是一开始怎么都过不了啊 同学用自己写的read过了,后来看了一下各位大佬说改成分钟随便过,就随便过了 Problem ...

随机推荐

  1. Python Twisted系列教程15:测试诗歌

    作者:dave@http://krondo.com/tested-poetry/  译者: Cheng Luo 你可以从”第一部分 Twist理论基础“开始阅读:也可以从”Twisted 入门!“浏览 ...

  2. 「小程序JAVA实战」 小程序的事件(11)

    转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-11/ 我们以前在web开发的时候,web页面也有一些相关的事件,当然小程序要接触屏幕要进行一些点击 ...

  3. asp.net js 存取cookie

    asp.net //传进来的 public BaseController(BaseHttpHandler handler, HttpContext context) // { //根据地址设置cook ...

  4. c之指针退化和printf小陷阱

    今天参加了个笔试和面试,面试官给我指出了我试卷上的错误,我才发现,我的知识疏漏之处原来有不少,很是感谢. 记得曾经有本书,专门写c的陷阱来着,里面有很多都牵扯到指针.嘿嘿,这小家伙古灵精怪,总是喜欢误 ...

  5. libevent源码深度剖析二

    libevent源码深度剖析二 ——Reactor模式 张亮 前面讲到,整个libevent本身就是一个Reactor,因此本节将专门对Reactor模式进行必要的介绍,并列出libevnet中的几个 ...

  6. MyBatis—实现关联表查询

    一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...

  7. 551. Student Attendance Record I 从字符串判断学生考勤

    [抄题]: You are given a string representing an attendance record for a student. The record only contai ...

  8. 面试题:filter过滤器 listener 监听器 案例有点用

    1.Filter工作原理(执行流程) 当客户端发出Web资源的请求时,Web服务器根据应用程序配置文件设置的过滤规则进行检查,若客户请求满足过滤规则,则对客户请求/响应进行拦截,对请求头和请求数据进行 ...

  9. git 的使用方法

    git 的使用有3个主要步骤: 1.1 工作区域操作: 在自己的git账号下构建一个工作目录, 并往工作目录里添加文件内容(cp /root/data/VIP_Amount_prediction/* ...

  10. 使用 Sentry集中处理错误

    Sentry的简介 Sentry 是一个实时的事件日志和聚合平台,基于 Django 构建. Sentry 可以帮助你将程序的所有 exception 自动记录下来,处理 exception 是每个程 ...