-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path063-fancy-fence.cpp
More file actions
74 lines (73 loc) · 1.92 KB
/
Copy path063-fancy-fence.cpp
File metadata and controls
74 lines (73 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Fancy Fence [1402A]
* Problem: https://codeforces.com/problemset/problem/1402/A
* Verdict: ACCEPTED Solved: 2024-03-23
* Language: C++17 (GCC 7-32)
* Runtime: 139 ms Memory: 10400 KB
* Tags: *special, data structures, dsu, implementation, math, sortings
* Author: BidoTeima
* Source: https://codeforces.com/contest/1402/submission/252934304
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
using ll = long long;
const int N = 1e5 + 3, mod = 1e9 + 7;
pair<int,ll>sz[N];
ll ans,cnt;
int par[N], h[N], w[N], n;
int find_set(int a){
if(a == par[a]) return a;
return par[a] = find_set(par[a]);
}
void merge(int a, int b){
a = find_set(a), b = find_set(b);
if(a == b) return;
if(sz[a].first < sz[b].first) swap(a, b);
par[b] = a;
sz[a].first += sz[b].first;
cnt -= sz[a].second*(sz[a].second + 1) / 2 + sz[b].second*(sz[b].second + 1) / 2;
sz[a].second += sz[b].second;
sz[a].second %= mod;
cnt += sz[a].second*(sz[a].second + 1) / 2;
cnt = (cnt + mod*mod) % mod;
}
void make_set(int i){
par[i] = i;
sz[i].first = 1;
sz[i].second = w[i];
cnt += w[i]*1ll*(w[i] + 1) / 2;
cnt %= mod;
if(i > 0 && sz[i-1].first){
merge(i, i - 1);
}
if(i + 1 < n && sz[i+1].first){
merge(i, i + 1);
}
}
ll sum(ll l, ll r){
return (r*(r+1) - l*(l-1))>>1;
}
map<int,vector<int>>occ{};
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i = 0; i < n; i++)cin>>h[i],occ[h[i]].push_back(i);
for(int i = 0; i < n; i++)cin>>w[i];
sort(h,h+n,greater<int>());
h[n] = 0;
for(int i = 0; i < n; i++){
if(h[i] == h[i + 1]) continue;
vector<int>&v = occ[h[i]];
for(int x : v){
make_set(x);
}
ans += (sum(h[i + 1] + 1, h[i])%mod * cnt%mod)%mod;
ans %= mod;
}
//cout<<"y";
cout<<ans;
return 0;
}