Problem E. Matrix from Arrays(杭电2018年多校第四场+思维+打表找循环节)
题目链接: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年多校第四场+思维+打表找循环节)的更多相关文章
- 2018 HDU多校第四场赛后补题
2018 HDU多校第四场赛后补题 自己学校出的毒瘤场..吃枣药丸 hdu中的题号是6332 - 6343. K. Expression in Memories 题意: 判断一个简化版的算术表达式是否 ...
- 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数值范围太 ...
- 杭电2018暑假多校第一场 D Distinct Values hdu6301 贪心
Distinct Values Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 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=0m ...
- 杭电第四场 hdu6336 Problem E. Matrix from Arrays 打表找规律 矩阵前缀和(模板)
Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 ...
- 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 ...
- 杭电多校第四场 E Matrix from Arrays
Problem E. Matrix from Arrays Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 ...
- 2018 HDU多校第三场赛后补题
2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...
- Problem L. Visual Cube(杭电多校2018年第三场+模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6330 题目: 题意:给你长宽高,让你画出一个正方体. 思路:模拟即可,湘潭邀请赛热身赛原题,不过比那个 ...
随机推荐
- lintcode-14-二分查找
二分查找 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. 样例 在数组 ...
- <Android>菜单资源的使用
典型菜单资源结构:<menu>根元素且没有属性,在<menu>根元素里面会嵌套<item>和<group>子元素,<item>里面可以嵌套& ...
- c语言作业1
- week1技术随笔
2016-09-06 2016年9月3日 类别c 内容c 开始时间s 结束时间e 被打断时间I 总计(min) 读书 读构建之法 8:40 10:00 11 69 读书 构建之法-个人能力 , ...
- IO复用、多进程和多线程三种并发编程模型
I/O复用模型 I/O复用原理:让应用程序可以同时对多个I/O端口进行监控以判断其上的操作是否可以进行,达到时间复用的目的.在书上看到一个例子来解释I/O的原理,我觉得很形象,如果用监控来自10根不同 ...
- 【Python】python文件名和文件路径操作
Readme: 在日常工作中,我们常常涉及到有关文件名和文件路径的操作,在python里的os标准模块为我们提供了文件操作的各类函数,本文将分别介绍“获得当前路径”“获得当前路径下的所有文件和文件夹, ...
- 【刷题】BZOJ 2038 [2009国家集训队]小Z的袜子(hose)
Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命-- 具体来说,小Z把这N只 ...
- POJ3498:March of the Penguins——题解
最近的题解的故事背景割. 题目: 描述 在靠近南极的某处,一些企鹅站在许多漂浮的冰块上.由于企鹅是群居动物,所以它们想要聚集到一起,在同一个冰块上.企鹅们不想把自己的身体弄湿,所以它们在冰块之间跳跃, ...
- BZOJ3236:[AHOI2013]作业——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3236 第一种做法: 建两棵主席树分别处理两个问题. 第一个问题水,第二个问题参考SPOJ3267/ ...
- LOJ6303:水题——题解
https://loj.ac/problem/6303 题目来自LOJ. 就记一个公式,设f(n,k)为n!里分解得到的k(k为质数)的个数,则f(n,k)=f(n/k,k)+n/k. 证明很好证,显 ...