#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(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] = par[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] >= cmp[i]) {
int sum1 = uf.consum(tmp[i][j]-1);
int sum2 = uf.consum(tmp[i][j]);
mpl(sum,1ll*sum1*sum2%mod);
}
if(tmp[i][j]+1 < N && h[tmp[i][j]+1] > cmp[i]) {
int sum1 = uf.consum(tmp[i][j]);
int sum2 = uf.consum(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 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |