E - Evaluate Matrix Sum

Description

Given a matrix, the elements of which are all integer number from 0 to 50, you are required to evaluate the square sum of its specified sub-matrix.


Input

The first line of the input contains a single integer T (1 <= T <= 5), the number of test cases.

For each test case, the first line contains two integers m and n (1
<= m, n <= 500), which are the row and column sizes of the matrix,
respectively. The next m lines with n numbers each gives the elements
of the matrix.

The next line contains a single integer N (1
<= N <= 100,000), the number of queries. The next N lines give one
query on each line, with four integers r1, c1, r2, c2 (1 <= r1 <=
r2 <= m, 1 <= c1 <= c2 <= n), which are the indices of the
upper-left corner and lower-right corner of the sub-matrix in question.


Output

For each test case, first print the number of the test case, then N
lines with one number on each line, the required square sum. Refer to
the sample output for details.


Sample Input

2
2 3
1 2 3
4 5 6
2
1 1 2 2
1 3 2 3
3 3
4 2 3
2 5 1
7 9 2
1
1 1 3 3


Sample Output

Case 1:
46
45
Case 2:
193
题意:给一个n*m矩阵  下表(1,1)~~~(n,m)给两个点做对角线   求中间矩阵的值朴素算法也可以过   但是这个在不断输入的同时也将值存在数组中   查询更快
 #include <iostream>
 #include <string.h>
 #include <stdio.h>

 using namespace std;
 int map[505][505];

 int main()
 {
     int t;
     int n,m;
     int k;
     int tt,x1,x2,y1,y2;
     int num;
     scanf("%d",&t);
     for(num=1;num<=t;num++)
     {
         scanf("%d%d",&n,&m);
         memset(map,0,sizeof(map));
         for(int i=1;i<=n;i++)
         {
             for(int j=1;j<=m;j++)
             {
                 scanf("%d",&k);
                 map[i][j]=k*k+map[i-1][j]+map[i][j-1]-map[i-1][j-1];
             }
         }
         printf("Case %d:\n",num);
         scanf("%d",&tt);
         while(tt--)
         {
             scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
             int ans=map[x1-1][y1-1]+map[x2][y2]-map[x1-1][y2]-map[x2][y1-1];
             printf("%d\n",ans);

         }
     }
     return 0;
 }

E - Evaluate Matrix Sum

时间: 2024-10-12 14:56:23

E - Evaluate Matrix Sum的相关文章

ACdream-1171 Matrix sum, 最大费用最大流

Matrix sum Time Limit: 8000/4000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description sweet和zero在玩矩阵游戏,sweet画了一个N * M的矩阵,矩阵的每个格子有一个整数.zero给出N个数Ki,和M个数Kj,zero要求sweet选出一些数,满足从第 i 行至少选出了Ki个数,第j列至少选出了K

Acdream 1171 Matrix sum 上下界费用流

题目链接:点击打开链接 Matrix sum Time Limit: 8000/4000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem Problem Description sweet和zero在玩矩阵游戏,sweet画了一个N * M的矩阵,矩阵的每个格子有一个整数.zero给出N个数Ki,和M个数Kj,zero要求sweet选出一些数,满足从第 i 行至少选出了Ki

HihoCoder - 1336 Matrix Sum

You are given an N × N matrix. At the beginning every element is 0. Write a program supporting 2 operations: 1. Add x y value: Add value to the element Axy. (Subscripts starts from 0 2. Sum x1 y1 x2 y1: Return the sum of every element Axy for x1 ≤ x

POJ 3233 Matrix Power Series

矩阵快速幂+二分求前n项和 矩阵快速幂是有模板的,多做几道题就会理解,前提是要会快速幂取模: 之所以用二分是因为求和的过程:A^1+A^2...+A^(k-1)+A^k,   k是1e9的,所以暴力求和肯定会TLE,在网上找到 了二分求矩阵和的方法: 公式为  (1+A^(k/2))*(A+A^2+..+A^k/2)   的,所以可以写成二分递归,如果k为奇数的话,sum就加上A^k(k为当 前的k值,不再是最初的值),反正是个公式,你要不信的话可以证明一下,所以就贴代码了,感觉到姿势不够优美

POJ3233:Matrix Power Series(矩阵快速幂+二分)

http://poj.org/problem?id=3233 题目大意:给定矩阵A,求A + A^2 + A^3 + … + A^k的结果(两个矩阵相加就是对应位置分别相加).输出的数据mod m.k<=10^9.这道题两次二分,相当经典.首先我们知道,A^i可以二分求出.然后我们需要对整个题目的数据规模k进行二分.比如,当k=6时,有:A + A^2 + A^3 + A^4 + A^5 + A^6 =(A + A^2 + A^3) + A^3*(A + A^2 + A^3)应用这个式子后,规模

poj 3233 Matrix Power Series(等比矩阵求和)

http://poj.org/problem?id=3233 ps转: 用二分方法求等比数列前n项和:即 原理: (1)若n==0 (2)若n%2==0     (3)若n%2==1 代码如下: LL sum(LL p,LL n) { if(n==0) return 1; if(n&1) return (1+pow(p,(n>>1)+1))*sum(p,n>>1); else return (1+pow(p,(n>>1)+1))*sum(p,(n-1)>&

poj Matrix Power Series 矩阵幂求和

题意:给一个n*n的矩阵A,求k次幂之和 S = A + A2 + A3 + … + Ak 思路:矩阵快速幂. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef struct node { int matrix[55][55]; }Matrix; Matrix a,sa,unit; int n,m,k,i,j; Matrix add(Matrix a,M

C++题解:Matrix Power Series ——矩阵套矩阵的矩阵加速

Matrix Power Series r时间限制: 1 Sec 内存限制: 512 MB 题目描述 给定矩阵A,求矩阵S=A^1+A^2+--+A^k,输出矩阵,S矩阵中每个元都要模m. 数据范围: n (n ≤ 30) , k (k ≤ 109) ,m (m < 104) 输入 输入三个正整数n,k,m 输出 输出矩阵S mod m 样例输入 2 2 4 0 1 1 1 样例输出 1 2 2 3 这道题不多说,可以得出加速矩阵(E为单位矩阵,也就是形为\(\begin{bmatrix}1&

Python练习题 048:Project Euler 021:10000以内所有亲和数之和

本题来自 Project Euler 第21题:https://projecteuler.net/problem=21 ''' Project Euler: Problem 21: Amicable numbers Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b