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 main()
 {
     int t;
     int n,m;
     int k;
     int tt,x1,x2,y1,y2;
     int num;
     int map[505][505];
     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;
 }
 
 
 
 
 
 
 
 #include<iostream>
#include <string.h>
#include <stdio.h> using namespace std; int a[ 510 ][ 510 ];
int sum[ 510 ][ 510 ]; int main()
{
    int t,n,m,tt;
    int ss;
    int k;
    int r1,c1,r2,c2;
    scanf("%d",&t);
    for(k=1; k<=t; k++)
    {
        scanf("%d%d",&n,&m);
        memset(sum,0,sizeof(sum));
        for(int i=1; i<=n; i++)
        {
            ss = 0;
            for(int j=1; j<=m; j++)
            {
                scanf("%d",&a[i][j]);
                ss += a[i][j] * a[i][j];
                sum[i][j] = sum[ i - 1 ][ j ] + ss;
            }
        }
        printf("Case %d:\n",k);
        scanf("%d",&tt);
        while(tt--)
        {
            scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
            printf("%d\n",sum[ r2 ][ c2 ] - sum[ r1-1 ][ c2 ] -  sum[ r2 ][ c1 - 1 ] + sum[ r1 - 1 ][ c1 - 1 ]  );
        }
    }
    return 0; }

E - Evaluate Matrix Sum的更多相关文章

  1. HihoCoder1336 Matrix Sum(二维树状数组求和)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You are given an N × N matrix. At the beginning every element ...

  2. hihocode 1336 Matrix Sum 【二维树状数组】

    题目 两个操作: 1. Add x y value: Add value to the element Axy. (Subscripts starts from 0 2. Sum x1 y1 x2 y ...

  3. 1336 : Matrix Sum (hihocoder)

    题目链接: 点击打开链接 二维树状数组,百度一大堆,我只是存代码的 #include<stdio.h> #include<iostream> #include<algor ...

  4. Matrix Sum HihoCoder - 1336 二维树状数组 感觉好像二维差分。

    #include<cstdio> #include<cstring> using namespace std; typedef long long ll; ; ; ll c[N ...

  5. Project Euler 345: Matrix Sum

    题目 思路: 将问题转化成最小费用流 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize(4) #incl ...

  6. UVa 11149 Power of Matrix(倍增法、矩阵快速幂)

    题目链接: 传送门 Power of Matrix Time Limit: 3000MS      Description 给一个n阶方阵,求A1+A2+A3+......Ak. 思路 A1+A2+. ...

  7. POJ 3233 Matrix Power Series(矩阵高速功率+二分法)

    职务地址:POJ 3233 题目大意:给定矩阵A,求A + A^2 + A^3 + - + A^k的结果(两个矩阵相加就是相应位置分别相加).输出的数据mod m. k<=10^9.     这 ...

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

    Matrix Power Series r时间限制: 1 Sec 内存限制: 512 MB 题目描述 给定矩阵A,求矩阵S=A^1+A^2+--+A^k,输出矩阵,S矩阵中每个元都要模m. 数据范围: ...

  9. POJ 3233 Matrix Power Series(二分等比求和)

    Matrix Power Series [题目链接]Matrix Power Series [题目类型]二分等比求和 &题解: 这题我原来用vector写的,总是超时,不知道为什么,之后就改用 ...

随机推荐

  1. MVC中利用knockout.js实现动态uniqueId

    题目比较拗口,但是这篇文章确实直说这一点. knockout.js是一个JS库,它的官网是http://knockoutjs.com/ 这篇文章的重点是knockout在工作的一个功能中的应用.最终效 ...

  2. day1-windows下python和selenium的安装

    这是一个完整的安装包,下载下来是一个.exe的文件 只需双击,下一步下一步默认安装即可 python从2.7开始都会携带pip插件,做了scripe的环境变量可以,在网络畅通的情况下可以在cmd的命令 ...

  3. C++ 的虚析构函数

    当一个基类的指针指向一个派生类的对象,并用该基类的指针去删除或者析构派生类对象时,如果基类的析构函数不是声明为虚函数,那么在析构时基类的析构函数将会被直接调用,派生类的析构函数应为没被调用而导致内存泄 ...

  4. jvm 基础

    1. JDK 包含 java 程序设计语言,JVM, Java API类库. java 开发最小环境 2. JRE : Java API类库中java se API 子集和java 虚拟机(HotSp ...

  5. Sophus libSophus.so

    在编译包含Sophus的源文件的时候,出现如下错误 ../lib/libmyslam.so: undefined reference to `Sophus::SO3::SO3(double, doub ...

  6. oracle导出expdp导入impdp

    conn sys/password as sysdba;创建用户test1CREATE USER test1 IDENTIFIED BY "pass1";GRANT CONNECT ...

  7. VMware 15 Pro密钥

    YG5H2-ANZ0H-M8ERY-TXZZZ-YKRV8 UG5J2-0ME12-M89WY-NPWXX-WQH88 UA5DR-2ZD4H-089FY-6YQ5T-YPRX6 GA590-86Y0 ...

  8. .net调用word转换pdf出现80080005错误的解决办法

    检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80080005. 1:在服务器上安装offi ...

  9. JS高级- OOP-ES5

    1. OOP 面向对象三大特点: 封装,继承,多态 封装: 问题: 构造函数可重用代码和结构定义,但无法节约内存 为什么: 放在构造函数内的方法定义,每new一次,都会反复创建副本——浪费内存 解决: ...

  10. c#中的as,is和强转

    as和强转之间的区别: as转换类型失败时不会抛出异常:强转类型失败时会抛出异常 引入is先对变量进行检验: if (foo is int) { i = (int)foo; } logger log ...