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 pb push_back
#define fi first
#define se second
#define TranLeThanhTam "TranLeThanhTam"
#define all(v) v.begin(),v.end()
#define reset(a,i) memset(a,i,sizeof(a))
#define FOR(i,a,b) for (int i = (a), _b = (b); i <= _b; ++i)
#define FORD(i,a,b) for (int i = (a), _b = (b); i >= _b; --i)
#define compact(v) sort(all(v)), v.erase(unique(all(v)), end(v))
#define sz(v) (int) v.size()
#define MASK(i) (1LL << (i)
#define BIT(i,x) ((x & MASK(i)) >> (i))
#define int ll
typedef long long ll;
typedef long double lb;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
const int Thanh_Tam = 1e5 + 17;
const int inf = 1e9 + 17;
const long long infll = 1e18 + 17;
const int MOD = 1e9 + 7;
const int Log = 18;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, 1, 0, -1};
const int max_block_size = 270;
const ll P = 311;
const int maxA = 1e2 + 17;
const int addi = 1e9;
const char dir[4] = {'W', 'S', 'N', 'E'};
const int Mod[3] = {998244353, MOD, 759186269};
ll mul(ll a, ll b, ll mod);
ll binpow(ll a, ll b, ll mod)
{
ll res = 1;
while (b > 0)
{
if (b & 1) res = mul(res,a,mod);
b >>= 1;
a = mul(a,a,mod);
}
return res;
}
ll add(ll a, ll b, ll mod) { return (a + b) % mod; }
ll sub(ll a, ll b, ll mod) { return (a - b + mod) % mod; }
ll mul(ll a, ll b, ll mod) { return ((a % mod) * (b % mod)) % mod; }
ll div(ll a, ll b, ll mod) { return mul(a, binpow(b, mod - 2, mod), mod); }
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rand_range(ll l, ll r) { return uniform_int_distribution<ll>(l,r)(rng); }
int n;
int h[Thanh_Tam], w[Thanh_Tam], l[Thanh_Tam], r[Thanh_Tam];
ll pref_w[Thanh_Tam];
ll ans = 0;
stack<int> st;
vector<pii> order;
map<pair<int,pii>, bool> mp;
ll sum(int x)
{
return ((1LL * x * (x + 1)) >> 1) % MOD;
}
ll calc(int x, int y)
{
return (1LL * sum(x) * sum(y)) % MOD;
}
ll merge(int x1, int y1, int x2, int y2)
{
if (x1 != x2) return 0;
return (sum(x1) * 1LL * y1 * y2) % MOD;
}
void ThanhTam()
{
cin >> n;
FOR(i,1,n) cin >> h[i];
FOR(i,1,n) cin >> w[i];
//Prepare
FOR(i,1,n) pref_w[i] = pref_w[i - 1] + w[i];
//Find left
while (!st.empty()) st.pop();
FOR(i,1,n)
{
while (!st.empty())
if (h[st.top()] >= h[i]) st.pop();
else break;
if (st.empty()) l[i] = 0;
else l[i] = st.top();
st.push(i);
}
//Find right
while (!st.empty()) st.pop();
FORD(i,n,1)
{
while (!st.empty())
if (h[st.top()] >= h[i]) st.pop();
else break;
if (st.empty()) r[i] = n + 1;
else r[i] = st.top();
st.push(i);
}
//Calculate answer
FOR(i,1,n)
if (!mp.count({h[i], {l[i], r[i]}}))
{
ans = add(ans, calc(pref_w[r[i] - 1] - pref_w[l[i]], h[i] - min(h[i], max(h[l[i]], h[r[i]]))), MOD);
mp[{h[i], {l[i], r[i]}}] = true;
//cout << ans << ' ';
ans = add(ans, merge(pref_w[r[i] - 1] - pref_w[l[i]], h[i] - min(h[i], max(h[l[i]], h[r[i]])), pref_w[r[i] - 1] - pref_w[l[i]], min(h[i], max(h[l[i]], h[r[i]]))), MOD);
//cout << ans << '\n';
}
cout << ans;
return;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
if (fopen(TranLeThanhTam".inp","r"))
{
freopen(TranLeThanhTam".inp","r",stdin);
freopen(TranLeThanhTam".out","w",stdout);
}
int t = 1;
//cin >> t;
while (t--)
ThanhTam();
return 0;
}
/*
2 * 4 => 30
2 * 2 => 9
2 * 3 => 18
2 * 1 => 3
Cách suy ra 2 * 4 từ 2 * 3 và 2 * 1
2 * 1 + 2 * 3 + 2 * 3 * 1 = 3 + 18 + 9 = 30 (Điều kiện: 2 cạnh đầu phải bằng nhau)
0
0000
00000000
00000000
0000000000
*/
Compilation message (stderr)
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:142:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
142 | freopen(TranLeThanhTam".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:143:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
143 | freopen(TranLeThanhTam".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |