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 ...
随机推荐
- (cvpr2019) The Degradation Model and Solution of DPSR
新的退化模型: $y = (x\downarrow_{s}) \otimes k + n $ 其中$\downarrow_{s}$代表尺度因子为$s$的双三次下采样,$y$表达的是低分辨率图像(经过双 ...
- laravel-admin 上传图片过程中遇到的问题
1. 报错 Disk [admin] not configured, please add a disk config in `config/filesystems.php`. 在config/fil ...
- 【融云分析】如何实现分布式场景下唯一 ID 生成?
◀背景▶ 对于一套分布式部署的 IM 系统,要求每条消息的 ID 要保证在集群中全局唯一且按生成时间有序排列.如何快速高效的生成消息数据的唯一 ID ,是影响系统吞吐量的关键因素.那么,融云是如何做到 ...
- redis(一主两从三哨兵模式搭建)记录
转自:http://www.cnblogs.com/fly-piglet/p/9836314.html 目的: 让看看这篇文章的的人能够知道:软件架构.软件的安装.配置.基本运维的操作.高可用测试.也 ...
- 移动端js调试工具:eruda
通常写前端页面都在Chrome浏览器的开发模式下进行调试,但是写放在移动端的H5页面时,有时候会遇到在Chrome上调试没有问题,但是在手机的浏览器上有问题的情况:或者有些功能只能在特定的容器中才能其 ...
- Docker初始
如今Docker的使用已经非常普遍,特别在一线互联网公司.使用Docker技术可以帮助企业快速水平扩展服务,从而到达弹性部署业务的能力.在云服务概念兴起之后,Docker的使用场景和范围进一步发展,如 ...
- Java 键盘输入数字(空格隔开) 将数字存入数组
Scanner sc = new Scanner(System.in); String inputString = sc.nextLine(); String stringArray[] = inpu ...
- Bugku-CTF之速度要快
Day21 速度要快 速度要快!!!!!! http://123.206.87.240:8002/web6/
- 动态令牌验证遇到的问题(判断用户长按backspace键)
因为最近负责泰康项目的前端工作,他们的登录需要进行安全验证,也就是所谓的双因素验证,即在OA平台登录过后,还需要安全部门发送安全令牌进行验证.令牌验证效果如下: 主要功能有:1.默认第一项focus. ...
- centos设置网卡开机自启动
1.进入目录/etc/sysconfig/network-scripts/2.修改ifcfg-enxxxxxxxx 文件 (即你的网卡标识命名的配置文件)3. 将ONBOOT=no改成yes4. ...