제출 #910090

#제출 시각아이디문제언어결과실행 시간메모리
910090NK_Fancy Fence (CEOI20_fancyfence)C++17
100 / 100
25 ms5060 KiB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>

using namespace std;

#define nl '\n'
#define pb push_back

using ll = long long;

const int MOD = 1e9 + 7;
const int INV2 = 500000004;

template<class T> using V = vector<T>;
template<class T, size_t SZ> using AR = array<T, SZ>;

int main() {
	cin.tie(0)->sync_with_stdio(0);
	
	int N; cin >> N;
	V<ll> A(N); for(auto& x : A) cin >> x;
	V<ll> B(N); for(auto& x : B) cin >> x;
	A.pb(0); B.pb(0);

	auto sum = [&](ll l, ll r) {
		l %= MOD, r %= MOD;
		// sum of l + (l + 1) + (l + 2) + ... + r;
		ll L = (((l * 1LL * (l - 1)) % MOD) * 1LL * INV2) % MOD; // exclusive L
		ll R = (((r * 1LL * (r + 1)) % MOD) * 1LL * INV2) % MOD; // inclusive R
		return (R - L + 10LL * MOD) % MOD;
	};


	V<AR<ll, 2>> stk = {{0, -1}};

	ll ans = 0;
	for(int i = 0; i <= N; i++) {
		ll x = A[i], amt = B[i];

		ll w = 0;
		while(stk.back()[1] != -1 && stk.back()[0] >= x) {
			ll H = stk.back()[0], W = stk.back()[1]; stk.pop_back();

			ll h = max(x, stk.back()[0]); amt += W, w += W;

			// w blocks width, from h + 1 to H height
			ll ways = (sum(h + 1, H) * 1LL * sum(1, w)) % MOD;
			
			// cout << i << " => " << w << " - " << h + 1 << " -> " << H << " ==> " << ways << endl;


			ans += ways;
			if (ans >= MOD) ans -= MOD;
		}

		stk.pb({x, amt});
	}

	cout << ans << nl;




	exit(0-0);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...