Prefix sum and difference

Keywords: Algorithm

Prefix and

Enter a sequence of integers of length n.

Next, enter m more queries, and enter a pair of l,r for each query.

For each query, the sum of the number l to the number r in the original sequence is output.

Input format

The first line contains two integers n and m.

The second row contains n integers, representing the integer sequence.

The next m lines, each containing two integers l and r, represent the range of a query.

Output format

There are m lines in total, and each line outputs a query result.

Data range

1≤l≤r≤n,
1≤n,m≤100000,
− 1000 ≤ the value of the element in the sequence ≤ 1000

Input example:

5 3
2 1 3 6 4
1 2
1 3
2 4

Output example:

3
6
10

answer:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws Exception{
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String[] str = bufferedReader.readLine().split(" ");
        int n = Integer.parseInt(str[0]);
        int m = Integer.parseInt(str[1]);
        str = bufferedReader.readLine().split(" ");
        int[] a = new int[n];
        int[] c = new int[n+1];
        c[0] = 0;
        for (int i=0;i<n;i++){
            a[i] = Integer.parseInt(str[i]);
            c[i+1] = c[i]+a[i];
        }
        while (m-->0){
            str = bufferedReader.readLine().split(" ");
            int l = Integer.parseInt(str[0]);
            int r = Integer.parseInt(str[1]);
            System.out.println(c[r]-c[l-1]);
        }
    }
}

Example:

Sum of submatrix

Enter an integer matrix with n rows and m columns, and then enter q queries. Each query contains four integers x1, Y1, X2 and Y2, representing the upper left corner coordinates and lower right corner coordinates of a sub matrix.

For each query, the sum of all numbers in the output submatrix.

Input format

The first line contains three integers n, m, q.

Next, n rows, each containing m integers, represent the integer matrix.

The next q line contains four integers x1, Y1, X2 and Y2, representing a set of queries.

Output format

There are q rows in total, and each row outputs a query result.

Data range

1≤n,m≤1000,
1≤q≤200000,
1≤x1≤x2≤n,
1≤y1≤y2≤m,
− 1000 ≤ value of elements in the matrix ≤ 1000

Input example:

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

Output example:

17
27
21

answer:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws Exception{
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String[] str = bufferedReader.readLine().split(" ");
        int n = Integer.parseInt(str[0]);
        int m = Integer.parseInt(str[1]);
        int q = Integer.parseInt(str[2]);
        int[][] a = new int[n][m];
        int[][] c = new int[n+1][m+1];
        c[0][0] = 0;
        for (int i=0;i<n;i++){
            str = bufferedReader.readLine().split(" ");
            for (int j=0;j<m;j++){
                a[i][j] = Integer.parseInt(str[j]);
                c[i+1][j+1] = c[i][j+1]+c[i+1][j]-c[i][j]+a[i][j];
            }
        }
        while (q-->0){
            str = bufferedReader.readLine().split(" ");
            int x1 = Integer.parseInt(str[0]);
            int y1 = Integer.parseInt(str[1]);
            int x2 = Integer.parseInt(str[2]);
            int y2 = Integer.parseInt(str[3]);
            System.out.println(c[x2][y2]-c[x2][y1-1]-c[x1-1][y2]+c[x1-1][y1-1]);
        }
    }
}

Difference

Enter a sequence of integers of length n.

Next, enter m operations. Each operation contains three integers l,r,c, which means adding c to each number between [l,r] in the sequence.

Please output the sequence after all operations.

Input format

The first line contains two integers n and m.

The second line contains n integers, representing the sequence of integers.

The next m lines, each containing three integers l, r, c, represent an operation.

Output format

A total of one line, including n integers, representing the final sequence.

Data range

1≤n,m≤100000,
1≤l≤r≤n,
−1000≤c≤1000,
− 1000 ≤ value of element in integer sequence ≤ 1000

Input example:

6 3
1 2 2 1 2 1
1 3 1
3 5 1
1 6 1

Output example:

3 4 5 3 4 2

answer:

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {

    static int[] a;
    static int[] b;

    public static void fun(int l, int r, int c){
        b[l]+=c;
        b[r+1]-=c;
    }

    public static void main(String[] args) throws Exception{
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String[] str = bufferedReader.readLine().split(" ");
        int n = Integer.parseInt(str[0]);
        int m = Integer.parseInt(str[1]);
        a = new int[n+2];
        b = new int[n+2];
        str = bufferedReader.readLine().split(" ");
        for (int i=1;i<=n;i++){
            a[i] = Integer.parseInt(str[i-1]);
            fun(i,i,a[i]);
        }
        while (m-->0){
            str = bufferedReader.readLine().split(" ");
            int l = Integer.parseInt(str[0]);
            int r = Integer.parseInt(str[1]);
            int c = Integer.parseInt(str[2]);
            fun(l,r,c);
        }
        for (int i=1;i<=n;i++)
            a[i] = a[i-1]+b[i];
        for (int i=1;i<=n;i++)
            System.out.print(a[i]+" ");
        System.out.println();
    }
}

Examples

Difference matrix

Enter an integer matrix with n rows and m columns, and then enter q operations. Each operation contains five integers x1,y1,x2,y2,c, where (x1,y1) and (x2,y2) represent the upper left and lower right coordinates of a sub matrix.

Each operation adds c to the value of each element in the selected sub matrix.

Please output the matrix after all operations.

Input format

The first line contains integers n,m,q.

Next, n rows, each containing m integers, represent the integer matrix.

The next q line contains five integers x1,y1,x2,y2,c, representing an operation.

Output format

There are n rows in total, with m integers in each row, representing the final matrix after all operations are completed.

Data range

1≤n,m≤1000,
1≤q≤100000,
1≤x1≤x2≤n1,
1≤y1≤y2≤m,
−1000≤c≤1000,
− 1000 ≤ value of elements in the matrix ≤ 1000

Input example:

3 4 3
1 2 2 1
3 2 2 1
1 1 1 1
1 1 2 2 1
1 3 2 3 2
3 1 3 4 1

Output example:

2 3 4 1
4 3 4 1
2 2 2 2

answer:

package cn.smxy.ln3;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    static int[][] a;
    static int[][] c;
    public static void fun(int x1, int y1, int x2, int y2, int b){
        c[x1][y1]+=b;
        c[x2+1][y1]-=b;
        c[x1][y2+1]-=b;
        c[x2+1][y2+1]+=b;
    }

    public static void main(String[] args) throws Exception{
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        String[] str = bufferedReader.readLine().split(" ");
        int n = Integer.parseInt(str[0]);
        int m = Integer.parseInt(str[1]);
        int q = Integer.parseInt(str[2]);
        a = new int[n+2][m+2];
        c = new int[n+2][m+2];
        c[0][0] = 0;
        for (int i=1;i<=n;i++){
            str = bufferedReader.readLine().split(" ");
            for (int j=1;j<=m;j++){
                a[i][j] = Integer.parseInt(str[j-1]);
                fun(i,j,i,j,a[i][j]);
            }
        }
        while (q-->0){
            str = bufferedReader.readLine().split(" ");
            int x1 = Integer.parseInt(str[0]);
            int y1 = Integer.parseInt(str[1]);
            int x2 = Integer.parseInt(str[2]);
            int y2 = Integer.parseInt(str[3]);
            int b = Integer.parseInt(str[4]);
            fun(x1,y1,x2,y2,b);
        }
        for (int i=1;i<=n;i++)
            for (int j=1;j<=m;j++)
                c[i][j] += c[i-1][j]+c[i][j-1]-c[i-1][j-1];
        for (int i=1;i<=n;i++) {
            for (int j = 1; j <= m; j++)
                System.out.print(c[i][j]+" ");
            System.out.println();
        }

    }
}


Posted by razta on Tue, 26 Oct 2021 08:14:35 -0700