1. Problem E. Matrix from Arrays
  2.  
  3. Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others)
  4. Total Submission(s): Accepted Submission(s):
  5.  
  6. Problem Description
  7. Kazari has an array A length of L, she plans to generate an infinite matrix M using A.
  8. The procedure is given below in C/C++:
  9.  
  10. int cursor = ;
  11. for (int i = ; ; ++i) {
  12. for (int j = ; j <= i; ++j) {
  13. M[j][i - j] = A[cursor];
  14. cursor = (cursor + ) % L;
  15. }
  16. }
  17.  
  18. 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.
  19.  
  20. Input
  21. The first line of the input contains an integer T (≤T≤) denoting the number of test cases.
  22. Each test case starts with an integer L (≤L≤) denoting the length of A.
  23. The second line contains L integers A0,A1,...,AL− (≤Ai≤).
  24. The third line contains an integer Q (≤Q≤) denoting the number of queries.
  25. 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).
  26.  
  27. Output
  28. For each test case, print an integer representing the sum over the specific sub matrix for each query.
  29.  
  30. Sample Input
  31.  
  32. Sample Output
  33.  
  34. Source
  35. Multi-University Training Contest
  36.  
  37. Recommend
  38. chendu | We have carefully selected several similar problems for you:

如图二 根据容斥原理S=S1-S2-S3+S4;;S1, S2, S3, S4都是以(x, y)为右下角,以(0, 0)为左上角的矩阵,问题就转化成了求这样的矩阵图一;

米黄色的面积表示有多少个完整的循环矩阵,下方白条及右方白条表示只有长或宽不完整的矩阵,橙黄色面积表示不完整的循环矩阵;

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<string.h>
  4. using namespace std;
  5. #define ll long long
  6. ll m[][];
  7. ll a[];
  8. ll sum[][];
  9. ll len;
  10. ll jisuan(int x,int y)
  11. {
  12. ll ans=(x/len)*(y/len)*sum[len][len];//多少个重复规律
  13. ans+=sum[x%len][len]*(y/len)+sum[len][y%len]*(x/len);//左边和下面
  14. ans+=sum[x%len][y%len];//左下角
  15. return ans;
  16.  
  17. }
  18. int main()
  19. {
  20. int T;
  21. scanf("%d",&T);
  22. while(T--)
  23. {
  24. int l;
  25. scanf("%d",&l);
  26. for(int i=;i<l;i++)
  27. scanf("%lld",&a[i]);
  28. int cursor = ;
  29. for (int i = ; i<; ++i)
  30. {
  31. for (int j = ; j <= i; ++j)
  32. {
  33. m[j+][i - j+] = a[cursor];
  34. cursor = (cursor + ) %l;
  35. }
  36. }
  37. len=*l;
  38. memset(sum, , sizeof(sum));
  39. for(ll i=; i<=len; i++){
  40. for(ll j=; j<=len; j++){
  41. sum[i][j]=sum[i][j-]+sum[i-][j]-sum[i-][j-]+m[i][j];//容斥原理
  42. }
  43. }
  44. int q;
  45. scanf("%d",&q);
  46. while(q--)
  47. {
  48. int x0,y0,x1,y1;
  49. scanf("%d%d%d%d",&x0,&y0,&x1,&y1);
  50. x0++,y0++,x1++,y1++;
  51. ll ans=; ans=jisuan(x1,y1)+jisuan(x0-,y0-)-jisuan(x0-,y1)-jisuan(x1,y0-);
  52. cout<<ans<<endl;
  53.  
  54. }
  55.  
  56. }
  57.  
  58. return ;
  59. }

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. 2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165335

    一.资源下载以及工具安装 1.下载虚拟机工具VMware. 下载链接 :https://www.baidu.com/link?url=uuaBW5ETUl3GrhUKvPbbEc7QlQvGHfpD8 ...

  2. flask 电子邮件进阶实践-用模板发送163邮件

    电子邮件进阶实践 下面来学习构建邮件的HTML正文,并使用模板组织内容. 一封电子邮件的正文可以是纯文本(text/plain),也可以是HTML格式的文本(text/html).处于全面的考虑,一封 ...

  3. Windows Server 2012安装IIS 8.0

    一.安装 1.鼠标右键[This PC]→[Manage] 2.选择[Add Roles and Features] 3.勾选[.Net Framewore 3.5] 和 [.Net Framewor ...

  4. 前端使用node.js的http-server开启一个本地服务器

    前端使用node.js的http-server开启一个本地服务器 在写前端页面中,经常会在浏览器运行HTML页面,从本地文件夹中直接打开的一般都是file协议,当代码中存在http或https的链接时 ...

  5. IP通信基础学习第四周(上)

    IP地址现在由因特网名字与号码指派公司ICANN进行分配,它是标志一个主机(或路由器)和一条链路的接口,其编址方法有:分类的IP地址.子网的划分.构成超网. 分类两级IP地址可以记为:IP::={&l ...

  6. [转载]DBMS_LOB

    1.LOB背景 在现在的系统开发中,需要存储的已不仅仅是简单的文字信息,也包括一些图片.音像资料或者超长的文本,这要求后台数据库具有存储这些数据的能力,Oracle通过提供LOB对象实现了该功能. 2 ...

  7. vue/iview使用moment.js

    方法一 main.js引入moment 获取当前时间 this.time = this.$moment()._d; // 当前时间 this.time0 =this.$moment().subtrac ...

  8. 【Alpha】Scrum Meeting 10

    目录 前言 任务分配 燃尽图 会议照片 签入记录 困难 前言 第10次会议于4月14日19:00在教一316召开. 交流确认了任务进度,对下一阶段任务进行分配.时长40min. 任务分配 姓名 当前阶 ...

  9. wordpress安装教程

    最近安装了wordpress来搭建自己的网站,过程有些艰辛,以防以后转移服务器再次遇到这个难题,在此记下自己的这次安装过程以及一些问题,同时也供遇到相同问题的初次接触者做参考. 另外说明一下我用的操作 ...

  10. eclipse上的.properties文件中文编辑显示问题

    安装 装Properties Editor插件,方法: Help --> Install New Software -->输入:http://propedit.sourceforge.jp ...