#include <bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9 + 7;
int add(int a, int b)
{
a %= mod, b %= mod;
return (((a + b) % mod) + mod) % mod;
}
int mult(int a, int b)
{
a %= mod, b %= mod;
return (((a * b) % mod) + mod) % mod;
}
int sum(int l, int r)
{
return ((r - l + 1) * (l + r) / 2) % mod;
}
int res = 0;
struct DSU
{
int n;
vector<int> e, val;
void init(int N)
{
n = N;
e.assign(n + 1, -1);
val.assign(n + 1, 0);
}
int get(int x)
{
if (e[x] < 0) return x;
return e[x] = get(e[x]);
}
void makeval(int x, int y)
{
x = get(x);
val[x] = y;
res = add(res, val[x] * (val[x] + 1) / 2);
}
void unite(int x, int y)
{
x = get(x), y = get(y);
if (x == y) return;
if (e[x] > e[y]) swap(x, y);
res = add(res, mult(val[x], val[y]));
val[x] = add(val[x], val[y]);
val[y] = 0;
e[x] += e[y];
e[y] = x;
}
};
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
int h[n + 1], w[n + 1];
for (int i = 1; i <= n; i++) cin >> h[i];
for (int i = 1; i <= n; i++) cin >> w[i];
map<int, vector<int>> mp;
for (int i = 1; i <= n; i++) mp[h[i]].push_back(i);
DSU dsu;
dsu.init(n);
int ans = 0;
for (auto it = mp.rbegin(); it != mp.rend(); it++)
{
auto &[a, b] = *it;
for (int i : b) dsu.makeval(i, w[i]);
for (int i : b)
{
if (i - 1 >= 1 && h[i - 1] >= a) dsu.unite(i, i - 1);
if (i + 1 <= n && h[i + 1] >= a) dsu.unite(i, i + 1);
}
auto it1 = it;
it1++;
int nxt = 0;
if (it1 != mp.rend()) nxt = (*it1).first;
ans = add(ans, mult(sum(nxt + 1, a), res));
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
600 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 |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 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 |
10 ms |
3032 KB |
Output is correct |
4 |
Correct |
19 ms |
5712 KB |
Output is correct |
5 |
Correct |
19 ms |
5584 KB |
Output is correct |
6 |
Correct |
18 ms |
5604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
976 KB |
Output is correct |
3 |
Correct |
11 ms |
3292 KB |
Output is correct |
4 |
Correct |
22 ms |
6352 KB |
Output is correct |
5 |
Correct |
25 ms |
6360 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
408 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
4 ms |
860 KB |
Output is correct |
4 |
Correct |
13 ms |
3292 KB |
Output is correct |
5 |
Correct |
22 ms |
6344 KB |
Output is correct |
6 |
Correct |
25 ms |
6348 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
5 ms |
1824 KB |
Output is correct |
9 |
Correct |
16 ms |
5560 KB |
Output is correct |
10 |
Correct |
43 ms |
16336 KB |
Output is correct |
11 |
Correct |
42 ms |
16220 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
488 KB |
Output is correct |
3 |
Correct |
1 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
10 ms |
3032 KB |
Output is correct |
12 |
Correct |
19 ms |
5708 KB |
Output is correct |
13 |
Correct |
19 ms |
5584 KB |
Output is correct |
14 |
Correct |
18 ms |
5620 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
860 KB |
Output is correct |
17 |
Correct |
11 ms |
3288 KB |
Output is correct |
18 |
Correct |
22 ms |
6360 KB |
Output is correct |
19 |
Correct |
22 ms |
6356 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
4 ms |
1884 KB |
Output is correct |
22 |
Correct |
16 ms |
5460 KB |
Output is correct |
23 |
Correct |
44 ms |
16208 KB |
Output is correct |
24 |
Correct |
43 ms |
16212 KB |
Output is correct |
25 |
Correct |
1 ms |
792 KB |
Output is correct |
26 |
Correct |
1 ms |
504 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
5 ms |
1824 KB |
Output is correct |
31 |
Correct |
5 ms |
1884 KB |
Output is correct |
32 |
Correct |
13 ms |
3420 KB |
Output is correct |
33 |
Correct |
29 ms |
8264 KB |
Output is correct |
34 |
Correct |
69 ms |
15640 KB |
Output is correct |
35 |
Correct |
25 ms |
6232 KB |
Output is correct |
36 |
Correct |
93 ms |
16468 KB |
Output is correct |
37 |
Correct |
68 ms |
12116 KB |
Output is correct |
38 |
Correct |
1 ms |
344 KB |
Output is correct |
39 |
Correct |
52 ms |
10836 KB |
Output is correct |
40 |
Correct |
56 ms |
16212 KB |
Output is correct |
41 |
Correct |
52 ms |
16472 KB |
Output is correct |
42 |
Correct |
41 ms |
16464 KB |
Output is correct |