任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6336

Problem E. Matrix from Arrays

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1711    Accepted Submission(s): 794

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 = 0;

for (int i = 0; ; ++i) {    for (int j = 0; j <= i; ++j) {         M[j][i - j] = A[cursor];        cursor = (cursor + 1) % 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 (1≤T≤100) denoting the number of test cases.
Each test case starts with an integer L (1≤L≤10) denoting the length of A.
The second line contains L integers A0,A1,...,AL−1 (1≤Ai≤100).
The third line contains an integer Q (1≤Q≤100) denoting the number of queries.
Each of next Q lines consists of four integers x0,y0,x1,y1 (0≤x0≤x1≤108,0≤y0≤y1≤108) 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
1
3
1 10 100
5
3 3 3 3
2 3 3 3
2 3 5 8
5 1 10 10
9 99 999 1000

Sample Output
1
101
1068
2238
33076541

Source
2018 Multi-University Training Contest 4

题意概括:

给一个长度为 L 的序列,要求按照代码的要求构建一个矩阵。

Q次查询,每次查询输入矩阵的左上角和右下角坐标,输出矩阵的值的总和。

解题思路:

并没有什么过人的天分,老老实实把构建的矩阵输出来找规律。

发现大矩阵是长宽 为 2*L 的小矩阵构造而成的。

问题就转换为了类似于求矩阵面积的问题,也就可以通过二维前缀和 sum(x, y) 来求解答案。

例如要求解 (x1, y1, x2, y2) 的值就等于 sum(x2, y2) - sum(x1-1, y2) - sum(x2, y1-1) + sum(x1-1, y1-1);

如何计算 sum(x, y)呢?首先统计有多少个 2L * 2L 的子矩阵,然后再单独加上这些除掉这些子矩阵的其余部分。

AC code:

 1 #include<set>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<iostream>
 5 #include<cstring>
 6 #include<vector>
 7 #include<queue>
 8 #include<cmath>
 9 #define INF 0x3f3f3f3f
10 #define LL long long
11 using namespace std;
12 const int MAXN = 1e3+10;
13 LL sum;
14 int mmp[MAXN][MAXN];
15 int N, M, L, Q;
16 LL sum1[MAXN], sum2[MAXN];
17 int A[MAXN];
18
19 void init()
20 {
21     memset(mmp, 0, sizeof(mmp));
22     memset(sum1, 0, sizeof(sum1));
23     memset(sum2, 0, sizeof(sum2));
24     int cnt = 0;
25     for(int i = 0; i < 4*L; i++){
26         for(int j = 0; j <= i; j++){
27             mmp[j][i-j] = A[cnt];
28             cnt = (cnt+1)%L;
29         }
30     }
31     sum = 0;
32     for(int i = 0; i < 2*L; i++){
33         for(int j = 0; j < 2*L; j++){
34             sum+=mmp[i][j];
35             sum1[i] += mmp[i][j];
36             sum2[j] += mmp[i][j];
37         }
38     }
39 }
40
41 LL query(int x, int y)
42 {
43     int cnt_x = (x+1)/(2*L);
44     int cnt_y = (y+1)/(2*L);
45     LL res = sum*cnt_x*cnt_y;
46     int lenx = (x+1)%(2*L);
47     int leny = (y+1)%(2*L);
48
49     for(int i = 0; i < lenx; i++) res+= cnt_y*sum1[i];
50     for(int j = 0; j < leny; j++) res+= cnt_x*sum2[j];
51
52
53     for(int i = 0; i < lenx; i++)
54         for(int j = 0; j < leny; j++){
55             res+=mmp[i][j];
56         }
57
58     return res;
59 }
60
61 int main()
62 {
63     int T_case;
64     int x1, x2, y1, y2;
65     scanf("%d", &T_case);
66     while(T_case--){
67         scanf("%d", &L);
68         for(int i = 0; i < L; i++){
69             scanf("%d", &A[i]);
70         }
71         init();
72
73         scanf("%d", &Q);
74         while(Q--){
75             scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
76             LL ans = query(x2, y2)-query(x1-1, y2) - query(x2, y1-1) + query(x1-1, y1-1);
77             printf("%lld\n", ans);
78         }
79     }
80     return 0;
81 }

View Code

转载于:https://www.cnblogs.com/ymzjj/p/10334522.html

2018 Multi-University Training Contest 4 Problem E. Matrix from Arrays 【打表+二维前缀和】相关推荐

  1. BZOJ4972 八月月赛 Problem B 小Q的方格纸 二维前缀和

    欢迎访问~原文出处--博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4972 八月月赛Problem B 题目概括 一个矩阵,一坨询问,问矩阵中一个特定方向的等腰直角三角 ...

  2. 2018 Multi-University Training Contest 3 Problem F. Grab The Tree 【YY+BFS】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6324 Problem F. Grab The Tree Time Limit: 2000/1000 MS ...

  3. 2018 蓝桥杯省赛 B 组模拟赛(一)I. 天上的星星(二维前缀和)

    题目描述 在一个星光摧残的夜晚,蒜头君一颗一颗的数这天上的星星. 蒜头君给在天上巧妙的画了一个直角坐标系,让所有的星星都分布在第一象.天上有 nn 颗星星,他能知道每一颗星星的坐标和亮度. 现在,蒜头 ...

  4. AtCoder Beginner Contest 203(Sponsored by Panasonic) D.Pond(二分+二维前缀和)

    link 思路: 先来想想暴力的写法: n2n^{2}n2枚举左上角的顶点,k2k^{2}k2求最小值. 考虑优化: 1.1.1.答案有单调性,可以二分答案,省去枚举左上角顶点的复杂度. 2.2.2. ...

  5. AtCoder Beginner Contest 203 Pond(二分+二维前缀和)

    样例输入 [样例1] 3 2 1 7 0 5 8 11 10 4 2 [样例2] 3 3 1 2 3 4 5 6 7 8 9 样例输出 [样例1] 4 [样例2] 5 据说这个题用对顶堆维护被卡了 先 ...

  6. [蓝桥杯 2018 国 B] 搭积木 (区间dp + 二维前缀和优化)

    原题链接(洛谷) 题目描述 小明对搭积木非常感兴趣.他的积木都是同样大小的正立方体. 在搭积木时,小明选取 m m m 块积木作为地基,将他们在桌子上一字排开,中间不留空隙,并称其为第 0 0 0 层 ...

  7. 2018 Multi-University Training Contest 4

    1001:Problem A. Integers Exhibition 重要结论:一个数如果是k-magic数,那么它的倍数也是k-magic数. 发现非k-magic数很少  一开始知道1是非k-m ...

  8. Sichuan University Programming Contest 2018 Preliminary

    嗯为了防止大家AK,所以这次的A题和K题我们就当做不存在好了! 经历了昨天写了两个多小时的博客没保存的心态炸裂,今天终于下了个Markdown.所以我猜这篇的格式应该会更好看一点! 好吧废话不多说 题 ...

  9. 2018 Multi-University Training Contest 7 Age of Moyu

    Age of Moyu 来追梦 Problem Description Mr.Quin love fishes so much and Mr.Quin's city has a nautical sy ...

最新文章

  1. 移动端自动播放音视频实现代码
  2. 基于Tkinter利用python实现颜色空间转换程序
  3. SAP Fiori Elements - how to set breakpoint to get converted xml view parsed by f
  4. C++笔记-基于邻接表的BFS(宽度优先遍历)
  5. 一次cpu占用100%的故障解决
  6. python isinstance()方法的使用
  7. DirectX9学习(四)装载位图
  8. IT杂谈(一):炫酷好玩网站汇总
  9. 倪海厦天纪笔记16_倪海厦《天纪·天机道》笔记
  10. buu [QCTF2018]Xman-RSA
  11. 蓝桥杯 试题 B: 纪念日
  12. 重阳重游万州文峰塔记
  13. [JavaSE基础笔记]Day10 石头迷阵实现
  14. 查看安卓系统的外接USB声卡
  15. SPH算法简介(三): 光滑核函数
  16. Java多线程系列-CyclicBarrier
  17. Gmail代理收发邮件
  18. c语言编写8个发光二极管循环右移,1、P1 口做输出口,接八只发光二极管,编写程序,使发光二极管循环点亮。...
  19. 字符编码的故事(ASCII ISO GBK GB2312 UTF-8)
  20. 你喜欢这个邮箱代码吗?用代码实现邮件的撰写和发送

热门文章

  1. 本地调试微信程序ngrok
  2. Python词汇比较运算符
  3. 《Attention Is All You Need》
  4. 网络爬虫之Url含有中文如何转码
  5. Dirichlet Distribution(狄利克雷分布)与Dirichlet Process(狄利克雷过程)
  6. 实数范围内(包含负数)的求模与求余运算异同
  7. Python range() 函数用法
  8. NLP--Word2Vec详解
  9. Struts2标签库常用标签
  10. Struts标签入门