This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* In the name of God */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
#define PB push_back
#define MP make_pair
#define all(a) (a).begin(), (a).end()
#define endl '\n'
#define dbg(x) cerr << '[' << #x << ": " << x << "]\n"
#define dbg2(x, y) cerr << '[' << #x << ": " << x << ", " << #y << ": " << y << "]\n"
#define YES cout << "YES\n"
#define NO cout << "NO\n"
const ll INF = (ll)2e18 + 1386;
const ld EPS = 0.000000000000001;
const int MOD = 1e9 + 7;
inline int _add(int a, int b){ int res = a + b; return (res >= MOD ? res - MOD : res); }
inline int _neg(int a, int b){ int res = (abs(a - b) < MOD ? a - b : (a - b) % MOD); return (res < 0 ? res + MOD : res); }
inline int _mlt(ll a, ll b){ return (a * b % MOD); }
inline void fileIO(string i, string o){ freopen(i.c_str(), "r", stdin); freopen(o.c_str(), "w", stdout); }
const int MAXN = 2e5 + 5, inv2 = (MOD + 1) / 2;
int C(int x){ return _mlt(_mlt(x, x - 1), inv2); }
int h[MAXN], w[MAXN];
int lef[MAXN], rig[MAXN];
ll ps[MAXN];
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> h[i];
for (int i = 1; i <= n; i++){
cin >> w[i];
ps[i] = _add(ps[i - 1], w[i]);
}
int ans = 0;
VI agha;
for (int i = 1; i <= n; i++){
while (!agha.empty() && h[agha.back()] > h[i]){
agha.pop_back();
}
lef[i] = (agha.empty() ? 0 : agha.back());
agha.PB(i);
}
agha.clear();
for (int i = n; i >= 1; i--){
while (!agha.empty() && h[agha.back()] >= h[i]){
agha.pop_back();
}
rig[i] = (agha.empty() ? n + 1 : agha.back());
agha.PB(i);
}
for (int i = 1; i <= n; i++){
int l = lef[i] + 1, r = rig[i] - 1;
int cw = _neg(C(_neg(ps[r], ps[l - 1]) + 1), _add(C(_neg(ps[i - 1], ps[l - 1]) + 1), C(_neg(ps[r], ps[i]) + 1)));
int ch = C(h[i] + 1);
ans = _add(ans, _mlt(cw, ch));
}
cout << ans;
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... |