import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        // Ler as dimensões da matriz
        System.out.print("Digite o número de linhas (m): ");
        int m = sc.nextInt();
        System.out.print("Digite o número de colunas (n): ");
        int n = sc.nextInt();

        // Ler a matriz
        int[][] matriz = new int[m][n];
        System.out.println("Digite a matriz:");
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                matriz[i][j] = sc.nextInt();
            }
        }

        // Ler o número de consultas
        System.out.print("Digite o número de consultas (q): ");
        int q = sc.nextInt();

        // Processar cada consulta
        for (int query = 0; query < q; query++) {
            System.out.println("Digite os valores x1, y1, x2, y2 para a consulta:");
            int x1 = sc.nextInt();
            int y1 = sc.nextInt();
            int x2 = sc.nextInt();
            int y2 = sc.nextInt();

            // Calcular a soma dos elementos na submatriz definida por (x1, y1) e (x2, y2)
            int soma = 0;
            for (int i = x1; i <= x2; i++) {
                for (int j = y1; j <= y2; j++) {
                    soma += matriz[i][j];
                }
            }

            // Imprimir o resultado da consulta
            System.out.println("Soma da submatriz: " + soma);
        }

        sc.close();
    }
}

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: