#include <bits/stdc++.h>
#define respagold ios_base::sync_with_stdio(0), cin.tie(0);
#define int long long
#define ll long long
#define int2 __int128_t
#define FOR( i, x, n, d ) for( int i = x; i <= n; i += d )
#define FORR( i, x, n, d ) for( int i = x; i >= n; i -= d )
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x.size())
#define pb push_back
#define ins insert
#define lb lower_bound
#define ub upper_bound
#define pii pair <int, int>
#define mkp make_pair
using namespace std;
const int N = 1e5 + 123;
const int inf = 1e18;
const int mod = 1e9 + 7;
const double eps = 1e-13;
int a[N], b[N], p[N], n, m, k, z, x, y, ans, lg[N], inv;
pii st[N][20];
mt19937 rng( chrono::steady_clock::now().time_since_epoch().count());
int rand( int l, int r )
{
uniform_int_distribution <int> uid( l, r );
return uid( rng );
}
pii get( int l, int r )
{
int d = lg[r - l + 1];
return min( st[l][d], st[r - (1 << d) + 1][d] );
}
void dc( int l, int r )
{
if( l > r ) return;
pii now = get( l, r );
int sum = 0, he = now.F, wl = p[now.S - 1] - p[l - 1], wr = p[r] - p[now.S], vnutri = b[now.S] * ( b[now.S] + 1 ) / 2;
wl = ( wl + mod ) % mod;
wr = ( wr + mod ) % mod;
vnutri %= mod;
// sum += (he - i + 1)
// sum += he * he - he * i + he
// sum = he * he - he * (he + 1) / 2 + he
sum = he * he - he * (he + 1) / 2 + he;
sum %= mod;
ans = ( ans + ( wl * wr + b[now.S] * (wl + wr) % mod + vnutri % mod ) % mod * sum % mod ) % mod;
dc( l, now.S - 1 );
dc( now.S + 1, r );
}
void solve()
{
cin >> n;
FOR( i, 1, n, 1 ) cin >> a[i], st[i][0] = { a[i], i };
FOR( i, 1, n, 1 ) cin >> b[i], lg[i] = lg[i >> 1] + (i != 1), p[i] = (p[i - 1] + b[i]) % mod;
FOR( j, 1, 17, 1 )
{
FOR( i, 1, n, 1 )
{
if( i + (1 << j) - 1 > n ) break;
st[i][j] = min( st[i][j - 1], st[i + (1 << j - 1)][j - 1] );
}
}
dc( 1, n );
cout << ans;
}
signed main()
{
respagold;
int test = 1;
if( !test ) cin >> test;
while( test -- ) solve();
}
# | 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... |