#include <set>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int mod = 1000000007;
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
int N;
cin >> N;
vector<int> H(N), W(N);
for(int i = 0; i < N; ++i) cin >> H[i];
for(int i = 0; i < N; ++i) cin >> W[i];
vector<int> WS(N + 1);
for(int i = 0; i < N; ++i) {
WS[i + 1] = WS[i] + W[i];
if(WS[i + 1] >= mod) WS[i + 1] -= mod;
}
vector<int> perm(N);
for(int i = 0; i < N; ++i) {
perm[i] = i;
}
sort(perm.begin(), perm.end(), [&](int i, int j) { return H[i] > H[j]; });
set<pair<int, int> > s;
long long current = 0;
int ans = 0;
for(int i = 0; i < perm.size(); ++i) {
int id = perm[i];
current = (current + 1LL * W[id] * (W[id] + 1)) % mod;
set<pair<int, int> >::iterator it = s.lower_bound(make_pair(id, -1));
int cl = id, cr = id + 1;
if(it != s.end()) {
if(it->first == cr) {
// connect!
current -= 1LL * (WS[cr] - WS[cl] + mod) * (WS[cr] - WS[cl] + mod + 1) % mod;
current -= 1LL * (WS[it->second] - WS[it->first] + mod) * (WS[it->second] - WS[it->first] + mod + 1) % mod;
cr = it->second;
current += 1LL * (WS[cr] - WS[cl] + mod) * (WS[cr] - WS[cl] + mod + 1) % mod;
it = s.erase(it);
}
}
if(it != s.begin()) {
--it;
if(it->second == cl) {
// connect!
current -= 1LL * (WS[cr] - WS[cl] + mod) * (WS[cr] - WS[cl] + mod + 1) % mod;
current -= 1LL * (WS[it->second] - WS[it->first] + mod) * (WS[it->second] - WS[it->first] + mod + 1) % mod;
cl = it->first;
current += 1LL * (WS[cr] - WS[cl] + mod) * (WS[cr] - WS[cl] + mod + 1) % mod;
it = s.erase(it);
}
}
s.insert(make_pair(cl, cr));
while(current < 0) current += mod;
while(current >= mod) current -= mod;
int hr = H[perm[i]], hl = (i + 1 != N ? H[perm[i + 1]] : 0);
int hc = (1LL * hr * (hr + 1) / 2 - 1LL * hl * (hl + 1) / 2) % mod;
int sub = 1LL * current * hc % mod;
ans = (ans + sub) % mod;
}
ans = 1LL * ans * (mod / 2 + 1) % mod;
cout << ans << endl;
return 0;
}
Compilation message
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:28:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | for(int i = 0; i < perm.size(); ++i) {
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
24 ms |
1664 KB |
Output is correct |
4 |
Correct |
43 ms |
2684 KB |
Output is correct |
5 |
Correct |
63 ms |
3064 KB |
Output is correct |
6 |
Correct |
41 ms |
2296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
512 KB |
Output is correct |
3 |
Correct |
25 ms |
1152 KB |
Output is correct |
4 |
Correct |
49 ms |
1912 KB |
Output is correct |
5 |
Correct |
39 ms |
1912 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
512 KB |
Output is correct |
4 |
Correct |
18 ms |
1152 KB |
Output is correct |
5 |
Correct |
40 ms |
2040 KB |
Output is correct |
6 |
Correct |
42 ms |
1972 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
512 KB |
Output is correct |
9 |
Correct |
24 ms |
1128 KB |
Output is correct |
10 |
Correct |
39 ms |
1960 KB |
Output is correct |
11 |
Correct |
36 ms |
1920 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
24 ms |
2304 KB |
Output is correct |
12 |
Correct |
44 ms |
3832 KB |
Output is correct |
13 |
Correct |
50 ms |
4132 KB |
Output is correct |
14 |
Correct |
47 ms |
3448 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
4 ms |
640 KB |
Output is correct |
17 |
Correct |
20 ms |
2048 KB |
Output is correct |
18 |
Correct |
41 ms |
3960 KB |
Output is correct |
19 |
Correct |
41 ms |
3960 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
640 KB |
Output is correct |
22 |
Correct |
21 ms |
2176 KB |
Output is correct |
23 |
Correct |
35 ms |
3744 KB |
Output is correct |
24 |
Correct |
48 ms |
3892 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
1 ms |
384 KB |
Output is correct |
29 |
Correct |
1 ms |
400 KB |
Output is correct |
30 |
Correct |
9 ms |
768 KB |
Output is correct |
31 |
Correct |
9 ms |
896 KB |
Output is correct |
32 |
Correct |
36 ms |
2552 KB |
Output is correct |
33 |
Correct |
40 ms |
2688 KB |
Output is correct |
34 |
Correct |
83 ms |
4864 KB |
Output is correct |
35 |
Correct |
87 ms |
4840 KB |
Output is correct |
36 |
Correct |
84 ms |
4984 KB |
Output is correct |
37 |
Correct |
114 ms |
4984 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
83 ms |
4088 KB |
Output is correct |
40 |
Correct |
59 ms |
3832 KB |
Output is correct |
41 |
Correct |
42 ms |
3840 KB |
Output is correct |
42 |
Correct |
46 ms |
3840 KB |
Output is correct |