#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define MAXN (1000005)
ld ans = 0, cursum = 0, mod = 1e9 + 7;
ll parent[MAXN], SIZE[MAXN];
ld tri(ld h){
return ((h + 1) * h) / ld(2); //the triangle sum formula is N*(N + 1) / 2 (basically the sum of 1,2,...,N)
}
ll find_set(ll x){
if(parent[x] == x) return x;
parent[x] = find_set(parent[x]);
return parent[x];
}
bool same_set(ll x,ll y){
return find_set(x) == find_set(y);
}
void merge_set(ll x,ll y){
if(same_set(x,y)) return;
ll Xsize = SIZE[find_set(x)], Ysize = SIZE[find_set(y)];
/*cursum -= (((Xsize + 1) * Xsize) / 2);
cursum -= (((Ysize + 1) * Ysize) / 2);
cursum = (cursum + mod) % mod;
ll totalsize = Xsize + Ysize;
cursum += (((totalsize + 1) * totalsize) / 2);
cursum = (cursum + mod) % mod;*/
cursum += (Xsize * Ysize);
cursum = fmod(cursum + mod,mod);
ll totalsize = Xsize + Ysize;
parent[find_set(x)] = find_set(y);
SIZE[find_set(x)] = totalsize;
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
ll N;
cin>>N;
pair<pair<ll,ll>,ll> arr[N];
for(ll i = 0;i < N;i++){
cin>>arr[i].first.first; //H[i]
}
for(ll i = 0;i < N;i++){
cin>>arr[i].first.second; //W[i]
}
for(ll i = 0;i < N;i++){
arr[i].second = i;
parent[i] = i;
SIZE[i] = arr[i].first.second;
}
set<ll,greater<ll> > heights;
for(ll i = 0;i < N;i++){
heights.insert(arr[i].first.first);
}
sort(arr,arr + N,greater<pair<pair<ll,ll>,ll> >());
bool can[N];
memset(can,0,sizeof(can));
ll ptr = -1;
for(auto h : heights){
cursum = 0;
while(ptr + 1 < N && arr[ptr + 1].first.first == h){
ptr++;
ll ind = arr[ptr].second;
can[ind] = 1;
cursum += (((arr[ptr].first.second + 1) * arr[ptr].first.second) / 2);
cursum = fmod(cursum + mod,mod);
if(ind >= 1 && can[ind - 1]){
//cout<<"MERGED: "<<ind - 1<<" "<<ind<<'\n';
merge_set(ind - 1,ind);
}
cursum = fmod(cursum + mod,mod);
if(ind < N - 1 && can[ind + 1]){
//cout<<"MERGED: "<<ind<<" "<<ind + 1<<'\n';
merge_set(ind,ind + 1);
}
cursum = fmod(cursum + mod,mod);
}
//cout<<"HEIGHT = "<<h<<" have cursum of "<<cursum<<'\n';
ans += (fmod(cursum + mod,mod) * fmod(tri(h),mod));
ans = fmod(ans + mod,mod);
}
cout<<fmod(ans + mod,mod)<<'\n';
}
/*
2
2 2
1 2
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Incorrect |
2 ms |
2396 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |