PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000101. Tổng đoạn trên mảng bất biến (Range Sum Query - Immutable)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng đoạn trên mảng bất biến (Range Sum Query - Immutable)

Range Sum Query - Immutable

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

An immutable array of nn integers a0,a1,…,an−1a_0,a_1,\ldots,a_{n-1} is indexed from zero. For each query (l,r)(l,r), find the sum of the elements at indices ll through rr, inclusive.

Input

The first line contains n,qn,q, the array length and number of queries.

The second line contains nn integers a0,…,an−1a_0,\ldots,a_{n-1}.

Each of the next qq lines contains l,rl,r.

Output

Print one integer sum per query, in the given order.

Subtasks

  • Subtask 1 (20 points): 1≤n≤10001\le n\le 1000; 1≤q≤10001\le q\le 1000; −100000≤ai≤100000-100000\le a_i\le 100000 for all 0≤i<n0\le i<n; 0≤l≤r<n0\le l\le r<n.
  • Subtask 2 (30 points): 1≤n≤100001\le n\le 10000; 1≤q≤100001\le q\le 10000; −100000≤ai≤100000-100000\le a_i\le 100000 for all 0≤i<n0\le i<n; 0≤l≤r<n0\le l\le r<n; r−l+1≤10r-l+1\le 10.
  • Subtask 3 (50 points): 1≤n≤100001\le n\le 10000; 1≤q≤100001\le q\le 10000; −100000≤ai≤100000-100000\le a_i\le 100000 for all 0≤i<n0\le i<n; 0≤l≤r<n0\le l\le r<n.

Examples

Input

6 3
-2 0 3 -5 2 -1
0 2
2 5
0 5

Output

1
-1
-3

Explanation

The six elements have indices 0 through 5. Query [0,2][0,2] gives −2+0+3=1-2+0+3=1. Query [2,5][2,5] gives 3−5+2−1=−13-5+2-1=-1. Query [0,5][0,5] covers the entire array, with sum −3-3.