#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAXN (1000005)
ll ans = 0, cursum = 0, mod = 1e9 + 7;
ll parent[MAXN], SIZE[MAXN];
ll tri(ll h){
return ((h + 1) * h) / 2ll; //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 * Ysize);
cursum %= mod;
ll totalsize = Xsize + Ysize;
totalsize %= mod;
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 %= mod;
if(ind >= 1 && can[ind - 1]){
merge_set(ind - 1,ind);
}
cursum %= mod;
if(ind < N - 1 && can[ind + 1]){
merge_set(ind,ind + 1);
}
cursum %= mod;
}
ans += ((cursum % mod) * (tri(h) % mod));
ans %= mod;
}
cout<<ans % mod<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
13 ms |
5724 KB |
Output is correct |
4 |
Correct |
25 ms |
9216 KB |
Output is correct |
5 |
Correct |
25 ms |
9228 KB |
Output is correct |
6 |
Correct |
25 ms |
9232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
3 ms |
2748 KB |
Output is correct |
3 |
Correct |
13 ms |
5908 KB |
Output is correct |
4 |
Correct |
27 ms |
9052 KB |
Output is correct |
5 |
Correct |
28 ms |
9132 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
3 ms |
2652 KB |
Output is correct |
4 |
Correct |
18 ms |
5908 KB |
Output is correct |
5 |
Correct |
30 ms |
9196 KB |
Output is correct |
6 |
Correct |
29 ms |
9208 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
4 ms |
3164 KB |
Output is correct |
9 |
Correct |
20 ms |
6748 KB |
Output is correct |
10 |
Correct |
42 ms |
13904 KB |
Output is correct |
11 |
Correct |
41 ms |
13912 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2404 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2392 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
12 ms |
5724 KB |
Output is correct |
12 |
Correct |
25 ms |
9052 KB |
Output is correct |
13 |
Correct |
24 ms |
9208 KB |
Output is correct |
14 |
Correct |
24 ms |
9048 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
3 ms |
2744 KB |
Output is correct |
17 |
Correct |
14 ms |
5724 KB |
Output is correct |
18 |
Correct |
27 ms |
9132 KB |
Output is correct |
19 |
Correct |
27 ms |
9052 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
4 ms |
3164 KB |
Output is correct |
22 |
Correct |
18 ms |
7000 KB |
Output is correct |
23 |
Correct |
35 ms |
13916 KB |
Output is correct |
24 |
Correct |
35 ms |
13812 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
4 ms |
3164 KB |
Output is correct |
31 |
Correct |
5 ms |
3164 KB |
Output is correct |
32 |
Correct |
17 ms |
5724 KB |
Output is correct |
33 |
Correct |
25 ms |
8272 KB |
Output is correct |
34 |
Correct |
49 ms |
13544 KB |
Output is correct |
35 |
Correct |
29 ms |
9052 KB |
Output is correct |
36 |
Correct |
51 ms |
13916 KB |
Output is correct |
37 |
Correct |
45 ms |
11860 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
41 ms |
11252 KB |
Output is correct |
40 |
Correct |
44 ms |
13648 KB |
Output is correct |
41 |
Correct |
36 ms |
13816 KB |
Output is correct |
42 |
Correct |
42 ms |
13656 KB |
Output is correct |