从左往右扫,找到比第i个小的第一个数字,l[i] = l[last] + (i - last) * m[i],用单调栈O(n)维护这个过程,再从右往左扫,同理可以算出r数组,注意一下long long #include <bits/stdc++.h> using namespace std; ; int m[N], ans[N]; long long l[N], r[N]; stack < int > s; int main() { int n; scanf("%d&qu…
D. Imbalanced Array time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given an array a consisting of n elements. The imbalance value of some subsegment of this array is the differenc…