AcWing 796. 子矩阵的和 (Prefix Sum) Describted by Java

Today train Prefix sum , two dimensional is a little complex than one dimensional,but it is not a big deal.
The problem:
输入一个n行m列的整数矩阵,再输入q个询问,每个询问包含四个整数x1, y1, x2, y2,表示一个子矩阵的左上角坐标和右下角坐标。

对于每个询问输出子矩阵中所有数的和。

输入格式
第一行包含三个整数n,m,q。

接下来n行,每行包含m个整数,表示整数矩阵。

接下来q行,每行包含四个整数x1, y1, x2, y2,表示一组询问。

输出格式
共q行,每行输出一个询问的结果。

数据范围
1≤n,m≤1000,
1≤q≤200000,
1≤x1≤x2≤n,
1≤y1≤y2≤m,
−1000≤矩阵内元素的值≤1000
输入样例:
3 4 3
1 7 2 4
3 6 2 8
2 1 2 3
1 1 2 2
2 1 3 4
1 3 3 4
输出样例:
17
27
21


First introduce the Prefix
We had an array : a[0] , a[1] ,…, a[i]
then we need to get the sum of a[Left] + a[Left + 1] + … + a[Right - 1] + a[Right]
We can make a new array called S[0] , S[1] , … , S[i] which
S[1] = a[1] ,
S[2] = a[1] + a[2],

S[i] = a[1] + a[2] + … + a[i - 1] + a[i]
This is Prefix Sum.
Cause if we use for to calculate sum of [L, R] , the Time Complexity is O(n)
use Prefix Sum , it is O(1).
Belows gives the process of calculation.

We always used to let S[0] = 0 to avoid border situation

The step:
在这里插入图片描述
The result :
在这里插入图片描述
And the Code :

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

public class Main {
    static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter pw = new PrintWriter(System.out);
    static int nums[][], S[][], n, m, q;

    public static void main(String[] args) throws Exception {
        st.nextToken();
        n = (int) st.nval;
        st.nextToken();
        m = (int) st.nval;
        st.nextToken();
        q = (int) st.nval;
        nums = new int[n + 1][m + 1];
        S = new int[n + 1][m + 1];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                st.nextToken();
                nums[i][j] = (int) st.nval;
                S[i][j] = S[i - 1][j] + S[i][j - 1] - S[i - 1][j - 1] + nums[i][j];
            }
        }
        while (q-- > 0) {
            st.nextToken();
            int x1 = (int) st.nval;
            st.nextToken();
            int y1 = (int) st.nval;
            st.nextToken();
            int x2 = (int) st.nval;
            st.nextToken();
            int y2 = (int) st.nval;
            pw.println(S[x2][y2] - S[x1 - 1][y2] - S[x2][y1 - 1] + S[x1 - 1][y1 - 1]);
            pw.flush();
        }
    }
}

The last , giving the one- dimensional situation.(thats very simple)
The Problem :
输入一个长度为n的整数序列。

接下来再输入m个询问,每个询问输入一对l, r。

对于每个询问,输出原序列中从第l个数到第r个数的和。

输入格式
第一行包含两个整数n和m。

第二行包含n个整数,表示整数数列。

接下来m行,每行包含两个整数l和r,表示一个询问的区间范围。

输出格式
共m行,每行输出一个询问的结果。

数据范围
1≤l≤r≤n,
1≤n,m≤100000,
−1000≤数列中元素的值≤1000
输入样例:
5 3
2 1 3 6 4
1 2
1 3
2 4
输出样例:
3
6
10


The Code :

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;

public class Main {
    static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter pw = new PrintWriter(System.out);
    static int nums[], S[], n, q;

    public static void main(String[] args) throws Exception {
        st.nextToken();
        n = (int) st.nval;
        st.nextToken();
        q = (int) st.nval;
        nums = new int[n + 1];
        S = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            st.nextToken();
            nums[i] = (int) st.nval;
            S[i] = S[i - 1] + nums[i];
        }

        while (q-- > 0) {
            st.nextToken();
            int l = (int) st.nval;
            st.nextToken();
            int r = (int) st.nval;
            pw.println(S[r] - S[l - 1] + " ");
            pw.flush();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值