题目链接: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. Python使用ElementTree美化XML格式

    Python中使用ElementTree可以很方便的处理XML,但是产生的XML文件内容会合并在一行,难以看清楚. 如下格式: <root><aa>aatext<cc&g ...

  2. 抽象类 C#

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. window service 创建

    1:vs中创建一个 window servece 2.右键 添加安装程序 3.更改属性视图中的Account属性为LocalService(本地服务) 更改ServiceName为你自己的服务名称   ...

  4. Foundation框架—时间输出格式NSDateFormatter

    在开发iOS程序时,有时候需要将时间格式调整成自己希望的格式,这个时候我们可以用NSDateFormatter类来处理.下面来介绍NSDateFormatter的常用属性和API:  1.常用属性 @ ...

  5. SPFA判負環

    馬上就退役了,時間不足就不多介紹了 反正DFS是會T飛的,BFS就沒關係了qwq #include<cmath> #include<queue> #include<cst ...

  6. Andorid API Package ---> android.app

    包名: android.app                                     Added in API level 1       URL:http://developer. ...

  7. CentOS 文件特殊权限SUID,SGID,SBIT

    1.SUID ,是一种对二进制程序进行设置的特殊权限,可以让二进制程序的执行者临时拥有所有者的权限(仅对拥有执行权限的二进制程序有效). (1)SUID权限仅对二进制程序有效: (2)本权限仅在执行该 ...

  8. [BZOJ4822] [CQOI2017] 老C的任务

    题目链接 BZOJ:https://lydsy.com/JudgeOnline/problem.php?id=4822. 洛谷:https://www.luogu.org/problemnew/sho ...

  9. BZOJ5324 & 洛谷4563 & LOJ2545:[JXOI2018]守卫——题解

    https://www.lydsy.com/JudgeOnline/problem.php?id=5324 https://www.luogu.org/problemnew/show/P4563 ht ...

  10. 浅谈Hibernate框架(一)——.hbm.xml中的配置

    Hibernate一枚“全自动”的ORM框架: 用IDE工具集成Hibernate会自动生成: 以.hbm.xml为后缀结尾的配置文件+ POJO类 + Dao类 主键查询: Session.load ...