#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;
map<int, pair<ll, ll>> index_dim_map;
set<pair<ll, int>> height_index_set;
{
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 curr_it = index_dim_map.lower_bound(ind_curr);
assert(curr_it->first == ind_curr);
auto [h_curr, w_curr] = index_dim_map[ind_curr];
ll h_prev = 0, w_prev = 0, h_next = 0, w_next = 0;
int ind_prev = -1, ind_next = -1;
auto first_it = index_dim_map.begin();
auto last_it = index_dim_map.end();
last_it--;
if (curr_it != last_it) {
auto next_it = curr_it;
next_it++;
ind_next = next_it->first;
tie(h_next, w_next) = next_it->second;
}
if (curr_it != first_it) {
auto prev_it = curr_it;
prev_it--;
ind_prev = prev_it->first;
tie(h_prev, w_prev) = prev_it->second;
}
assert(h_curr > h_prev);
assert(h_curr > h_next);
res += solve_rect(h_curr, w_curr);
res %= mod;
index_dim_map.erase(curr_it);
if (h_prev > h_next) {
res -= solve_rect(h_prev, w_curr);
res += mod;
res %= mod;
index_dim_map[ind_prev].second = (w_prev + w_curr) % mod;
}
else if (h_next > h_prev) {
res -= solve_rect(h_next, w_curr);
res += mod;
res %= mod;
index_dim_map[ind_next].second = (w_next + w_curr) % mod;
}
else {
res -= solve_rect(h_prev, w_curr);
res += mod;
res %= mod;
index_dim_map[ind_prev].second = (w_prev + w_curr + w_next) % mod;
height_index_set.erase({-h_next, ind_next});
index_dim_map.erase(ind_next);
}
}
assert(height_index_set.size() == 1);
assert(index_dim_map.size() == 1);
auto [h, w] = index_dim_map.begin()->second;
res += solve_rect(h, w);
res %= mod;
cout << res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
316 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
23 ms |
4596 KB |
Output is correct |
4 |
Correct |
35 ms |
7004 KB |
Output is correct |
5 |
Correct |
41 ms |
9040 KB |
Output is correct |
6 |
Correct |
25 ms |
5204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
596 KB |
Output is correct |
3 |
Correct |
10 ms |
2004 KB |
Output is correct |
4 |
Correct |
23 ms |
3800 KB |
Output is correct |
5 |
Correct |
25 ms |
3916 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
668 KB |
Output is correct |
4 |
Correct |
14 ms |
1988 KB |
Output is correct |
5 |
Correct |
28 ms |
3788 KB |
Output is correct |
6 |
Correct |
26 ms |
3912 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
8 ms |
1876 KB |
Output is correct |
9 |
Correct |
23 ms |
4540 KB |
Output is correct |
10 |
Correct |
76 ms |
16204 KB |
Output is correct |
11 |
Correct |
78 ms |
16220 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
320 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 |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
316 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
468 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 |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
21 ms |
4688 KB |
Output is correct |
12 |
Correct |
30 ms |
7012 KB |
Output is correct |
13 |
Correct |
42 ms |
8984 KB |
Output is correct |
14 |
Correct |
28 ms |
5192 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
596 KB |
Output is correct |
17 |
Correct |
12 ms |
1996 KB |
Output is correct |
18 |
Correct |
23 ms |
3788 KB |
Output is correct |
19 |
Correct |
22 ms |
3912 KB |
Output is correct |
20 |
Correct |
1 ms |
464 KB |
Output is correct |
21 |
Correct |
7 ms |
1808 KB |
Output is correct |
22 |
Correct |
21 ms |
4556 KB |
Output is correct |
23 |
Correct |
78 ms |
16204 KB |
Output is correct |
24 |
Correct |
96 ms |
16284 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
352 KB |
Output is correct |
28 |
Correct |
1 ms |
356 KB |
Output is correct |
29 |
Correct |
1 ms |
468 KB |
Output is correct |
30 |
Correct |
9 ms |
1924 KB |
Output is correct |
31 |
Correct |
9 ms |
1876 KB |
Output is correct |
32 |
Correct |
50 ms |
8072 KB |
Output is correct |
33 |
Correct |
61 ms |
8236 KB |
Output is correct |
34 |
Correct |
164 ms |
16124 KB |
Output is correct |
35 |
Correct |
125 ms |
16040 KB |
Output is correct |
36 |
Correct |
150 ms |
16232 KB |
Output is correct |
37 |
Correct |
183 ms |
16328 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
156 ms |
16276 KB |
Output is correct |
40 |
Correct |
118 ms |
16280 KB |
Output is correct |
41 |
Correct |
101 ms |
16340 KB |
Output is correct |
42 |
Correct |
84 ms |
16300 KB |
Output is correct |