题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6336

题目:

题意:给你一个l个元素的数组a,用题目中的程序构造一个新的矩阵,询问q次,问以(x1,y1)为左上角,(x2,y2)为右下角的矩阵内的元素之和(原点在左上角)。

思路:我们通过打表可以发现这个大矩阵都是以左上角2l*2l的小矩阵M循环出现的,所以对于每次查询我们只需统计他要查询的矩阵包含多少个完整的M,对于那些不构成完整的行列和,我们首先用前缀和统计出来,最后加起来即可。我的方法用下图表示,答案就是S1-S2-S3+S4(S4为左上角那个小正方形):

代码实现如下:

 #include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = ;
const int maxn = 1e6 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f; int t, l, q;
ll sum;
int a[], mp[][];
ll sum1[], sum2[]; void init() {
int cnt = ;
for(int i = ; i < * l; ++i) {
for(int j = ; j <= i; ++j) {
mp[j][i-j] = a[cnt];
cnt = (cnt + ) % l;
}
}
sum = ;
memset(sum1, , sizeof(sum1));
memset(sum2, , sizeof(sum2));
for(int i = ; i < * l; i++) {
for(int j = ; j < * l; j++) {
sum += mp[i][j];
}
}
for(int i = ; i < * l; ++i) {
for(int j = ; j < * l; ++j) {
sum1[i] += mp[i][j];
}
}
for(int j = ; j < * l; ++j) {
for(int i = ; i < * l; ++i) {
sum2[j] += mp[i][j];
}
}
} ll query(int x, int y) {
ll rec = ;
int xx = (x + ) / ( * l), yy = (y + ) / ( * l);
x = (x + ) % ( * l), y = (y + ) % ( * l);
rec = sum * xx * yy;
for(int i = ; i < y; i++) rec += xx * sum2[i];
for(int i = ; i < x; i++) rec += yy * sum1[i];
for(int i = ; i < x; i++) {
for(int j = ; j < y; j++) {
rec += mp[i][j];
}
}
return rec;
} int main() {
//FIN;
scanf("%d", &t);
while(t--) {
scanf("%d", &l);
for(int i = ; i < l; i++) {
scanf("%d", &a[i]);
}
init();
scanf("%d", &q);
int x0, y0, x2, y2;
while(q--) {
scanf("%d%d%d%d", &x0, &y0, &x2, &y2);
printf("%lld\n", query(x2, y2) - query(x0 - , y2) - query(x2, y0-) + query(x0-, y0-));
}
}
return ;
}

Problem E. Matrix from Arrays(杭电2018年多校第四场+思维+打表找循环节)的更多相关文章

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

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

  2. Problem B. Harvest of Apples(杭电2018年多校+组合数+逆元+莫队)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 题目: 题意:求C(n,0)+C(n,1)+……+C(n,m)的值. 思路:由于t和n数值范围太 ...

  3. 杭电2018暑假多校第一场 D Distinct Values hdu6301 贪心

    Distinct Values Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  4. 2018年多校第四场第二题 B. Harvest of Apples hdu6333

    题意:给定10^5以内的n,m求∑组合数(n,i),共10^5组数据. 题解: 定义 S(n, m) = \sum_{i = 0} ^ {m} {n \choose i}S(n,m)=∑​i=0​m​ ...

  5. 杭电第四场 hdu6336 Problem E. Matrix from Arrays 打表找规律 矩阵前缀和(模板)

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

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

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

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

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

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

  9. Problem L. Visual Cube(杭电多校2018年第三场+模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6330 题目: 题意:给你长宽高,让你画出一个正方体. 思路:模拟即可,湘潭邀请赛热身赛原题,不过比那个 ...

随机推荐

  1. C#控件DropDownList下拉列表默认打开

    c#中的控件DropDownList要实现默打开确实不容易,之前也是想过页面上的点击之后就打开了,那直接模拟点击不就行了,试过后大失所望,根本没有效果. 于是网上找到了一个例子能实现IE浏览器下的打开 ...

  2. JS高级 1

    关于string,number是大写,那么就是构造函数,变量不可能为null值,除非手动设置,要解除对象的引用的时候手动去除. in关键字操作数组的时候操作的是索引值,不是里面的内容,.在操作对象的时 ...

  3. JS DOM(2017.12.28)

    一.获得元素节点的方法 document.getElementById()    根据Id获取元素节点 document.getElementsByName()    根据name获取元素节点   遍 ...

  4. jCanvaScript canvas的操作库

    在jcscript.com上下载最新的jCanvaScript.1.5.18.min.js文件  里面有很多关于canvas的方法都已经是封装好了的,只需直接调用,但是要注意调用之前和调用之后都要写: ...

  5. RT-thread内核对象标志flag总结

    一.内核标志flag 在内核对象控制块中有一个标志成员flag(rt_uint8_t flag; ),这个标志在不同有内核对象中具有不同的含义.rt-thread的内核对象有定时器.线程.信号量.互斥 ...

  6. 原生JS表单序列化

    // 表单序列化,IE9+ HTMLFormElement.prototype.serialize = function() { var form = this; // 表单数据 var arrFor ...

  7. 【刷题】洛谷 P2764 最小路径覆盖问题

    题目描述 «问题描述: 给定有向图G=(V,E).设P 是G 的一个简单路(顶点不相交)的集合.如果V 中每个顶点恰好在P 的一条路上,则称P是G 的一个路径覆盖.P 中路径可以从V 的任何一个顶点开 ...

  8. [JLOI2014]松鼠的新家 树上差分

    差分 一开始竟然想分情况讨论来差分,然后发现各自情况要分析, 就是为了解决中间节点重复计算的问题, 结果 最后一想,中间重复计算了一次,那我最后减掉不就好了么,,, 那这就是一道差分裸题了(这是唯一不 ...

  9. BZOJ2752:[HAOI2012]高速公路——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2752 https://www.luogu.org/problemnew/show/P2221#sub ...

  10. 用ByteArrayOutputStream解决IO流乱码问题

    IO中用ByteArrayOutputStream解决乱码问题 --另一种解决乱码的方法 IO中另外一种防止乱码的方法:使用ByteArrayOutputStream在创建ByteArrayOutpu ...