#include <bits/stdc++.h>
using namespace std;
#define int long long
const __int128 mod = 1e9 + 7;
__int128 add(__int128 a, __int128 b)
{
return (((a + b) % mod) + mod) % mod;
}
__int128 mult(__int128 a, __int128 b)
{
return (((a * b) % mod) + mod) % mod;
}
__int128 sum(__int128 l, __int128 r)
{
return ((r - l + 1) * (l + r) / 2) % mod;
}
__int128 res = 0;
struct DSU
{
int n;
vector<int> e;
vector<__int128> 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, __int128 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, val[x] * val[y]);
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;
__int128 h[n + 1], w[n + 1];
for (int i = 1; i <= n; i++)
{
int x;
cin >> x;
h[i] = x;
}
for (int i = 1; i <= n; i++)
{
int x;
cin >> x;
w[i] = x;
}
map<__int128, vector<int>> mp;
for (int i = 1; i <= n; i++) mp[h[i]].push_back(i);
DSU dsu;
dsu.init(n);
__int128 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));
}
int couT = ans;
cout << couT << '\n';
}
Compilation message
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:85:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
85 | auto &[a, b] = *it;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
604 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 |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
10 ms |
4404 KB |
Output is correct |
4 |
Correct |
19 ms |
8020 KB |
Output is correct |
5 |
Correct |
19 ms |
7884 KB |
Output is correct |
6 |
Correct |
19 ms |
7924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
1116 KB |
Output is correct |
3 |
Correct |
11 ms |
4572 KB |
Output is correct |
4 |
Correct |
24 ms |
8808 KB |
Output is correct |
5 |
Correct |
22 ms |
8660 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
2 ms |
1116 KB |
Output is correct |
4 |
Correct |
11 ms |
4572 KB |
Output is correct |
5 |
Correct |
21 ms |
8540 KB |
Output is correct |
6 |
Correct |
22 ms |
8664 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
5 ms |
2264 KB |
Output is correct |
9 |
Correct |
18 ms |
6880 KB |
Output is correct |
10 |
Correct |
48 ms |
20052 KB |
Output is correct |
11 |
Correct |
48 ms |
20304 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
500 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
604 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 |
604 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 |
464 KB |
Output is correct |
11 |
Correct |
9 ms |
4308 KB |
Output is correct |
12 |
Correct |
18 ms |
8020 KB |
Output is correct |
13 |
Correct |
19 ms |
7888 KB |
Output is correct |
14 |
Correct |
18 ms |
7816 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
1116 KB |
Output is correct |
17 |
Correct |
11 ms |
4572 KB |
Output is correct |
18 |
Correct |
21 ms |
8660 KB |
Output is correct |
19 |
Correct |
22 ms |
8664 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
4 ms |
2396 KB |
Output is correct |
22 |
Correct |
18 ms |
6924 KB |
Output is correct |
23 |
Correct |
48 ms |
20048 KB |
Output is correct |
24 |
Correct |
49 ms |
20308 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 |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
604 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
5 ms |
2236 KB |
Output is correct |
31 |
Correct |
6 ms |
2392 KB |
Output is correct |
32 |
Correct |
13 ms |
4696 KB |
Output is correct |
33 |
Correct |
29 ms |
10320 KB |
Output is correct |
34 |
Correct |
75 ms |
19484 KB |
Output is correct |
35 |
Correct |
26 ms |
8532 KB |
Output is correct |
36 |
Correct |
80 ms |
20400 KB |
Output is correct |
37 |
Correct |
59 ms |
15444 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
50 ms |
13796 KB |
Output is correct |
40 |
Correct |
65 ms |
19976 KB |
Output is correct |
41 |
Correct |
54 ms |
20304 KB |
Output is correct |
42 |
Correct |
48 ms |
20200 KB |
Output is correct |