D. Sum of LDS

Problem Statement

You are given a permutation p1,p2,,pnp_1, p_2, \ldots, p_n of length nn (i.e., an array containing each integer from 11 to nn exactly once), with the property that for every 1in21 \leq i \leq n-2:

max(pi,pi+1)>pi+2 \max(p_i, p_{i+1}) > p_{i+2}

For every pair of indices 1lrn1 \leq l \leq r \leq n, consider the subarray [pl,pl+1,,pr][p_l, p_{l+1}, \ldots, p_r]. Let LDS(l,r)LDS(l, r) denote the length of the longest decreasing subsequence in this subarray.

Compute the sum:

1lrnLDS(l,r) \sum_{1 \leq l \leq r \leq n} LDS(l, r)

Definitions:

  • A permutation of length nn is an array of nn distinct integers from 11 to nn in any order.
  • A decreasing subsequence of length kk in an array bb is a sequence of indices i1<i2<<iki_1 < i_2 < \ldots < i_k such that bi1>bi2>>bikb_{i_1} > b_{i_2} > \ldots > b_{i_k}.

Solution Overview

Let’s analyze the problem step by step:

  1. Key Constraint: The condition max(pi,pi+1)>pi+2\max(p_i, p_{i+1}) > p_{i+2} ensures that two consecutive ascents cannot occur in the permutation.

  2. LDS Contribution: For any pair (pi,pi+1)(p_i, p_{i+1}) where pi>pi+1p_i > p_{i+1}, these two elements form a “block” that can contribute to the longest decreasing subsequence (LDS) in any subarray containing both. However, only one of them will actually increase the LDS length.

  3. Counting Contributions: For each such pair (pi,pi+1)(p_i, p_{i+1}) with pi>pi+1p_i > p_{i+1}, the number of subarrays containing both is (i+1)×(ni1)(i + 1) \times (n - i - 1), where ii is the index of pip_i (0-based).

  4. Total LDS Sum: If there are no such pairs, the sum is simply the sum over all subarrays of their lengths:

    n+2(n1)+3(n2)++n n + 2(n-1) + 3(n-2) + \ldots + n

    Otherwise, subtract the overcounted contributions for each (pi,pi+1)(p_i, p_{i+1}) where pi>pi+1p_i > p_{i+1}. This part can be optimized using O(1)O(1), but I did not implement it.

  5. Algorithm Steps:

    • Iterate through the permutation and identify all pairs (pi,pi+1)(p_i, p_{i+1}) with pi>pi+1p_i > p_{i+1}.
    • For each such pair, subtract (i+1)×(ni1)(i + 1) \times (n - i - 1) from the total LDS sum.
    • Output the final result.

This approach leverages the permutation’s structure and efficiently computes the required sum.

submission