#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
ll solve_rect(ll h, ll w) {
h %= mod;
w %= mod;
ll hc = (h * (h + 1) / 2) % mod;
ll wc = (w * (w + 1) / 2) % mod;
return (hc * wc) % mod;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
set<pair<ll, int>, greater<pair<ll, int>>> height_index_set;
map<int, pair<ll, ll>> index_dim_map;
height_index_set.insert({0, -1});
height_index_set.insert({0, n});
index_dim_map[-1] = {0, 0};
index_dim_map[n] = {0, 0};
{
vector<ll> hv(n), wv(n);
for (ll& h : hv) cin >> h;
for (ll& w : wv) cin >> w;
for (int i = 0; i < n; i++) {
if (i != n + 1 && hv[i] == hv[i + 1]) {
wv[i + 1] += wv[i];
continue;
}
index_dim_map[i] = {hv[i], wv[i]};
height_index_set.insert({hv[i], i});
}
}
ll res = 0;
while (height_index_set.size() > 1) {
auto _it = height_index_set.begin();
int ind_curr = _it->second;
height_index_set.erase(_it);
// should always be the iterator at key ind_curr
auto it_curr = index_dim_map.lower_bound(ind_curr);
auto& [h_curr, w_curr] = it_curr->second;
auto it_next = it_curr;
it_next++;
auto& [h_next, w_next] = it_next->second;
auto it_prev = it_curr;
it_prev--;
auto& [h_prev, w_prev] = it_prev->second;
res += solve_rect(h_curr, w_curr);
if (h_prev > h_next) {
res -= solve_rect(h_prev, w_curr);
w_prev = (w_prev + w_curr) % mod;
}
else if (h_prev < h_next) {
res -= solve_rect(h_next, w_curr);
w_next = (w_next + w_curr) % mod;
}
else {
res -= solve_rect(h_prev, w_curr);
w_prev = (w_prev + w_curr + w_next) % mod;
height_index_set.erase({h_next, it_next->first});
index_dim_map.erase(it_next);
}
res = (res % mod + mod) % mod;
index_dim_map.erase(it_curr);
}
cout << res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
22 ms |
4052 KB |
Output is correct |
4 |
Correct |
40 ms |
5844 KB |
Output is correct |
5 |
Correct |
50 ms |
7828 KB |
Output is correct |
6 |
Correct |
22 ms |
4088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
476 KB |
Output is correct |
3 |
Correct |
10 ms |
1108 KB |
Output is correct |
4 |
Correct |
19 ms |
1908 KB |
Output is correct |
5 |
Correct |
22 ms |
1876 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
4 ms |
468 KB |
Output is correct |
4 |
Correct |
10 ms |
1108 KB |
Output is correct |
5 |
Correct |
21 ms |
1876 KB |
Output is correct |
6 |
Correct |
23 ms |
1876 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
8 ms |
1628 KB |
Output is correct |
9 |
Correct |
19 ms |
3564 KB |
Output is correct |
10 |
Correct |
70 ms |
14312 KB |
Output is correct |
11 |
Correct |
86 ms |
14324 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
212 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 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 |
0 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 |
25 ms |
4052 KB |
Output is correct |
12 |
Correct |
31 ms |
5836 KB |
Output is correct |
13 |
Correct |
41 ms |
7884 KB |
Output is correct |
14 |
Correct |
22 ms |
4020 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
9 ms |
1108 KB |
Output is correct |
18 |
Correct |
19 ms |
1876 KB |
Output is correct |
19 |
Correct |
22 ms |
1900 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
7 ms |
1620 KB |
Output is correct |
22 |
Correct |
21 ms |
3556 KB |
Output is correct |
23 |
Correct |
77 ms |
14308 KB |
Output is correct |
24 |
Correct |
87 ms |
14332 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 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 |
9 ms |
1624 KB |
Output is correct |
31 |
Correct |
9 ms |
1620 KB |
Output is correct |
32 |
Correct |
55 ms |
7260 KB |
Output is correct |
33 |
Correct |
50 ms |
7336 KB |
Output is correct |
34 |
Correct |
147 ms |
14312 KB |
Output is correct |
35 |
Correct |
117 ms |
14156 KB |
Output is correct |
36 |
Correct |
147 ms |
14324 KB |
Output is correct |
37 |
Correct |
156 ms |
14280 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
144 ms |
14440 KB |
Output is correct |
40 |
Correct |
103 ms |
14388 KB |
Output is correct |
41 |
Correct |
91 ms |
14392 KB |
Output is correct |
42 |
Correct |
74 ms |
14284 KB |
Output is correct |