#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define ld long double
#define pb push_back
#define endl '\n'
#define all(v) v.begin(), v.end()
#define all1(v) v.begin() + 1, v.end()
#define rall(v) v.rbegin(), v.rend()
#define rall1(v) v.rbegin(), v.rend()-1
#define fr(m,n,k) for(int m=n;m<=k;m++)
#define frr(m,n,k) for(int m=n;m>=k;m--)
#define yes cout << "YES"
#define no cout << "NO"
#define yesm cout << "Yes"
#define nom cout << "No"
#define inf 1e18
#define ext {cout << -1; return;}
#define zxt {cout << 0; return;}
#define noxt {no;return;}
#define yesxt {yes;return;}
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a*(b/gcd(a,b)))
#define fill(x,s) memset(x, s, sizeof(x));
#define lb lower_bound
#define ub upper_bound
#define pi pair<int,int>
#define vi vector<int>
#define vvi vector<vi>
#define vs vector<string>
#define vpi vector<pi>
#define mii map<int,int>
#define si set<int>
#define fi first
#define se second
#define sz size()
#define el cout<<endl
int mod = 1e9 + 7;
// int mod = 998244353;
int ft(int n) {int ans = 1; for (int i = 1; i <= n; ++i) {ans = ans * i;} return ans;}
int binpow(int x, int y) {if (y == 0) return 1; int z = 1; while (y) { if (y & 1) z = z * x % mod; x = x * x % mod; y >>= 1;} return z;}
int inv(int x) {return binpow(x, mod - 2);}
int bintoint(string s) {reverse(all(s)); int xx = 0; int m = 1; int z = s.sz; fr(j, 0, z) {xx += (s[j] == '1' ? m : 0); m <<= 1;} return xx;}
string inttobin(int x) {if (x == 0) return "0"; string s; while (x) {s += (x & 1) + '0'; x >>= 1;} reverse(all(s)); return s;}
string ads(string s, string p) {if (s.sz > p.sz) swap(s, p); int n = s.sz, m = p.sz; reverse(all(s)); reverse(all(p)); int tm = 0; string st; fr(i, 0, n - 1) {tm = s[i] - '0' + p[i] - '0' + tm; st += '0' + tm % 10; tm /= 10;} fr(i, n, m - 1) {tm = p[i] - '0' + tm; st += '0' + tm % 10; tm /= 10;} if (tm != 0) {st += '0' + tm % 10;} reverse(all(st)); return st;}
bool id, id1, id2;
int n, m, k, l, r, a, b, c, d, e, x, y, z, i, j, q, t, o;
int mi = inf, ma, sum, ans, num, cnt;
string s, p;
const int N = 3e5 + 5;
void solve() {
cin >> n; vi h(n + 1), w(n + 1), ch(n + 1), suf(n + 2), pre(n + 1), bf(n + 1, 0), nx(n + 1, n + 1);
fr(i, 1, n) cin >> h[i], ch[i] = h[i]; fr(i, 1, n) cin >> w[i], pre[i] = pre[i - 1] + w[i], pre[i] %= mod;
sort(all1(ch));
int iv = inv(2);
suf[n + 1] = 0;
frr(i, n, 1) suf[i] = suf[i + 1] + w[i], suf[i] %= mod;
stack<int>st;
fr(i, 1, n) {
while (st.sz and h[st.top()] > h[i]) st.pop();
if (st.sz) bf[i] = st.top();
st.push(i);
}
st = {};
frr(i, n, 1) {
while (st.sz and h[st.top()] >= h[i]) st.pop();
if (st.sz) nx[i] = st.top();
st.push(i);
}
fr(i, 1, n) {
l = bf[i], r = nx[i]; l++; r--;
x = pre[i - 1] - pre[l - 1] + mod; x = x % mod;
y = pre[r] - pre[i] + mod; y = y % mod;
int now = h[i] * (h[i] + 1) / 2; now %= mod;
int wid = ((x * y) % mod) + (((x + y) * w[i]) % mod) + (((w[i] * (w[i] + 1)) / 2) % mod);
wid %= mod;
ans += now * wid % mod; ans %= mod;
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0);
int T = 1;
// cin >> T;
for (; T--;) {
cout << fixed << setprecision(5); solve();
cout << endl;
} return 0;
}
// Tc, angka kecil), No Overthink(trivial tp modified),
// Check Kasus Khusus(1,2,1e18), ganti jd bentuk lain (2 case or etc)
// bedah 1 1 variable yg bisa dicari, cari dari jwbn ke ques
// Sorting : Iterate Greedy (pake data struct) / dp / binser
// Graphs / Tree : DFS / adj / dis[a][b] != dis[a][c] + dis[c][b] / indegree
// Yang keliatan kyk dp : bisa jd brute force doang
// Data Structure : BIT / DSU / Segtree, SparseT / dequeue
// Compression, Line Sweep
// Parity (2 or 3, mod 4..) klo dilakuin 2 kali jadi sama aja -> parity mod 2
/*
brute force, greedy, pref sum, 2pointer, gcd/lcm, even/odd
min/max, binser, bfs/dfs, iterate, multiple/divisor
bipartite graph,bitmasking, sliding window, konstanta
divconquer, djikstra, stack(prev and next smaller)
prefix difference
*/
// tools :
//__lg(x),__builtinpopcount,nextpermutation(), is_sorted / alpha / digit,
Compilation message
fancyfence.cpp: In function 'void solve()':
fancyfence.cpp:60:7: warning: unused variable 'iv' [-Wunused-variable]
60 | int iv = inv(2);
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
15 ms |
3220 KB |
Output is correct |
4 |
Correct |
17 ms |
6488 KB |
Output is correct |
5 |
Correct |
29 ms |
6212 KB |
Output is correct |
6 |
Correct |
24 ms |
5960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
4 ms |
856 KB |
Output is correct |
3 |
Correct |
18 ms |
3472 KB |
Output is correct |
4 |
Correct |
33 ms |
6616 KB |
Output is correct |
5 |
Correct |
30 ms |
6744 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
10 ms |
3416 KB |
Output is correct |
5 |
Correct |
19 ms |
6748 KB |
Output is correct |
6 |
Correct |
33 ms |
6736 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
4 ms |
856 KB |
Output is correct |
9 |
Correct |
17 ms |
3476 KB |
Output is correct |
10 |
Correct |
19 ms |
6740 KB |
Output is correct |
11 |
Correct |
20 ms |
6748 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
9 ms |
3216 KB |
Output is correct |
12 |
Correct |
28 ms |
6472 KB |
Output is correct |
13 |
Correct |
28 ms |
6236 KB |
Output is correct |
14 |
Correct |
16 ms |
5976 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
864 KB |
Output is correct |
17 |
Correct |
10 ms |
3636 KB |
Output is correct |
18 |
Correct |
21 ms |
6744 KB |
Output is correct |
19 |
Correct |
23 ms |
6748 KB |
Output is correct |
20 |
Correct |
1 ms |
352 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
18 ms |
3420 KB |
Output is correct |
23 |
Correct |
21 ms |
6744 KB |
Output is correct |
24 |
Correct |
21 ms |
6752 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
512 KB |
Output is correct |
30 |
Correct |
3 ms |
860 KB |
Output is correct |
31 |
Correct |
3 ms |
860 KB |
Output is correct |
32 |
Correct |
17 ms |
3164 KB |
Output is correct |
33 |
Correct |
16 ms |
3164 KB |
Output is correct |
34 |
Correct |
33 ms |
5728 KB |
Output is correct |
35 |
Correct |
22 ms |
5724 KB |
Output is correct |
36 |
Correct |
34 ms |
5724 KB |
Output is correct |
37 |
Correct |
25 ms |
5728 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
26 ms |
5724 KB |
Output is correct |
40 |
Correct |
23 ms |
5980 KB |
Output is correct |
41 |
Correct |
22 ms |
5980 KB |
Output is correct |
42 |
Correct |
23 ms |
6244 KB |
Output is correct |