This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
stack <pair<int,int>> S;
long long n,v[100005],i,dr[100005],st[100005],x,suma[100001],mars[100001],j,val,Max[100005];
pair <int,int> sor[100005];
long long total;
#define mod 1000000007
long long rectCount(long long n, long long m)
{
return ((((m % 1000000007 * n % 1000000007) % 1000000007 * (n + 1) % 1000000007) % 1000000007 * (m + 1) % 1000000007) % 1000000007)/ 4;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
for(i=1;i<=n;i++){
cin>>v[i];
sor[i].first = v[i];
sor[i].second = i;
}
sort(sor+1,sor+n+1);
for(i=1;i<=n;i++){
cin>>x;
suma[i] = suma[i-1] + x;
}
for(i=1;i<=n;i++){
x = v[i];
while(S.empty() == false && x<S.top().first){
dr[S.top().second] = i;
S.pop();
}
S.push({x,i});
}
while(S.empty() == false){
S.pop();
}
for(i=n;i>=1;i--){
x = v[i];
while(S.empty() == false && x < S.top().first){
st[S.top().second] = i;
S.pop();
}
S.push({x,i});
}
for(i=1;i<=n;i++){
if(!dr[i])
dr[i] = n + 1;
}
for(i=1;i<=n;i++){
total += rectCount(suma[dr[sor[i].second]-1] - suma[st[sor[i].second]],sor[i].first) % 1000000007;
total -= rectCount(suma[dr[sor[i].second]-1] - suma[st[sor[i].second]],Max[sor[i].second]) % 1000000007;
for(j=st[sor[i].second];j<=dr[sor[i].second];j++){
Max[j] = sor[i].first;
}
}
cout << total % 1000000007;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |