Код: Выделить всё
ans+=(((l*r)%mod*arr[i]%mod)%mod)%mod;
Код: Выделить всё
ans = (ans + ((l * r) % mod * arr[i] % mod) % mod) % mod;
Пример 1:
Ввод: arr = [3,1,2 ,4]
Выход: 17
Объяснение:
Подмассивами являются [3], [1], [2], [4], [3,1], [1,2], [2,4], [3,1,2], [1,2,4], [3,1,2,4].
Минимум: 3, 1, 2, 4, 1, 1, 2, 1, 1, 1.
Сумма: 17
Код: Выделить всё
class Solution {
public:
vector pse(vector& arr, int n){
stack st;
vector vec(n);
for (int i = 0; i < n; i++) {
while (!st.empty() && arr[st.top()] >= arr[i]) {
st.pop();
}
vec[i] = st.empty() ? -1 : st.top();
st.push(i);
}
return vec;
}
vector nse(vector& arr, int n){
stack st;
vector vec(n);
for (int i = n - 1; i >= 0; i--) {
while (!st.empty() && arr[st.top()] > arr[i]) {
st.pop();
}
vec[i] = st.empty() ? n : st.top();
st.push(i);
}
return vec;
}
int sumSubarrayMins(vector& arr) {
int n=arr.size();
vector leftVec = pse(arr,n);
vector rightVec = nse(arr,n);
long long ans=0;
int mod= 1e9+7;
for(int i=0;i
Подробнее здесь: [url]https://stackoverflow.com/questions/79051958/in-my-code-why-this-does-not-work-ans-lrmodarrimodmodmod-but-th[/url]