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

#TP0000056. Đếm cặp chỉ số thỏa chuỗi bất đẳng thức (Yet Another Problem About Pairs Satisfying an Inequality)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Đếm cặp chỉ số thỏa chuỗi bất đẳng thức (Yet Another Problem About Pairs Satisfying an Inequality)

Yet Another Problem About Pairs Satisfying an Inequality

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given an integer array a1,a2,…,ana_1,a_2,\ldots,a_n, with positions numbered from 11 to nn.

An ordered pair of indices (i,j)(i,j) qualifies if the four numbers aia_i, ii, aja_j, jj satisfy the strict chain

ai<i<aj<j.a_i<i<a_j<j.

Count the valid index pairs. Positions in this inequality are 1-based, not 0-based.

Input

  • The first line contains integer nn: the array length.
  • The second line contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n.

Output

Print one integer: the number of index pairs satisfying ai<i<aj<ja_i<i<a_j<j.

Subtasks

  • Subtask 1 (20 points): 2≤n≤252\le n\le 25; 0≤ai≤1090\le a_i\le10^9 for every ii.
  • Subtask 2 (30 points): 2≤n≤25002\le n\le 2500; 0≤ai≤1090\le a_i\le10^9 for every ii.
  • Subtask 3 (50 points): 2≤n≤2000002\le n\le 200000; 0≤ai≤1090\le a_i\le10^9 for every ii.

Examples

Input

8
1 1 2 3 8 2 1 4

Output

3

Explanation

The valid index pairs are (2,4)(2,4), (2,8)(2,8), and (3,8)(3,8). For instance, pair (2,8)(2,8) has a2=1a_2=1, a8=4a_8=4, and satisfies 1<2<4<81<2<4<8. No other pair satisfies all four strict inequalities, so print 33.