Simple Query Problem
Time limit3sMemory limit1024 MB
Given a permutation and range queries, compute the sum of |a_i - a_j| over all pairs i <= j inside each query range.
- Level
Medium6 of 10
- Topics
- Array, Segment tree, Sorting
- Solved
- No attempts yet
Problem
You are given a permutation of the numbers through . Write a program that processes the following query.
- : Print the value of .
Input
The first line contains and , separated by a space. The second line contains the permutation of length , separated by spaces. Lines 3 through each contain and , separated by a space, one query per line.
Output
Print the answer to each query on its own line.