Problem:
Score : 400 points

Problem Statement

One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from his friend.

Find the following:

Constraints

1 ≦ N ≦ 200,000

(a_1, a_2, ..., a_N) is a permutation of (1, 2, ..., N).

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the answer.

Note that the answer may not fit into a 32-bit integer.

Sample Input 1

3
2 1 3

Sample Output 1

9

Sample Input 2

4
1 3 2 4

Sample Output 2

19

Sample Input 3

8
5 4 8 1 2 6 7 3

Sample Output 3

85

Solution in Go (1.6) (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}
	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	seg := Segment{}
	seg.init(n)
	aa := make([]int, n)
	for i := 0; i < n; i++ {
		aa[getNextInt(scanner)-1] = i
	}
	var ans int64
	for i := 0; i < n; i++ {
		seg.add(aa[i], 1)
		l := seg.nextLeft(aa[i])
		r := seg.nextRight(aa[i])
		ans += int64(aa[i]-l) * int64(r-aa[i]) * int64(i+1)

	}

	fmt.Fprintln(writer, ans)
	writer.Flush()
}

// Segment ...
type Segment struct {
	n, h, i int
	unit    []int
	bucket  [][]int
}

func (seg *Segment) init(n int) {
	seg.n = n
	seg.unit = make([]int, 1)
	seg.unit[0] = 1
	seg.bucket = make([][]int, 1)
	seg.bucket[0] = make([]int, n)

	chunk := 2
	i := 0
	for n > chunk+1 {
		n = (n-1)/chunk + 1
		seg.bucket = append(seg.bucket, make([]int, n))
		seg.unit = append(seg.unit, seg.unit[i]*chunk)
		i++
	}
	seg.h = len(seg.unit)
}

func (seg *Segment) add(index, value int) {
	for i := 0; i < seg.h; i++ {
		seg.bucket[i][index/seg.unit[i]] += value
	}
}

func (seg *Segment) minimize(index int) {
	raw := seg.bucket[0]
	for h := 1; h < seg.h; h++ {
		if seg.bucket[h][index/seg.unit[h]] > raw[index] {
			seg.bucket[h][index/seg.unit[h]] = raw[index]
		}
	}
}

func (seg *Segment) min(l, r int) int {
	if seg.n < r {
		r = seg.n
	}
	mn := math.MaxInt32
	for index := l; index < r; index += seg.unit[seg.i] {
		for seg.i = 0; seg.i < seg.h-1 && index%seg.unit[seg.i+1] == 0; seg.i++ {
			if index+seg.unit[seg.i+1] >= r {
				break
			}
		}
		if mn > seg.bucket[seg.i][index/seg.unit[seg.i]] {
			mn = seg.bucket[seg.i][index/seg.unit[seg.i]]
		}
	}

	return mn
}

func (seg *Segment) nextLeft(index int) int {
	raw := seg.bucket[0]
	for index = index - 1; index >= 0; index -= seg.unit[seg.i] {
		if raw[index] > 0 {
			return index
		}
		for seg.i = 0; seg.i < seg.h-1 && (index+1)%seg.unit[seg.i+1] == 0; seg.i++ {
			if seg.bucket[seg.i+1][index/seg.unit[seg.i+1]] != 0 {
				break
			}
		}
	}

	return -1
}

func (seg *Segment) nextRight(index int) int {
	raw := seg.bucket[0]
	for index = index + 1; index < seg.n; index += seg.unit[seg.i] {
		if raw[index] > 0 {
			return index
		}
		for seg.i = 0; seg.i < seg.h-1 && index%seg.unit[seg.i+1] == 0; seg.i++ {
			if seg.bucket[seg.i+1][index/seg.unit[seg.i+1]] != 0 {
				break
			}
		}
	}

	return seg.n
}