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. 使用jconsole监控JVM内存

    首先声明:此篇博文分析的是JDK1.8. JVM内存区域总体分两类:heap区和非heap区.Jconsole中对内存划分为同样的结构,如下: heap区又分为:  - Eden Space(伊甸园) ...

  2. kubernetes 源码安装部署 1.12

    一. 前期准备 参考文档 https://jimmysong.io/kubernetes-handbook/practice/create-tls-and-secret-key.html 1. 安装g ...

  3. 使用GoldenGate EVENTACTIONS执行数据的实时触发和定制化

    Oracle GoldenGate不仅可以在线实时同步数据(包括增量和存量),也内置有一套事件触发流程,允许用户根据某张表某条记录的某个特殊字段值,触发相应的自定义执行流程,比如接收到某个银行账号的大 ...

  4. php 把数组保存为标准的数组格式,存储到文件中

    <?php $file='./test.php'; $array=array('color'=> array('blue','red','green'),'size'=> array ...

  5. Haproxy官方文档翻译(第三章)全局参数(1) 附英文原文

    3.全局参数 在global这个节点里的参数是“进程范围的”并且经常是“操作系统指定”的.它们通常是一次性设置而且一旦正确设置不需要动来动去的.它们中的一些和命令行对应. global节点支持以下关键 ...

  6. python操作字符串类型json的注意点

    python操作json的方法有json.dumps——将json对象(字典)转换为字符串对象json.loads——将字符串对象转换为json对象(字典)如果定义json对象jsonstring1= ...

  7. 【Django-URL name详解005】

    1.打开zqxt_views/urls.py from django.contrib import admin from django.urls import path from calc impor ...

  8. 2018年-2019年第二学期第五周C#学习个人总结

    在本周我学习了第五章面向对象高级中的5.3多态,5.3多态中主要包括重写父类方法,多态的实现,base关键字,里氏转换原则,Object类.在重写父类方法中要求当重写父类的方法时,要求子类的方法名,参 ...

  9. Python day 03

    dya 03 今日内容 整形 布尔类型 字符串 补充 运算符补充 in value = '我是中国人' # 判断'中国'是否是value所代指的字符串的子序列. v1 = '中国' in value ...

  10. Spark机器学习基础一

    特征工程 对连续值处理 0.binarizer/二值化 from __future__ import print_function from pyspark.sql import SparkSessi ...