/* 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 |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4560 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
8 ms |
5444 KB |
Output is correct |
4 |
Correct |
15 ms |
6368 KB |
Output is correct |
5 |
Correct |
15 ms |
6172 KB |
Output is correct |
6 |
Correct |
14 ms |
5980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4564 KB |
Output is correct |
2 |
Correct |
3 ms |
4700 KB |
Output is correct |
3 |
Correct |
9 ms |
6228 KB |
Output is correct |
4 |
Correct |
18 ms |
7124 KB |
Output is correct |
5 |
Correct |
18 ms |
7392 KB |
Output is correct |
6 |
Correct |
1 ms |
4560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
3 ms |
4700 KB |
Output is correct |
4 |
Correct |
9 ms |
5980 KB |
Output is correct |
5 |
Correct |
18 ms |
7364 KB |
Output is correct |
6 |
Correct |
21 ms |
7384 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
3 ms |
4700 KB |
Output is correct |
9 |
Correct |
12 ms |
6084 KB |
Output is correct |
10 |
Correct |
18 ms |
7108 KB |
Output is correct |
11 |
Correct |
18 ms |
7128 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4572 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4448 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
8 ms |
5444 KB |
Output is correct |
12 |
Correct |
15 ms |
6548 KB |
Output is correct |
13 |
Correct |
15 ms |
6184 KB |
Output is correct |
14 |
Correct |
14 ms |
5900 KB |
Output is correct |
15 |
Correct |
1 ms |
4568 KB |
Output is correct |
16 |
Correct |
3 ms |
4700 KB |
Output is correct |
17 |
Correct |
14 ms |
5980 KB |
Output is correct |
18 |
Correct |
18 ms |
7132 KB |
Output is correct |
19 |
Correct |
22 ms |
7432 KB |
Output is correct |
20 |
Correct |
1 ms |
4440 KB |
Output is correct |
21 |
Correct |
3 ms |
4700 KB |
Output is correct |
22 |
Correct |
10 ms |
5980 KB |
Output is correct |
23 |
Correct |
23 ms |
7240 KB |
Output is correct |
24 |
Correct |
19 ms |
7140 KB |
Output is correct |
25 |
Correct |
1 ms |
4440 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4584 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
3 ms |
4700 KB |
Output is correct |
31 |
Correct |
3 ms |
4700 KB |
Output is correct |
32 |
Correct |
10 ms |
5464 KB |
Output is correct |
33 |
Correct |
10 ms |
5724 KB |
Output is correct |
34 |
Correct |
20 ms |
6480 KB |
Output is correct |
35 |
Correct |
18 ms |
6484 KB |
Output is correct |
36 |
Correct |
20 ms |
6596 KB |
Output is correct |
37 |
Correct |
19 ms |
6492 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
18 ms |
6492 KB |
Output is correct |
40 |
Correct |
18 ms |
6596 KB |
Output is correct |
41 |
Correct |
18 ms |
6740 KB |
Output is correct |
42 |
Correct |
18 ms |
6996 KB |
Output is correct |