import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), q = in.nextInt();
long[] arr = new long[n + 1];
long[] dp = new long[n + 1];
for(int i = 1; i <= n; i++) arr[i] = in.nextInt();
dp[0] = 0; dp[1] = arr[1];
for(int i = 2; i <= n; i++) dp[i] += dp[i - 1] + arr[i];
for(int i = 0; i < q; i++) {
int left = in.nextInt(), right = in.nextInt();
System.out.println(dp[right] - dp[left - 1]);
}
}
}
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(), m = in.nextInt(), q = in.nextInt();
int[][] arr = new int[n + 1][m + 1];
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++) arr[i][j] = in.nextInt();
long[][] dp = new long[n + 1][m + 1];
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] + arr[i][j] - dp[i - 1][j - 1];
while(q > 0) {
int x1 = in.nextInt(), y1 = in.nextInt(), x2 = in.nextInt(), y2 = in.nextInt();
System.out.println(dp[x2][y2] - dp[x2][y1 - 1] - dp[x1 - 1][y2] + dp[x1 - 1][y1 - 1]);
q--;
}
}
}
class Solution {
public int pivotIndex(int[] nums) {
int n = nums.length;
int[] f = new int[n];
int[] g = new int[n];
for(int i = 1; i < n; i++) {
f[i] = f[i - 1] + nums[i - 1];
}
for(int i = n - 2; i >= 0; i--) {
g[i] = g[i + 1] + nums[i + 1];
}
for(int i = 0; i < n; i++) {
if(f[i] == g[i]) return i;
}
return -1;
}
}
238. 除自身以外数组的乘积 - 力扣(LeetCode)
class Solution {
public int[] productExceptSelf(int[] nums) {
int n = nums.length;
int[] ret = new int[n];
int[] f = new int[n];
int[] g = new int[n];
f[0] = 1; g[n - 1] = 1;
for(int i = 1; i < n; i++) {
f[i] = f[i - 1] * nums[i - 1];
}
for(int i = n - 2; i >= 0; i--) {
g[i] = g[i + 1] * nums[i + 1];
}
for(int i = 0; i < n; i++) {
ret[i] = f[i] * g[i];
}
return ret;
}
}
class Solution {
public int subarraySum(int[] nums, int k) {
Map<Integer, Integer> hash = new HashMap<>();
int sum = 0, ret = 0;
hash.put(0,1);
for(int x : nums) {
sum += x;
ret += hash.getOrDefault(sum - k, 0);
hash.put(sum, hash.getOrDefault(sum, 0) + 1);
}
return ret;
}
}
974. 和可被 K 整除的子数组 - 力扣(LeetCode)
class Solution {
public int subarraysDivByK(int[] nums, int k) {
Map<Integer, Integer> hash = new HashMap<>();
int sum = 0;
hash.put(0, 1);
int ret = 0;
for(int x : nums) {
sum += x;
int target = (sum % k + k) % k;
ret += hash.getOrDefault(target, 0);
hash.put(target, hash.getOrDefault(target, 0) + 1);
}
return ret;
}
}
class Solution {
public int findMaxLength(int[] nums) {
Map<Integer, Integer> hash = new HashMap<>();
int ret = 0, sum = 0;
hash.put(0, -1);
for(int i = 0; i < nums.length; i++) {
sum += nums[i] == 0 ? -1 : 1;
if(hash.containsKey(sum)) ret = Math.max(ret, i - hash.get(sum));
else hash.put(sum, i); //只保留第一次的, 不存在才要更新
}
return ret;
}
}
class Solution {
public int[][] matrixBlockSum(int[][] mat, int k) {
int n = mat.length, m = mat[0].length;
int[][] dp = new int[n + 1][m + 1];
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + mat[i - 1][j - 1];
int[][] ret = new int[n][m];
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++) {
int x1 = Math.max(0, i - k) + 1, x2 = Math.min(n - 1, i + k) + 1;
int y1 = Math.max(0, j - k) + 1, y2 = Math.min(m - 1, j + k) + 1;
ret[i][j] = dp[x2][y2] - dp[x2][y1 - 1] - dp[x1 - 1][y2] + dp[x1 - 1][y1 - 1];
}
return ret;
}
}