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 二维前缀和的更多相关文章

  1. 杭电多校第四场 E Matrix from Arrays

    Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 ...

  2. hdu多校第八场Parentheses Matrix

    #include<bits/stdc++.h> using namespace std; ][]; int main() { int t; scanf("%d",&am ...

  3. 2018 HDU多校第四场赛后补题

    2018 HDU多校第四场赛后补题 自己学校出的毒瘤场..吃枣药丸 hdu中的题号是6332 - 6343. K. Expression in Memories 题意: 判断一个简化版的算术表达式是否 ...

  4. 2018 HDU多校第三场赛后补题

    2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...

  5. HDU 多校第四场题解

    对于 D 题的原题意,出题人和验题人赛前都没有发现标算存在的问题,导致了许多选手的疑惑和时间的浪费,在此表示真诚的歉意! 预计难度分布: Easy - DJKL, Medium - ABCEG, Ha ...

  6. HDU 6336.Problem E. Matrix from Arrays-子矩阵求和+规律+二维前缀和 (2018 Multi-University Training Contest 4 1005)

    6336.Problem E. Matrix from Arrays 不想解释了,直接官方题解: 队友写了博客,我是水的他的代码 ------>HDU 6336 子矩阵求和 至于为什么是4倍的, ...

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

  8. [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 ...

  9. [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 ...

随机推荐

  1. Mysql查询创建和导入操作

    如何安装: https://www.cnblogs.com/bigbrotherer/p/7241845.html 登录: mysql -uroot -p 输入密码:xxxx 显示当前数据库: sho ...

  2. source insight 4.0.86.0 安装过程中出现的问题

    1.sourceinsight_4.0.86.0-setup.exe 2.sourceinsight4.exe覆盖安装目录中的sourceinsight4.exe 3.导入lic文件 过程中360会将 ...

  3. vs编译过程中出的错

    如果运行时出现这样的错,关闭正在运行的.exe文件,即可正常运行

  4. win10 WSL kali 下载源 --另外 恭喜马哥喜提博客

    第一篇也不知道写什么,就把昨晚安装kali时遇见的事写一下吧! 因为win10应用商店已经加入了kali,也省的我再去网上下载镜像,可下载后  wsl  未能设置为开发人员模式, 这算是失误吧!步骤如 ...

  5. arch安装以及配置记录

    设置ip和网关 ifconfig eth0 10.82.16.233 netmask 255.255.255.0 route add default gw 10.82.16.1 或者 ip addr ...

  6. 每天一套题打卡|河南省第十一届ACM/ICPC

    A 计划日 题意:已知李明在YYYY年MM月DD日星期W订了学习计划,现在想看看李明N天后的完成情况和个人总结,你能告诉我那天的日期和星期几吗? 模拟日期计算: 计算星期可以用基姆拉尔森公式 //中国 ...

  7. Python - 如何统计序列中元素出现的频次

    1.用内置的count()方法,该方法返回子字符串在字符串中出现的次数(同样适用于列表)2.用collections模块的Counter类 示例: from collections import Co ...

  8. ES6解构过程添加一个默认值和赋值一个新的值

    const info = { name: 'xiaobe', } const { name: nickName = '未知' } = info; 其中nickName是解构过程中新声明的一个变量,并且 ...

  9. Pandas 基础(14) - DatetimeIndex and Resample

    这一小节要介绍两个内容, 一个是 DatetimeIndex 日期索引, 另一个是 Resample, 这是一个函数, 可以通过参数的设置, 来调整数据的查询条件, 从而得到不同的结果. 首先看下关于 ...

  10. _spellmod_aura_on_classmask

    -- 修改职业所有技能,公共冷却时间.冷却时间等--小技巧: 根据职业107,108来修改(近期会有改动) `aura` 技能光环ID `spellfamily` 职业代码 `type1` 百分比还是 ...