D. Imbalanced Array

Codeforces
IDCF817D
Time2000ms
Memory256MB
Difficulty
data structuresdivide and conquerdsusortings
English · Original
Chinese · Translation
Formal · Original
You are given an array _a_ consisting of _n_ elements. The _imbalance value_ of some subsegment of this array is the difference between the maximum and minimum element from this segment. The _imbalance value_ of the array is the sum of _imbalance values_ of all subsegments of this array. For example, the _imbalance value_ of array \[1, 4, 1\] is 9, because there are 6 different subsegments of this array: * \[1\] (from index 1 to index 1), _imbalance value_ is 0; * \[1, 4\] (from index 1 to index 2), _imbalance value_ is 3; * \[1, 4, 1\] (from index 1 to index 3), _imbalance value_ is 3; * \[4\] (from index 2 to index 2), _imbalance value_ is 0; * \[4, 1\] (from index 2 to index 3), _imbalance value_ is 3; * \[1\] (from index 3 to index 3), _imbalance value_ is 0; You have to determine the _imbalance value_ of the array _a_. ## Input The first line contains one integer _n_ (1 ≤ _n_ ≤ 106) — size of the array _a_. The second line contains _n_ integers _a_1, _a_2... _a__n_ (1 ≤ _a__i_ ≤ 106) — elements of the array. ## Output Print one integer — the _imbalance value_ of _a_. [samples]
你被给定一个包含 $n$ 个元素的数组 $[a]$。该数组某个子段的 _不平衡值_ 定义为该子段中最大元素与最小元素的差。数组的 _不平衡值_ 是该数组所有子段的 _不平衡值_ 之和。 例如,数组 $[1, 4, 1]$ 的 _不平衡值_ 为 $9$,因为该数组有 $6$ 个不同的子段: 你需要确定数组 $[a]$ 的 _不平衡值_。 第一行包含一个整数 $n$ ($1 ≤ n ≤ 10^6$) —— 数组 $[a]$ 的大小。 第二行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$ ($1 ≤ a_i ≤ 10^6$) —— 数组的元素。 请输出一个整数 —— 数组 $[a]$ 的 _不平衡值_。 ## Input 第一行包含一个整数 $n$ ($1 ≤ n ≤ 10^6$) —— 数组 $[a]$ 的大小。第二行包含 $n$ 个整数 $a_1, a_2, \dots, a_n$ ($1 ≤ a_i ≤ 10^6$) —— 数组的元素。 ## Output 请输出一个整数 —— 数组 $[a]$ 的 _不平衡值_。 [samples]
**Definitions** Let $ n \in \mathbb{Z}^+ $ be the length of the array. Let $ A = (a_1, a_2, \dots, a_n) $ be a sequence of integers with $ a_i \in \mathbb{Z}^+ $. Let $ \mathcal{S} = \{ [i,j] \mid 1 \leq i \leq j \leq n \} $ be the set of all contiguous subsegments (subarrays) of $ A $. For a subsegment $ [i,j] $, define its imbalance value as: $$ \text{imbalance}([i,j]) = \max_{k \in [i,j]} a_k - \min_{k \in [i,j]} a_k $$ **Objective** Compute the total imbalance value of the array: $$ \sum_{[i,j] \in \mathcal{S}} \left( \max_{k=i}^j a_k - \min_{k=i}^j a_k \right) $$
Samples
Input #1
3
1 4 1
Output #1
9
API Response (JSON)
{
  "problem": {
    "name": "D. Imbalanced Array",
    "description": {
      "content": "You are given an array _a_ consisting of _n_ elements. The _imbalance value_ of some subsegment of this array is the difference between the maximum and minimum element from this segment. The _imbalanc",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF817D"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given an array _a_ consisting of _n_ elements. The _imbalance value_ of some subsegment of this array is the difference between the maximum and minimum element from this segment. The _imbalanc...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "你被给定一个包含 $n$ 个元素的数组 $[a]$。该数组某个子段的 _不平衡值_ 定义为该子段中最大元素与最小元素的差。数组的 _不平衡值_ 是该数组所有子段的 _不平衡值_ 之和。\n\n例如,数组 $[1, 4, 1]$ 的 _不平衡值_ 为 $9$,因为该数组有 $6$ 个不同的子段:\n\n你需要确定数组 $[a]$ 的 _不平衡值_。\n\n第一行包含一个整数 $n$ ($1 ≤ n ≤ 10^6...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "**Definitions**  \nLet $ n \\in \\mathbb{Z}^+ $ be the length of the array.  \nLet $ A = (a_1, a_2, \\dots, a_n) $ be a sequence of integers with $ a_i \\in \\mathbb{Z}^+ $.\n\nLet $ \\mathcal{S} = \\{ [i,j] \\mi...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments