Interval Queries
题号:NC222407
时间限制:C/C++/Rust/Pascal 4秒,其他语言8秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
64bit IO Format: %lld

题目描述

Given a sequence of length and quries. For each query, three integers are given and you should calculate:

where .

输入描述:

The first line contains two integers , denoting the length of given sequence and the number of quries respectively.

The second line contains integers , denoting the given sequence.

Following lines each contains three integers , denoting each query.

It's guaranteed that .

输出描述:

Output  lines each containing one integer, denoting the answers to the queries.
示例1

输入

复制
5 2
2 4 1 5 3
3 4 2
3 3 3

输出

复制
19
193

说明

For the first query, f(\{A_3, A_4\}) = f(\{1, 5\}) = 1, f(\{A_2, A_3, A_4, A_5\}) = f(\{4, 1, 5, 3\}) = 3, so the answer is 1\times 6^0 + 3\times 6^1 = 1+18 = 19.

For the second query, f(\{A_3\}) = f(\{1\}) = 1, f(\{A_2, A_3, A_4\}) = f(\{4, 1, 5\}) = 2, f(\{A_1, A_2, A_3, A_4, A_5\}) = f(\{2, 4, 1, 5, 3\}) = 5, so the answer is 1\times 6^0 + 2\times 6^1 + 5\times 6^2 = 1 + 12 + 180 = 193.