【hdu 6336】 Matrix from Arrays
【链接】 我是链接,点我呀:)
【题意】
在这里输入题意
【题解】
找个规律会发现
M[i][j] = M[i-2*L][j] = M[i][j-2*L]
先预处理出来(1,1)-(2L,2L)这个矩阵以及他的二维前缀和
那么对于要求的(x0,y0)-(x1,y1)这个矩阵。
可以用若干个(1,1)-(x,y)这样的前缀矩阵通过加加减减算出来。
对于(1,1)-(x,y)这样的矩阵。
显然是由若干个(1,1)-(2L,2L)矩阵合并而成的(x/L)*(y/L)个。
多余的部分(下边,右下角以及右边)也能很快的求得。(利用(1,1)-(2L,2L)这个矩阵的前缀和
【代码】
#include <bits/stdc++.h>
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define all(x) x.begin(),x.end()
#define pb push_back
#define lson l,mid,rt<<1
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define res(x) scanf("%s",x)
#define rson mid+1,r,rt<<1|1
using namespace std;
const double pi = acos(-1);
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};
const int MAXL = 10;
int L,A[MAXL+10],M[MAXL*2+10][MAXL*2+10];
void creat_M(){
int cursor = 0,cnt = 0;
for (int i = 0;;++i){
for (int j = 0;j <= i;j++){
int x = j+1,y = i-j+1;
if (x>=1 && x<= 2*L && y>=1 && y<=2*L) {
M[x][y] = A[cursor];
cnt++;
if (cnt==4*L*L){
return;
}
}
cursor = (cursor + 1)%L;
}
}
}
LL calc(int x,int y){
LL temp1 = 1LL*M[L][L]*(x/L)*(y/L);
LL temp2 = 1LL*M[x%L][L]*(y/L);
LL temp3 = 1LL*M[L][y%L]*(x/L);
LL temp4 = M[x%L][y%L];
return temp1+temp2+temp3+temp4;
}
int main(){
#ifdef LOCAL_DEFINE
freopen("rush_in.txt", "r", stdin);
#endif
ios::sync_with_stdio(0),cin.tie(0);
int T;
cin >> T;
while (T--){
cin >> L;
rep1(i,0,L-1) cin >> A[i];
memset(M,0,sizeof M);
creat_M();
rep1(i,1,2*L)
rep1(j,1,2*L)
M[i][j] = M[i][j] + M[i-1][j] + M[i][j-1] -M[i-1][j-1];
L*=2;
int Q;
cin >> Q;
while (Q--){
int xx0,yy0,xx1,yy1;
cin >> xx0 >> yy0 >> xx1 >> yy1;
xx0++;yy0++;xx1++;yy1++;
LL ans = calc(xx1,yy1)-calc(xx0-1,yy1)-calc(xx1,yy0-1)+calc(xx0-1,yy0-1);
cout<<ans<<endl;
}
}
return 0;
}
【hdu 6336】 Matrix from Arrays的更多相关文章
- 【数位dp】【HDU 3555】【HDU 2089】数位DP入门题
[HDU 3555]原题直通车: 代码: // 31MS 900K 909 B G++ #include<iostream> #include<cstdio> #includ ...
- 【HDU 5647】DZY Loves Connecting(树DP)
pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- 【HDU 2196】 Computer(树的直径)
[HDU 2196] Computer(树的直径) 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 这题可以用树形DP解决,自然也可以用最直观的方法解 ...
- 【HDU 2196】 Computer (树形DP)
[HDU 2196] Computer 题链http://acm.hdu.edu.cn/showproblem.php?pid=2196 刘汝佳<算法竞赛入门经典>P282页留下了这个问题 ...
- 【HDU 5145】 NPY and girls(组合+莫队)
pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...
- 【HDU 5015】233 Matrix
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5015 [算法] 矩阵乘法 [代码] #include<bits/stdc++.h> u ...
- 【NOIP模拟】matrix(简化矩阵)
题目背景 SOURCE:NOIP2016-RZZ-1 题目描述 给出两个 N×N 的矩阵 A.B,矩阵每行每列标号 0-N-1 .定义这两个矩阵的乘积 AB 为
- 【POJ 3233】Matrix Power Series
[题目链接] 点击打开链接 [算法] 要求 A^1 + A^2 + A^3 + ... + A^k 考虑通过二分来计算这个式子 : 令f(k) = A^1 + A^2 + A ^ 3 + ... + ...
随机推荐
- AlterDialog 经常使用的样式
使用AlerDialog 创建对话框 : AlertDialog.Builder builder = new AlertDialog.Builder(this); 1.设置简单的对话框 builder ...
- 150723培训心得(queue)
queue(STL中函数,就是指队列) #include <iostream> #include <queue> using namespace std; //这 ...
- Linux - 理不清的权限chmod与chown区别
chmod是修改第一列内容的 ,chown是修改第3,4列内容的. [root@local ~]# chmod 777 -R add.sh [root@local ~]# chown jiqing:j ...
- Windows下Vim主题变更
默认的好丑! 主题位置. 修改配置文件. 添加主题设置. 新的主题,很高端大气. set fileencodings=utf8,ucs-bom,cp936,big set fileencoding=u ...
- 2-1 Restful中HTTP协议介绍
Restful是一种基于资源的软件架构风格,所以从定义上来说是跟HTTP无关的.但是本课程提到的Restful API是基于HTTP协议的一种实现.所有相关知识都是基于现有的HTTP协议而来,并没有对 ...
- 一起学Android之Fragment
概述 本文以一个简单的小例子,简述在Android开发中,Fragment的常见用法,仅供学习分享使用,如有不足之处,还请指正. 什么是Fragment? Fragment代表一个功能或者用户界面的一 ...
- redis启动加载过程、数据持久化
背景 公司一年的部分业务数据放在redis服务器上,但数据量比较大,单纯的string类型数据一年就将近32G,而且是经过压缩后的. 所以我在想能否通过获取string数据的时间改为保存list数据类 ...
- 关于原生app、webApp、混合app的介绍
WebApp 原生App(Native App) 混合App(hybrid App) webApp: 用html5,css3 js开发的网页,运行在移动端的浏览器 zepto.angular.vue. ...
- 文件被占用导致Hive Load文件不成功
用Python写了个用LOAD命令将文件导入Hive的程序,开始代码写成下面这样: def loadToHive(bakFilePath, tbName): try: transport = TSoc ...
- Laravel 查询某天数据 whereDate