#include <bits/stdc++.h>
using namespace std;
#define int int64_t
#define ld long double
#define ar array
#define all(a) a.begin(), a.end()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 1e12;
const int MOD = 1e9+7;
const int inv2 = 5e8+4;
struct UFDS {
vector<int> p, wn;
UFDS(int n) : p(n,-1), wn(n, 0) {}
int fin(int v) { return p[v] < 0 ? v : p[v] = fin(p[v]); }
bool join(int a, int b) {
a = fin(a), b = fin(b);
if (a == b) return false;
if (-p[a] < -p[b]) swap(a, b);
p[a] += p[b]; p[b] = a; (wn[a] += wn[b]) %= MOD;
return true;
}
};
void solve() {
int N; cin >> N;
vector<int> h(N), w(N);
UFDS ufds(N);
for (int &i : h) cin >> i;
for (int &i : w) cin >> i;
for (int i = 0; i < N; i++) ufds.wn[i] = w[i];
vector<int> order(N);
iota(order.begin(), order.end(), 0);
sort(order.begin(), order.end(), [&](int a, int b) { return h[a] > h[b]; });
int ans = 0;
vector<bool> act(N, false);
for (int x : order) {
int lw = 0, rw = 0;
if (x-1 >= 0 && act[x-1]) {
lw = ufds.wn[ufds.fin(x-1)];
ufds.join(x, x-1);
}
if (x+1 < N && act[x+1]) {
rw = ufds.wn[ufds.fin(x+1)];
ufds.join(x, x+1);
}
int a = (h[x] * (h[x] + 1) % MOD) * inv2 % MOD;
int b = (w[x] * ufds.wn[ufds.fin(x)]) % MOD - ((w[x] - 1) * w[x] % MOD * inv2 % MOD);
b = (b % MOD + MOD) % MOD;
ans += (a * b % MOD) + ((lw * rw % MOD) * a % MOD) % MOD;
ans %= MOD;
act[x] = true;
}
ans = (ans + MOD) % MOD;
cout << ans << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1; //cin >> t;
while (t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
15 ms |
2764 KB |
Output is correct |
4 |
Correct |
29 ms |
5392 KB |
Output is correct |
5 |
Correct |
31 ms |
5380 KB |
Output is correct |
6 |
Correct |
29 ms |
5380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
844 KB |
Output is correct |
3 |
Correct |
17 ms |
3148 KB |
Output is correct |
4 |
Correct |
35 ms |
6084 KB |
Output is correct |
5 |
Correct |
36 ms |
6268 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
4 ms |
844 KB |
Output is correct |
4 |
Correct |
17 ms |
3272 KB |
Output is correct |
5 |
Correct |
33 ms |
6144 KB |
Output is correct |
6 |
Correct |
37 ms |
6268 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
4 ms |
844 KB |
Output is correct |
9 |
Correct |
18 ms |
3280 KB |
Output is correct |
10 |
Correct |
33 ms |
5964 KB |
Output is correct |
11 |
Correct |
33 ms |
6136 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
15 ms |
2884 KB |
Output is correct |
12 |
Correct |
29 ms |
5372 KB |
Output is correct |
13 |
Correct |
30 ms |
5380 KB |
Output is correct |
14 |
Correct |
30 ms |
5316 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
4 ms |
844 KB |
Output is correct |
17 |
Correct |
17 ms |
3252 KB |
Output is correct |
18 |
Correct |
33 ms |
6128 KB |
Output is correct |
19 |
Correct |
39 ms |
6248 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
4 ms |
844 KB |
Output is correct |
22 |
Correct |
18 ms |
3276 KB |
Output is correct |
23 |
Correct |
33 ms |
6008 KB |
Output is correct |
24 |
Correct |
33 ms |
6132 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
5 ms |
844 KB |
Output is correct |
31 |
Correct |
5 ms |
844 KB |
Output is correct |
32 |
Correct |
20 ms |
3160 KB |
Output is correct |
33 |
Correct |
22 ms |
3148 KB |
Output is correct |
34 |
Correct |
44 ms |
5880 KB |
Output is correct |
35 |
Correct |
40 ms |
6000 KB |
Output is correct |
36 |
Correct |
49 ms |
6164 KB |
Output is correct |
37 |
Correct |
46 ms |
6180 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
49 ms |
6192 KB |
Output is correct |
40 |
Correct |
46 ms |
6132 KB |
Output is correct |
41 |
Correct |
37 ms |
6180 KB |
Output is correct |
42 |
Correct |
34 ms |
6136 KB |
Output is correct |