#include<bits/stdc++.h>
using namespace std;
constexpr int mod = 1000000007;
void mpl(int &x,int y) {
x += y;
if(x >= mod) x -= mod;
}
struct UnionFind {
vector<int>par,size,sum;
UnionFind(int n,vector<int>w) {
par.resize(n);
size.resize(n);
sum.resize(n);
for(int i = 0; i < n; i++) {
par[i] = i;
sum[i] = w[i];
}
}
int find(int x) {
if(par[x] == x) {
return x;
}
return par[x] = find(par[x]);
}
void unite(int u,int v) {
u = find(u);
v = find(v);
if(u == v) {
return;
}
if(size[u] > size[v]) swap(u,v);
size[v] += size[u];
mpl(sum[v],sum[u]);
par[u] = v;
}
int consum(int x) {
return sum[find(x)];
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N;
cin >> N;
vector<int>h(N),w(N),cmp;
for(int i = 0; i < N; i++) {
cin >> h[i];
cmp.push_back(h[i]);
}
for(int i = 0; i < N; i++) {
cin >> w[i];
}
cmp.push_back(0);
sort(cmp.begin(),cmp.end());
cmp.erase(unique(cmp.begin(),cmp.end()),cmp.end());
vector<vector<int>>tmp(cmp.size());
for(int i = 0; i < N; i++) {
h[i] = lower_bound(cmp.begin(),cmp.end(),h[i])-cmp.begin();
tmp[h[i]].push_back(i);
}
UnionFind uf(N,w);
int ans = 0,sum = 0;
for(int i = cmp.size()-1; i >= 1; i--) {
for(int j = 0; j < tmp[i].size(); j++) {
mpl(sum,1ll*(1+w[tmp[i][j]])*(w[tmp[i][j]])/2%mod);
if(tmp[i][j] && h[tmp[i][j]-1] >= h[tmp[i][j]]) {
int sum1 = uf.consum(tmp[i][j]-1);
int sum2 = uf.consum(tmp[i][j]);
uf.unite(tmp[i][j]-1,tmp[i][j]);
mpl(sum,1ll*sum1*sum2%mod);
}
if(tmp[i][j]+1 < N && h[tmp[i][j]+1] > h[tmp[i][j]]) {
int sum1 = uf.consum(tmp[i][j]);
int sum2 = uf.consum(tmp[i][j]+1);
uf.unite(tmp[i][j],tmp[i][j]+1);
mpl(sum,1ll*sum1*sum2%mod);
}
}
int res = 1ll*(1+cmp[i])*cmp[i]/2%mod;
mpl(res,mod-1ll*(1+cmp[i-1])*cmp[i-1]/2%mod);
mpl(ans,1ll*res*sum%mod);
}
cout << ans << "\n";
}
Compilation message
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:68:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for(int j = 0; j < tmp[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
11 ms |
1948 KB |
Output is correct |
4 |
Correct |
21 ms |
3796 KB |
Output is correct |
5 |
Correct |
21 ms |
3824 KB |
Output is correct |
6 |
Correct |
21 ms |
3516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
596 KB |
Output is correct |
3 |
Correct |
12 ms |
2004 KB |
Output is correct |
4 |
Correct |
27 ms |
3568 KB |
Output is correct |
5 |
Correct |
27 ms |
3608 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
596 KB |
Output is correct |
4 |
Correct |
12 ms |
1936 KB |
Output is correct |
5 |
Correct |
25 ms |
3540 KB |
Output is correct |
6 |
Correct |
27 ms |
3564 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
4 ms |
1108 KB |
Output is correct |
9 |
Correct |
19 ms |
3160 KB |
Output is correct |
10 |
Correct |
38 ms |
8456 KB |
Output is correct |
11 |
Correct |
38 ms |
8524 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
11 ms |
1952 KB |
Output is correct |
12 |
Correct |
22 ms |
3836 KB |
Output is correct |
13 |
Correct |
21 ms |
3684 KB |
Output is correct |
14 |
Correct |
20 ms |
3628 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
13 ms |
1944 KB |
Output is correct |
18 |
Correct |
27 ms |
3540 KB |
Output is correct |
19 |
Correct |
30 ms |
3536 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
4 ms |
1108 KB |
Output is correct |
22 |
Correct |
18 ms |
3124 KB |
Output is correct |
23 |
Correct |
40 ms |
8608 KB |
Output is correct |
24 |
Correct |
40 ms |
8524 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
5 ms |
1108 KB |
Output is correct |
31 |
Correct |
5 ms |
1108 KB |
Output is correct |
32 |
Correct |
17 ms |
1956 KB |
Output is correct |
33 |
Correct |
25 ms |
4384 KB |
Output is correct |
34 |
Correct |
62 ms |
8212 KB |
Output is correct |
35 |
Correct |
35 ms |
3660 KB |
Output is correct |
36 |
Correct |
64 ms |
8512 KB |
Output is correct |
37 |
Correct |
63 ms |
6272 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
54 ms |
5680 KB |
Output is correct |
40 |
Correct |
48 ms |
8396 KB |
Output is correct |
41 |
Correct |
39 ms |
8536 KB |
Output is correct |
42 |
Correct |
34 ms |
8548 KB |
Output is correct |