杭电第四场 hdu6336 Problem E. Matrix from Arrays 打表找规律 矩阵前缀和(模板)
Problem E. Matrix from Arrays
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1384 Accepted Submission(s): 630
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.
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).
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
101
1068
2238
33076541
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e3+10;
const ll mod = 1e9+7;
const double pi = acos(-1.0);
const double eps = 1e-8;
//FILE* fout = fopen("0001.out", "w");
ll n, T;
ll mapn[maxn][maxn], a[maxn];
ll dp[maxn][maxn]; //(i,j)区域的前缀和
ll get( ll s, ll t ) { //这里的s,t由x,y减一得到,有可能产生负数
if( s == -1 || t == -1 ) { //如果s,t为负数,dp的值为0
return 0;
}
ll x = s%n, cnt_x = s/n; //判断s,t范围内由几个2*n的区域组成
ll y = t%n, cnt_y = t/n;
//debug(x), debug(cnt_x), debug(y), debug(cnt_y);
//debug(dp[x][n-1]), debug(dp[n-1][y]), debug(dp[x][y]); return dp[x][n-1]*cnt_y+dp[n-1][y]*cnt_x+dp[n-1][n-1]*cnt_x*cnt_y+dp[x][y];
}
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
scanf("%lld",&T);
while( T -- ) {
memset(dp,0,sizeof(dp));
memset(mapn,0,sizeof(mapn));
scanf("%lld",&n);
for( ll i = 0; i < n; i ++ ) {
scanf("%lld",&a[i]);
}
ll cur = 0;
for( ll i = 0; i <= 100; i ++ ) {
for( ll j = 0; j <= i; j ++ ) {
mapn[j][i-j] = a[cur];
cur = (cur+1)%n;
}
}
dp[0][0] = mapn[0][0];
for( ll i = 1; i < 2*n; i ++ ) {
dp[0][i] = dp[0][i-1] + mapn[0][i];
}
for( ll i = 1; i < 2*n; i ++ ) {
dp[i][0] = dp[i-1][0] + mapn[i][0];
}
for( ll i = 1; i < 2*n; i ++ ) {
for( ll j = 1; j < 2*n; j ++ ) { //求前缀和
dp[i][j] = mapn[i][j] + dp[i][j-1] + dp[i-1][j] - dp[i-1][j-1];
}
}
n = 2*n; //将n变成2*n 因为如果是奇数大小为L*L,如果为偶数大小为2L*2L的矩阵是重复出现
ll q, x0, y0, x1, y1;
scanf("%lld",&q);
while( q -- ) {
scanf("%lld%lld%lld%lld",&x0,&y0,&x1,&y1);
//fprintf( fout, "%lld\n", get(x1,y1)-get(x1,y0-1)-get(x0-1,y1)+get(x0-1,y0-1) );
printf("%lld\n",get(x1,y1)-get(x1,y0-1)-get(x0-1,y1)+get(x0-1,y0-1));
}
}
return 0;
}
杭电第四场 hdu6336 Problem E. Matrix from Arrays 打表找规律 矩阵前缀和(模板)的更多相关文章
- 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 ...
- 数学--数论--HDU 1792 A New Change Problem (GCD+打表找规律)
Problem Description Now given two kinds of coins A and B,which satisfy that GCD(A,B)=1.Here you can ...
- A Simple Problem with Integers 循环节 修改 平方 找规律 线段树
A Simple Problem with Integers 这个题目首先要打表找规律,这个对2018取模最后都会进入一个循环节,这个循环节的打表要用到龟兔赛跑. 龟兔赛跑算法 floyed判环算法 ...
- 杭电多校第十场 hdu6432 Cyclic 打表找规律
Cyclic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)Total Su ...
- Problem E. Matrix from Arrays(杭电2018年多校第四场+思维+打表找循环节)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6336 题目: 题意:给你一个l个元素的数组a,用题目中的程序构造一个新的矩阵,询问q次,问以(x1,y ...
- 杭电oj 1016 Prime Ring Problem
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu6373 Pinball 杭电第六场 物理知识
Pinball Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total S ...
- 杭电第六场 hdu6362 oval-and-rectangle 积分求期望
oval-and-rectangle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu6354 杭电第五场 Everything Has Changed 计算几何
Everything Has Changed Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java ...
随机推荐
- codeforces 327 A Ciel and Dancing
题目链接 给你一串只有0和1的数字,然后对某一区间的数翻转1次(0变1 1变0),只翻转一次而且不能不翻转,然后让你计算最多可能出现多少个1. 这里要注意很多细节 比如全为1,要求必须翻转,这时候我们 ...
- UE4 坐标系 坐标轴旋转轴
Pitch是围绕Y轴旋转,也叫做俯仰角. Yaw是围绕Z轴旋转,也叫偏航角. Roll是围绕X轴旋转,也叫翻滚角. UE4里,蓝图中的rotation的三个依次为roll,pitch,yaw.C++中 ...
- 微服务之springboot 自定义配置(一)Application配置文件
配置的文件的格式 springboot可以识别两种格式的配置文件,分别是yml和properties 文件.我们可以将application.properties文件换成application.yml ...
- 进军pc市场 华为剑走偏锋可有戏?
尽管官方并未正式公布,但在前段时间,华为将要进军PC市场的消息在业内传得沸沸扬扬,据知情人士曝料,其第一款个人电脑将在今年4月上线.而华为将进军PC市场的消息,对其他智能手机厂商来说又意味着什么呢? ...
- 基于Spring注解的上下文初始化过程源码解析(二)
上一篇看完了register方法的代码,继续跟后面代码 后面执行refresh方法,代码清单如下: public void refresh() throws BeansException, Illeg ...
- [NUnit] discover test finished: 0 found issue
%Temp%\VisualStudioTestExplorerExtensions & restart visual studio
- 阿里P8Java大牛仅用46张图让你弄懂JVM的体系结构与GC调优。
本PPT从JVM体系结构概述.GC算法.Hotspot内存管理.Hotspot垃圾回收器.调优和监控工具六大方面进行讲述.图文并茂不生枯燥. 此PPT长达46页,全部展示篇幅过长,本文优先分享前十六页 ...
- LayDate使用
layDate非常愿意和您成为工作伙伴.她致力于成为全球最用心的web日期支撑,为国内外所有从事web应用开发的同仁提供力所能及的动力.她基于原生JavaScript精心雕琢,兼容了包括IE6在内的所 ...
- k8s学习笔记
9.deployment:声明式的升级应用 9.1.使用RC实现滚动升级 #kubectl rolling-update kubia-v1 kubia-v2 --image=luksa/kubia:v ...
- React中控制台警告
1.dll_lib.js:1 Warning: bind(): You are binding a component method to the component. React does this ...