#include <bits/stdc++.h>
using namespace std;
# define int long long
# define fir first
# define sec second
# define pb push_back
# define endl "\n"
const int cnst = 2e5+5;
bool mutipletestcase = 0;
//bool debug = false;
const int mod = 1e9+7;
int F[cnst];
int inv[cnst];
int A(int a, int b) {return ((a%mod)+(b%mod))%mod;}
int S(int a, int b) {return ((a%mod)-(b%mod)+mod)%mod;}
int M(int a, int b) {return ((a%mod)*(b%mod))%mod;}
int E(int b, int p) {
if(!p) return 1;
return (p%2 ? M(E(M(b, b), p/2), b) : E(M(b, b), p/2));
}
int C(int n, int r) {return M(M(F[n], inv[n-r]), inv[r]);}
void prefac(int maxn) {
F[0] = 1;
for(int i = 1; i<=maxn; i++) F[i] = M(F[i-1], i);
inv[maxn] = E(F[maxn], mod-2);
for(int i = maxn-1; i>=0; i--) inv[i] = M(inv[i+1], i+1);
}
int gcd(int a, int b) {return b ? gcd(b, a%b): a;}
int lcm(int a, int b) {return M(a/gcd(a, b), b);}
int L[cnst];
int R[cnst];
int count1[cnst];
int root[cnst];
int find(int x) {return root[x] == x ? x : root[x] = find(root[x]);}
void merge(int x, int y) {
int lx = L[find(x)];int ly = L[find(y)];
int rx = R[find(x)];int ry = R[find(y)];
int cx = count1[find(x)];int cy = count1[find(y)];
root[find(x)] = find(y);
L[find(x)] = min(lx, ly);
R[find(x)] = max(rx, ry);
count1[find(x)] = cx+cy;
}
void solve() {
int n; cin >> n;
int H[n+5];
int W[n+5];
for(int i = 1; i<=n; i++) cin >> H[i];
for(int j = 1; j<=n; j++) cin >> W[j];
int ans = 0;
vector<tuple<int, int, int>> vec;
for(int i = 1; i<=n; i++) vec.pb({H[i], W[i], i}), ans = A(ans, M(H[i]*(H[i]+1)/2, W[i]*(W[i]+1)/2));
// for(int i = 1; i<=n; i++) cerr << H[i] << " " << W[i] << endl;
sort(vec.begin(), vec.end());
reverse(vec.begin(), vec.end());
bool vis[n+5];
// memset(count, 0, sizeof(count));
memset(vis, 0, sizeof(vis));
for(int i = 1; i<=n; i++) L[i] = R[i] = root[i] = i;
// cerr << ans << endl;
for(auto [h, w, id]: vec) {
count1[id] = w; vis[id] = 1;
// mx[id] = h; tt[id] = M(h, w);
// cerr << h << " " << w << " " << id << endl;
// cerr << count[id-1] << " " << count[id] << " " << count[id+1] << endl;
if(vis[id-1] && vis[id+1] && id != 1 && id != n) {
ans = A(ans, M(h*(h+1)/2, S(M(A(count1[find(id-1)], w), A(count1[find(id+1)], w)), M(w, w))));
merge(id, id-1);
merge(id, id+1);
}
else if(vis[id-1] && id != 1) {
ans = A(ans, M(M(count1[find(id)], count1[find(id-1)]), h*(h+1)/2));
merge(id, id-1);
}
else if(vis[id+1] && id != n) {
ans = A(ans, M(M(count1[find(id)], count1[find(id+1)]), h*(h+1)/2));
merge(id, id+1);
}
// for(int i = 1; i<=n; i++) cerr << count[i] << " "; cerr << endl;
// cerr << ans << endl;
}
cout << ans%mod << endl;
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int t = 1;
if(mutipletestcase) cin >> t;
while(t--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6496 KB |
Output is correct |
5 |
Correct |
1 ms |
6496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6496 KB |
Output is correct |
2 |
Correct |
1 ms |
6496 KB |
Output is correct |
3 |
Correct |
13 ms |
9428 KB |
Output is correct |
4 |
Correct |
26 ms |
11984 KB |
Output is correct |
5 |
Correct |
26 ms |
12752 KB |
Output is correct |
6 |
Correct |
25 ms |
11984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6504 KB |
Output is correct |
2 |
Correct |
4 ms |
7268 KB |
Output is correct |
3 |
Correct |
14 ms |
9804 KB |
Output is correct |
4 |
Correct |
28 ms |
12760 KB |
Output is correct |
5 |
Correct |
28 ms |
12752 KB |
Output is correct |
6 |
Correct |
1 ms |
6504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6504 KB |
Output is correct |
2 |
Correct |
1 ms |
6500 KB |
Output is correct |
3 |
Correct |
4 ms |
7252 KB |
Output is correct |
4 |
Correct |
14 ms |
9668 KB |
Output is correct |
5 |
Correct |
28 ms |
13000 KB |
Output is correct |
6 |
Correct |
29 ms |
13260 KB |
Output is correct |
7 |
Correct |
1 ms |
6500 KB |
Output is correct |
8 |
Correct |
3 ms |
7388 KB |
Output is correct |
9 |
Correct |
13 ms |
9716 KB |
Output is correct |
10 |
Correct |
20 ms |
12672 KB |
Output is correct |
11 |
Correct |
20 ms |
12752 KB |
Output is correct |
12 |
Correct |
1 ms |
6500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6488 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6620 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
13 ms |
9420 KB |
Output is correct |
12 |
Correct |
26 ms |
12088 KB |
Output is correct |
13 |
Correct |
25 ms |
11968 KB |
Output is correct |
14 |
Correct |
25 ms |
12396 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
3 ms |
7332 KB |
Output is correct |
17 |
Correct |
15 ms |
9660 KB |
Output is correct |
18 |
Correct |
29 ms |
13372 KB |
Output is correct |
19 |
Correct |
32 ms |
13516 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
3 ms |
7256 KB |
Output is correct |
22 |
Correct |
13 ms |
9704 KB |
Output is correct |
23 |
Correct |
20 ms |
13004 KB |
Output is correct |
24 |
Correct |
21 ms |
12748 KB |
Output is correct |
25 |
Correct |
1 ms |
6488 KB |
Output is correct |
26 |
Correct |
1 ms |
6488 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
6 ms |
7256 KB |
Output is correct |
31 |
Correct |
5 ms |
7512 KB |
Output is correct |
32 |
Correct |
15 ms |
9788 KB |
Output is correct |
33 |
Correct |
16 ms |
9936 KB |
Output is correct |
34 |
Correct |
27 ms |
12608 KB |
Output is correct |
35 |
Correct |
45 ms |
12992 KB |
Output is correct |
36 |
Correct |
30 ms |
12740 KB |
Output is correct |
37 |
Correct |
29 ms |
12736 KB |
Output is correct |
38 |
Correct |
1 ms |
6488 KB |
Output is correct |
39 |
Correct |
27 ms |
12748 KB |
Output is correct |
40 |
Correct |
41 ms |
12780 KB |
Output is correct |
41 |
Correct |
22 ms |
12740 KB |
Output is correct |
42 |
Correct |
21 ms |
12748 KB |
Output is correct |