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;
#define int int64_t
#define pii pair<int, int>
const int MOD = 1e9 + 7;
signed main() {
int n;cin>>n;
vector<pii> tog(n);
for (auto &x:tog)cin>>x.first;
for (auto &x:tog)cin>>x.second;
stack<pii> s;
int cmSum=0;
int res=0;
for (int i=0;i<n;i++) {
int w=tog[i].second, h=tog[i].first;
while (s.size() && s.top().first >= h) s.pop();
cmSum += w;
cmSum %= MOD;
if (s.size()) {
int leftBound = s.top().second;
int heightSum = ((((h * (h+1)) % MOD) * 500000004) % MOD);
int exp = ((cmSum - leftBound) + 2*MOD) % MOD;
int widthSum = ((((((cmSum * (cmSum+1))) % MOD) * 500000004) % MOD) - ((((exp * (exp + 1)) % MOD) * 500000004) % MOD) + 2 * MOD) % MOD;
res += (heightSum * widthSum) % MOD;
// cout << (heightSum * widthSum) % MOD << "\n";
}
else {
int heightSum = ((((h * (h+1)) % MOD) * 500000004) % MOD);
int widthSum = (((((cmSum * (cmSum+1))) % MOD) * 500000004) % MOD);
res += (heightSum * widthSum) % MOD;
// cout << (heightSum * widthSum) % MOD << "\n";
}
s.push({h, cmSum});
}
cout << res << "\n";
}
# | 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... |