Submission #1280403

#TimeUsernameProblemLanguageResultExecution timeMemory
1280403reginoxFancy Fence (CEOI20_fancyfence)C++20
43 / 100
16 ms2468 KiB
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(v) begin(v), end(v)
#define pi pair<int, int>
#define vi vector<int>
using namespace std;
const int N = 1e5+3;
const ll mod = 1e9+7;
const ll mul =  5e8 + 4;
int n, h[N], w[N];

ll cnt(ll x){
	return ((x * (x+1)) % mod * mul) % mod;
}

int main(){
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> h[i];
	for(int i = 1; i <= n; i++) cin >> w[i];
	if(is_sorted(h+1, h+n+1)){
		ll res = 0;
		ll cur = 0;
		for(int i = 1; i <= n; i++){
			res = (res + w[i] * cur + cnt(h[i]) * cnt(w[i])) % mod;
			cur = (cur + w[i] * cnt(h[i])) % mod;
		}
		cout << res;
	}
	else{
		ll res = 0;
		ll cur = 0;
		stack<pair<ll, ll>> st;
		for(int i = 1; i <= n; i++){
			ll wc = 0;
			while(!st.empty() && (st.top()).first > h[i]){
				wc += (st.top()).second;
				cur = (cur - (st.top()).second * cnt((st.top()).first)) % mod;
				cur = (cur + mod) % mod;
				st.pop();
			}
			cur = (cur + wc * cnt(h[i])) % mod;
			res = (res + cnt(h[i]) * cnt(w[i]) + w[i] * cur) % mod;
			cur = (cur + w[i] * cnt(h[i])) % mod;
			st.push({h[i], w[i]});
		}
		cout << res;
	}
	return 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...