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系列八之请求方法、g对象和钩子函数
一.get方法 ,post方法 post请求在模板中要注意几点: (1)input标签中,要写name来标识这个value的key,方便后台获取. (2)在写form表单的时候,要指定method=' ...
- delphi OleVariant转换RecordSet
delphi OleVariant转换RecordSet uses Data.Win.ADODB; function varToRecordSet( parms : OleVariant ) : Da ...
- Tornado 高并发源码分析之一---启动一个web服务
前言: 启动一个tornado 服务器基本代码 class HomeHandler(tornado.web.RequestHandler): #创建 RequesHandler 对象,处理接收到的 h ...
- apache配置多个虚拟主机 localhost访问不了解决方案
在httpd-vhosts.conf,重定向localhost <VirtualHost *:80> ServerAdmin webmaster@dummy-host2.exampl ...
- Basics
[Basics] 1.You can declare multiple constants or multiple variables on a single line, separated by c ...
- PHP网站
1.NetBeans 解决PHP调试问题:https://netbeans.org/ 支持PHP调试的版本 2. http://document.thinkphp.cn/manual_3_2.html ...
- 主机不能访问虚拟机中的web服务【解决方案】
百度了其它一些方法都不行,最后实在没辙,关了windows防火墙和Linux防火墙,居然能够访问了,我服. 总结一下,原来是Red Hat Linux 6.0防火墙没有开启端口80,开启的方法为(老版 ...
- 设置MongoDB课程环境
Setting Up Your Course Environment This course is designed to be very hands on. Virtually all of the ...
- Linux nethack
一.简介 游戏目标:在地下城的最底层取得炎多的护符项链(Amulet of Yendor),并返回最上层,在圣祭坛上供奉给神灵.完成整个游戏的奖赏是,玩家会成为不朽的半神. 二.安装 1)下载源码 ...
- win10手动开启wifi
win+R键,输入cmd,以管理员身份运行,输入netsh wlan set hostednetwork mode=allow ssid=wifi key=wifimima123回车 解释一下: ss ...