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;
const int MAX_N = 1e5;
const int MOD = 1e9 + 7;
int h[MAX_N], w[MAX_N], p[MAX_N];
vector <int> s;
int lgPut( int x, int n ) {
if ( n == 0 )
return 1;
int p = lgPut( x, n / 2 );
p = (long long)p * p % MOD;
if ( n % 2 == 1 )
p = (long long)p * x % MOD;
return p;
}
int main() {
int n;
cin >> n;
for ( int i = 0; i < n; i++ )
cin >> h[i];
for ( int i = 0; i < n; i++ )
cin >> w[i];
int inv2 = lgPut( 2, MOD - 2 );
int ans = 0, sum = 0;
for ( int i = 0; i < n; i++ ) {
while ( !s.empty() && h[i] <= h[s.back()] ) {
sum = (sum - (long long)h[i] * (h[i] + 1) % MOD * inv2 % MOD * p[s.back()] % MOD + MOD) % MOD;
p[i] = (p[i] + p[s.back()]) % MOD;
s.pop_back();
}
s.push_back( i );
sum = (sum + (long long)h[i] * (h[i] + 1) % MOD * inv2 % MOD * p[i] % MOD) % MOD;
ans = (ans + ((long long)h[i] * (h[i] + 1) % MOD * inv2 % MOD) * ((long long)w[i] * (w[i] + 1) % MOD * inv2 % MOD)) % MOD;
ans = (ans + (long long)sum * w[i] % MOD) % MOD;
sum = (sum + (long long)h[i] * (h[i] + 1) % MOD * inv2 % MOD * w[i] % MOD) % MOD;
// sum = (sum + w[i]) % MOD;
}
cout << ans << "\n";
return 0;
}
# | 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... |