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

#PS0000294. Tổng tích mọi cặp trong đoạn (Sum of Pair Products in a Range)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tổng tích mọi cặp trong đoạn (Sum of Pair Products in a Range)

Sum of Pair Products in a Range

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

For each inclusive range [l,r][l,r], compute ∑l≤i<j≤raiaj\sum_{l\le i<j\le r}a_i a_j. Count each unordered pair of distinct indices once. A singleton gives zero. Print the exact integer, without a modulus.

Input

First line: n,qn,q. Second line: nn array elements. Each of the next qq lines contains l,rl,r.

Output

Print each answer on its own line.

Subtasks

  • Subtask 1 (20 points): 1≤n,q≤2000001\le n,q\le200000, ∣ai∣≤1000000000|a_i|\le1000000000, 1≤l≤r≤n1\le l\le r\le n. Additionally: n≤100,q≤100n\le100, q\le100.
  • Subtask 2 (30 points): 1≤n,q≤2000001\le n,q\le200000, ∣ai∣≤1000000000|a_i|\le1000000000, 1≤l≤r≤n1\le l\le r\le n. Additionally: ∣ai∣≤100|a_i|\le100.
  • Subtask 3 (50 points): 1≤n,q≤2000001\le n,q\le200000, ∣ai∣≤1000000000|a_i|\le1000000000, 1≤l≤r≤n1\le l\le r\le n.

Example 1

Input

3 2
1 2 3
1 3
2 2

Output

11
0

Explanation

The three pairs give 2+3+6=11. A singleton has no pair.

Example 2

Input

3 2
-3 2 -1
1 3
1 2

Output

-5
-6

Explanation

The three products -6, 3, -2 sum to -5; the second range gives -6.