【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 + ... + ...
随机推荐
- 《C++ Primer Plus》学习笔记2
<C++ Primer Plus>学习笔记2 第五章 循环和关系表达式 ========================================================== ...
- jQuery必知要点(一)
1. jQuery框架的显著特点. jQuery强调的理念是写的少,做的多(write less.do more). 其主要特点有:轻量级.强大的选择器.美丽的DOM操作封装.可靠的事件处理机制.完好 ...
- hdu1209(Clock)
pid=1209">点击打开hdu1209 Problem Description There is an analog clock with two hands: an hour h ...
- 150723培训心得(queue)
queue(STL中函数,就是指队列) #include <iostream> #include <queue> using namespace std; //这 ...
- Java程序猿的JavaScript学习笔记(9—— jQuery工具方法)
计划按例如以下顺序完毕这篇笔记: Java程序猿的JavaScript学习笔记(1--理念) Java程序猿的JavaScript学习笔记(2--属性复制和继承) Java程序猿的JavaScript ...
- Swift3.0中关于日期类的使用指引
日期的处理在大大小小的iOS项目中都十分常见,随着Swift3.0正式版的即将推出,语法的改变让NSDate以及相关类的使用都与之前略有不同,这里将会对基于Swift3.0版本的NSDate及相关类的 ...
- 一起学Android之Fragment
概述 本文以一个简单的小例子,简述在Android开发中,Fragment的常见用法,仅供学习分享使用,如有不足之处,还请指正. 什么是Fragment? Fragment代表一个功能或者用户界面的一 ...
- Linux Shell Scripting Cookbook 读书笔记 7
ping, du, ps, kill, 收集系统信息 判断网络中哪些主机是活动主机 #!/bin/bash for ip in 10.215.70.{1..255}; do ( ping $ip -c ...
- map使用
// map使用 1 #include <iostream> #include "insertVal.h" #include "sort.h" us ...
- 爱,死亡和机器人 第十四集 齐马蓝 中文字幕(Python处理utf8文件获取想要的内容)
处理代码 file = "a.srt" fi = open(file, mode='r') a = fi.readline() i = 1 while len(str(a)) != ...