| # | Time | Username | Problem | Language | Result | Execution time | Memory | 
|---|---|---|---|---|---|---|---|
| 1231373 | tminh | Fancy Fence (CEOI20_fancyfence) | C++20 | 0 ms | 328 KiB | 
#include "bits/stdc++.h"
using namespace std;
#define task ""
#define ll long long
#define endl '\n'
#define fi first
#define se second
#define vall(a) (a).begin(), (a).end()
#define sze(a) (int)a.size()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ep emplace_back
#define pb push_back
#define pf push_front
const ll mod = 1e9 + 7;
const int N = 1e6 + 5;
const ll oo = 1e18;
bool START;
int n, h[N], w[N];
ll pre[N];
stack<int> s;
int l[N], r[N];
ll ans = 0;
ll get(ll l, ll r) {
	return ((pre[r] - pre[l - 1]) % mod + mod) % mod;
}
inline void solve() {
	for (int i = 1; i <= n; ++i) pre[i] = (pre[i - 1] + w[i]) % mod;
	 
	for (int i = 1; i <= n; ++i) {
		while(!s.empty() && h[s.top()] >= h[i]) s.pop();
		l[i] = (s.empty() ? 0 : s.top());
		s.push(i);
	}
	while(!s.empty()) s.pop();
	for (int i = n; i >= 1; --i) {
		while(!s.empty() && h[s.top()] >= h[i]) s.pop();
		r[i] = (s.empty() ? n + 1 : s.top());
		s.push(i);
	}
	
	ll ans = 0;
	for (int i = 1; i <= n; ++i) {
		int prev_h = max(h[l[i]], h[r[i]]);
		ll H = (h[i] - prev_h);
		ll W = get(l[i] + 1, r[i] - 1);
		
		// cout << W << " " << H << " " << ((W * (W + 1) / 2) % mod * (prev_h + 1)) % mod * H << endl;
		ans = (ans + (((W * (W + 1) / 2) % mod * prev_h) % mod * H) % mod + ((W * (W + 1) / 2) % mod * (H * (H + 1) / 2) % mod)) % mod;
		
	}
	cout << ans;
	
}
inline void input() {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> h[i];
    for (int i = 1; i <= n; ++i) cin >> w[i];
    return solve();
}
bool END;
int main() {
    if(fopen(task ".inp", "r")) {
        freopen(task ".inp", "r", stdin);
        freopen(task ".out", "w", stdout);
    }
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    input();
    
    
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << 's' << endl;
    cerr << "Memory: " << fabs ((&END - &START)) / 1048576.0 << "MB\n";
    return 0;
}
Compilation message (stderr)
| # | 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... | ||||
