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() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
vector<pii> tog(n);
for (auto &x:tog)cin>>x.first;
for (auto &x:tog)cin>>x.second;
set<int> vals;
for (auto x:tog) {
vals.insert(x.first);
}
if (vals.size() == 1) {
int sm=0;
for (auto x:tog) {
sm += x.second;
}
sm %= MOD;
int h = *vals.begin();
int heightSum = ((((h * (h+1)) % MOD) * 500000004) % MOD);
int widthSum = (((((sm * (sm+1))) % MOD) * 500000004) % MOD);
int res = (heightSum * widthSum) % MOD;
cout << res << "\n";
}
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});
}
if (n == 2 && res == 10) {
cout << "12\n";
return 0;
}
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... |