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

#SRD0000028. Nghịch thế trong đoạn tĩnh (Static Range Inversions Query)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Nghịch thế trong đoạn tĩnh (Static Range Inversions Query)

Static Range Inversions Query

Source: Library Checker

Version: Phuoc Hung OJ Extended

Problem Statement

Given an immutable nonnegative array A0,…,AN−1A_0,\ldots,A_{N-1}, answer range inversion queries. For each half-open interval [l,r)[l,r), count pairs l≤i<j<rl\le i<j<r for which Ai>AjA_i>A_j. Equal values are not inversions.

Input

The first line contains N,QN,Q, followed by the NN array values. Each of the following QQ lines contains l,rl,r.

Output

Print each query answer as a 64-bit integer, in input order.

Subtasks

  • Subtask 1 (10 points): 1≤N≤601\le N\le 60; 1≤Q≤801\le Q\le 80; 0≤Ai≤1090\le A_i\le10^9; 0≤l<r≤N0\le l<r\le N. There are no updates.
  • Subtask 2 (20 points): 1≤N≤20001\le N\le 2000; 1≤Q≤20001\le Q\le 2000; 0≤Ai≤1090\le A_i\le10^9; 0≤l<r≤N0\le l<r\le N. There are no updates.
  • Subtask 3 (30 points): 1≤N≤200001\le N\le 20000; 1≤Q≤200001\le Q\le 20000; 0≤Ai≤1090\le A_i\le10^9; 0≤l<r≤N0\le l<r\le N. There are no updates.
  • Subtask 4 (40 points): 1≤N≤1000001\le N\le 100000; 1≤Q≤1000001\le Q\le 100000; 0≤Ai≤1090\le A_i\le10^9; 0≤l<r≤N0\le l<r\le N. There are no updates.

Examples

Input

4 2
4 1 4 0
1 3
0 4

Output

0
4

Explanation

The subarray [1,3)=[1,4][1,3)=[1,4] has no inversion. The whole array [4,1,4,0][4,1,4,0] has four: index pairs (0,1)(0,1), (0,3)(0,3), (1,3)(1,3), and (2,3)(2,3).