Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Focus on adjacent pairs. If , try proving that is greater than every later element, not just .
Use induction: from , the condition gives . After that, every next element is smaller than the max of two already-smaller elements.
For a subarray , take every index with and , then also take . Those chosen values form a decreasing subsequence.
Now upper-bound it. If , a decreasing subsequence cannot contain both positions and . Also, two adjacent increases cannot happen back-to-back because that would violate the given condition.
So for every subarray, . Sum all subarray lengths, then subtract the number of subarrays containing each increasing edge .
The condition is the whole damn trick:
It says every new element is smaller than at least one of the previous two. That makes the LDS much simpler than it looks.
Lemma 1: a descent dominates everything after it
Suppose
Then for every .
Proof:
So by induction, once there is a descent at , the value is bigger than everything to its right.
Build a long decreasing subsequence
Look at some subarray .
Take every position with
and also take position .
These positions are increasing by index. Their values are decreasing because each chosen descent-start is greater than every later value.
So this is a valid decreasing subsequence of length
Since every adjacent pair is either an increase or a descent, this is also
So we have a lower bound.
Now prove it is optimal
If , then no decreasing subsequence can contain both positions and , because the earlier value is smaller. That pair forces us to miss at least one element.
Also, increasing pairs cannot overlap. If both
and
were true, then would be greater than both previous values, contradicting
So all adjacent increasing pairs inside a subarray are disjoint. If a subarray has adjacent increases and length , any decreasing subsequence misses at least one element from each of those disjoint pairs. Therefore its length is at most
The lower bound already reached , so:
That is the entire problem. No patience sorting, no segment tree, no wizard nonsense.
Summing over all subarrays
First sum the lengths of all subarrays:
Now subtract the contribution of adjacent increases.
For a fixed increasing edge with , it appears in exactly the subarrays with
There are choices for and choices for , so this edge is counted in
subarrays.
Thus the answer is
Complexity
One scan of the array:
per test case, with memory in this implementation. You can do memory too, but storing the permutation is clean and easily fits.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> p(n + 1);
for (int i = 1; i <= n; i++) cin >> p[i];
ll nn = n;
ll ans = nn * (nn + 1) * (nn + 2) / 6;
for (int i = 1; i < n; i++) {
if (p[i] < p[i + 1]) {
ans -= 1LL * i * (n - i);
}
}
cout << ans << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> p(n + 1);
for (int i = 1; i <= n; i++) cin >> p[i];
ll nn = n;
ll ans = nn * (nn + 1) * (nn + 2) / 6;
for (int i = 1; i < n; i++) {
if (p[i] < p[i + 1]) {
ans -= 1LL * i * (n - i);
}
}
cout << ans << '\n';
}
return 0;
}