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>
#include <vector>
#include <stack>
#include <fstream>
using namespace std;
#define int long long
#define pii pair<int, int>
const int MOD = 1e9 + 7;
const int INV2 = 500000004;
ifstream cin("a");
ofstream cout("b");
int32_t main() {
int n; cin >> n;
vector<int> h(n), w(n);
for (int i = 0; i < n; ++i) cin >> h[i];
for (int i = 0; i < n; ++i) cin >> w[i];
stack<int> hsum, area;
stack<pii> hs;
hsum.push(0), area.push(0);
hs.push({0, 0});
int ans = 0;
for (int i = 0; i < n; ++i) {
// add indiv to answer:
ans += (h[i] * (h[i] + 1) / 2 % MOD) * (w[i] * (w[i] + 1) / 2 % MOD) % MOD;
// cout << ans << ' ';
// remove unneccesary:
int nw = 0;
while (hs.top().first >= h[i]) nw += hs.top().second, hsum.pop(), area.pop(), hs.pop();
// insert (h[i], nw):
// cout << "nw " << nw << ' ';
int lh = hs.top().first + 1;
if (nw > 0) {
hsum.push((hsum.top() + (h[i] + lh) % MOD * (h[i] - lh + 1) % MOD * INV2 % MOD) % MOD);
area.push((area.top() + hsum.top() * nw % MOD) % MOD);
hs.push({h[i], w[i]});
}
// add rest to answer:
ans = (ans + w[i] * area.top() % MOD) % MOD;
// insert (h[i], w[i]):
if (nw > 0) hsum.pop(), area.pop(), hs.pop();
lh = hs.top().first + 1;
w[i] = (w[i] + nw) % MOD;
// cout << lh << ' ' << h[i] << ' ';
hsum.push((hsum.top() + (h[i] + lh) % MOD * (h[i] - lh + 1) % MOD * INV2 % MOD) % MOD);
// cout << hsum.top() << ' ';
area.push((area.top() + hsum.top() * w[i] % MOD) % MOD);
hs.push({h[i], w[i]});
// cout << ans << '\n';
}
cout << ans << '\n';
return 0;
}
/*
System.out.println("Hello World");
public static double yamom(String "s"){
}
*/
# | 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... |