hdu多校第4场E. Matrix from Arrays HDU 二维前缀和
Problem E. Matrix from Arrays Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others)
Total Submission(s): Accepted Submission(s): 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 = ;
for (int i = ; ; ++i) {
for (int j = ; j <= i; ++j) {
M[j][i - j] = A[cursor];
cursor = (cursor + ) % 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 (≤T≤) denoting the number of test cases.
Each test case starts with an integer L (≤L≤) denoting the length of A.
The second line contains L integers A0,A1,...,AL− (≤Ai≤).
The third line contains an integer Q (≤Q≤) denoting the number of queries.
Each of next Q lines consists of four integers x0,y0,x1,y1 (≤x0≤x1≤,≤y0≤y1≤) 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 Sample Output Source
Multi-University Training Contest Recommend
chendu | We have carefully selected several similar problems for you:
如图二 根据容斥原理S=S1-S2-S3+S4;;S1, S2, S3, S4都是以(x, y)为右下角,以(0, 0)为左上角的矩阵,问题就转化成了求这样的矩阵图一;
米黄色的面积表示有多少个完整的循环矩阵,下方白条及右方白条表示只有长或宽不完整的矩阵,橙黄色面积表示不完整的循环矩阵;
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
#define ll long long
ll m[][];
ll a[];
ll sum[][];
ll len;
ll jisuan(int x,int y)
{
ll ans=(x/len)*(y/len)*sum[len][len];//多少个重复规律
ans+=sum[x%len][len]*(y/len)+sum[len][y%len]*(x/len);//左边和下面
ans+=sum[x%len][y%len];//左下角
return ans; }
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int l;
scanf("%d",&l);
for(int i=;i<l;i++)
scanf("%lld",&a[i]);
int cursor = ;
for (int i = ; i<; ++i)
{
for (int j = ; j <= i; ++j)
{
m[j+][i - j+] = a[cursor];
cursor = (cursor + ) %l;
}
}
len=*l;
memset(sum, , sizeof(sum));
for(ll i=; i<=len; i++){
for(ll j=; j<=len; j++){
sum[i][j]=sum[i][j-]+sum[i-][j]-sum[i-][j-]+m[i][j];//容斥原理
}
}
int q;
scanf("%d",&q);
while(q--)
{
int x0,y0,x1,y1;
scanf("%d%d%d%d",&x0,&y0,&x1,&y1);
x0++,y0++,x1++,y1++;
ll ans=; ans=jisuan(x1,y1)+jisuan(x0-,y0-)-jisuan(x0-,y1)-jisuan(x1,y0-);
cout<<ans<<endl; } } return ;
}
hdu多校第4场E. Matrix from Arrays HDU 二维前缀和的更多相关文章
- 杭电多校第四场 E Matrix from Arrays
Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 ...
- hdu多校第八场Parentheses Matrix
#include<bits/stdc++.h> using namespace std; ][]; int main() { int t; scanf("%d",&am ...
- 2018 HDU多校第四场赛后补题
2018 HDU多校第四场赛后补题 自己学校出的毒瘤场..吃枣药丸 hdu中的题号是6332 - 6343. K. Expression in Memories 题意: 判断一个简化版的算术表达式是否 ...
- 2018 HDU多校第三场赛后补题
2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...
- HDU 多校第四场题解
对于 D 题的原题意,出题人和验题人赛前都没有发现标算存在的问题,导致了许多选手的疑惑和时间的浪费,在此表示真诚的歉意! 预计难度分布: Easy - DJKL, Medium - ABCEG, Ha ...
- HDU 6336.Problem E. Matrix from Arrays-子矩阵求和+规律+二维前缀和 (2018 Multi-University Training Contest 4 1005)
6336.Problem E. Matrix from Arrays 不想解释了,直接官方题解: 队友写了博客,我是水的他的代码 ------>HDU 6336 子矩阵求和 至于为什么是4倍的, ...
- 2018 Multi-University Training Contest 4 Problem E. Matrix from Arrays 【打表+二维前缀和】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6336 Problem E. Matrix from Arrays Time Limit: 4000/20 ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] 240. Search a 2D Matrix II 搜索一个二维矩阵 II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
随机推荐
- jenkins构建任务后发送邮件
1.jenkins登录后-系统管理-系统设置打开后定位到下面的位置:系统管理员邮件地址一定要填写 2.下滑页面定位到extend E-mail Notification:这个是jenkins的一个插件 ...
- SpringBoot Controller接收参数的几种常用方式
第一类:请求路径参数1.@PathVariable获取路径参数.即url/{id}这种形式. 2.@RequestParam获取查询参数.即url?name=这种形式 例子GET http://loc ...
- JAVA获取计算机IP地址
import java.net.InetAddress;import java.net.UnknownHostException;public class HuoQu { public stat ...
- oracle 对表的操作
对日期数据的插入 insert into tabname(datecol) value(sysdate) ; -- 用date值 insert into tabname(datecol) value ...
- 内存管理 re模块
# 内存垃圾回收机制: # 工作原理:引用计数 # 引用就+1 释放就是-1 当计数为0时 就会被垃圾回收机制回收 # 标记清除:解决循环引用导致的内存泄漏 # 标记:GC roots可以直接或间接访 ...
- phpstudy-5.6.27-nts 安装redis扩展
redis扩展安装流程 第一步: 首先直接查看一下phpinfo()的信息 找到下面两条信息 Architecture x86 PHP Extension Build API20131226,NTS, ...
- 爬微信好友签名和QQ好友签名
先说如何爬微信好友签名,主要使用itchat,这个库提供直接的api来获取好友信息,只要用正则过滤出就行了.说一下步骤,就不贴代码了.# 登陆# 获取好友列表# 提取签名# jieba分词# word ...
- 配置IPV6地址
题:在考试系统上设定接口eth0使用下列IPV6地址: system1上的地址应该是2003:ac18::305/64 system2上的地址应该是2003:ac18::30a/64 两个系统必须能与 ...
- 回车\r与换行\n
在计算机出现之前,有一种电传机械打字机,每秒可以打10个字符.但是有一个问题,就是打满一行后,需要进行换行,换行是需要0.2秒.如果这时有字符传入,就会丢失两个字符.为了解决这个问题,便定义了两个字符 ...
- TabLayout基本使用
前言 Tablayout继承自HorizontalScrollView,可以用作顶部标签效果.底部导航栏效果.一般多与ViewPager一起使用. 想直接了解如何实现短下滑效果的请看:TabLayou ...