#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll M = 1000000007;
ll n;
ll h[101010], w[101010];
ll hst[303030], wst[303030];
void hinit(ll l, ll r, ll id) {
if (l == r) {
hst[id] = h[l];
return;
}
ll m = (l + r) / 2;
hinit(l, m, 2 * id);
hinit(m + 1, r, 2 * id + 1);
hst[id] = min(hst[2 * id], hst[2 * id + 1]);
}
void winit(ll l, ll r, ll id) {
if (l == r) {
wst[id] = w[l];
return;
}
ll m = (l + r) / 2;
winit(l, m, 2 * id);
winit(m + 1, r, 2 * id + 1);
wst[id] = wst[2 * id] + wst[2 * id + 1];
}
ll hqry(ll l, ll r, ll id, ll s, ll e) {
if (l > e || s > r)return M;
if (s <= l && r <= e)return hst[id];
ll m = (l + r) / 2;
return min(hqry(l, m, 2 * id, s, e), hqry(m + 1, r, 2 * id + 1, s, e));
}
ll wqry(ll l, ll r, ll id, ll s, ll e) {
if (l > e || s > r)return 0;
if (s <= l && r <= e)return wst[id];
ll m = (l + r) / 2;
return wqry(l, m, 2 * id, s, e) + wqry(m + 1, r, 2 * id + 1, s, e);
}
ll f(ll s, ll e, ll t) {
if (s > e)return 0;
ll mh = hqry(0, n - 1, 1, s, e);
if (mh == t) {
ll l = s, r = e, m;
while (l < r) {
m = (l + r) / 2;
if (hqry(0, n - 1, 1, s, m) == t) {
r = m;
}
else {
l = m + 1;
}
}
return (f(s, l - 1, t) + f(l + 1, e, t)) % M;
}
else {
ll hn = (mh * (mh + 1) - t * (t + 1)) / 2;
hn %= M;
ll ws = wqry(0, n - 1, 1, s, e) % (2 * M), wn = ws * (ws + 1) / 2 % M;
ll r = wn * hn % M;
return (r + f(s, e, mh)) % M;
}
}
int main() {
ll i;
scanf("%lld", &n);
for (i = 0; i < n; i++) {
scanf("%lld", &h[i]);
}
for (i = 0; i < n; i++) {
scanf("%lld", &w[i]);
}
hinit(0, n - 1, 1);
winit(0, n - 1, 1);
printf("%lld", f(0, n - 1, 0));
return 0;
}
Compilation message
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:67:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | scanf("%lld", &n);
| ~~~~~^~~~~~~~~~~~
fancyfence.cpp:69:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | scanf("%lld", &h[i]);
| ~~~~~^~~~~~~~~~~~~~~
fancyfence.cpp:72:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%lld", &w[i]);
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
81 ms |
5220 KB |
Output is correct |
4 |
Correct |
278 ms |
13000 KB |
Output is correct |
5 |
Correct |
187 ms |
9780 KB |
Output is correct |
6 |
Correct |
106 ms |
7480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
24 ms |
1716 KB |
Output is correct |
3 |
Correct |
148 ms |
7012 KB |
Output is correct |
4 |
Correct |
321 ms |
13748 KB |
Output is correct |
5 |
Correct |
326 ms |
13756 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
28 ms |
1676 KB |
Output is correct |
4 |
Correct |
158 ms |
6904 KB |
Output is correct |
5 |
Correct |
339 ms |
13812 KB |
Output is correct |
6 |
Correct |
313 ms |
13732 KB |
Output is correct |
7 |
Correct |
2 ms |
460 KB |
Output is correct |
8 |
Correct |
29 ms |
2616 KB |
Output is correct |
9 |
Correct |
158 ms |
9256 KB |
Output is correct |
10 |
Correct |
341 ms |
22264 KB |
Output is correct |
11 |
Correct |
353 ms |
22272 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
408 KB |
Output is correct |
10 |
Correct |
2 ms |
416 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
284 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
82 ms |
5368 KB |
Output is correct |
12 |
Correct |
283 ms |
12916 KB |
Output is correct |
13 |
Correct |
167 ms |
9852 KB |
Output is correct |
14 |
Correct |
113 ms |
7544 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
22 ms |
1848 KB |
Output is correct |
17 |
Correct |
149 ms |
7700 KB |
Output is correct |
18 |
Correct |
308 ms |
14456 KB |
Output is correct |
19 |
Correct |
323 ms |
14492 KB |
Output is correct |
20 |
Correct |
2 ms |
424 KB |
Output is correct |
21 |
Correct |
25 ms |
2644 KB |
Output is correct |
22 |
Correct |
155 ms |
9384 KB |
Output is correct |
23 |
Correct |
372 ms |
22312 KB |
Output is correct |
24 |
Correct |
372 ms |
22344 KB |
Output is correct |
25 |
Correct |
1 ms |
280 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
320 KB |
Output is correct |
28 |
Correct |
1 ms |
300 KB |
Output is correct |
29 |
Correct |
2 ms |
336 KB |
Output is correct |
30 |
Correct |
9 ms |
1100 KB |
Output is correct |
31 |
Correct |
10 ms |
1056 KB |
Output is correct |
32 |
Correct |
50 ms |
3908 KB |
Output is correct |
33 |
Correct |
49 ms |
3808 KB |
Output is correct |
34 |
Correct |
104 ms |
6732 KB |
Output is correct |
35 |
Correct |
98 ms |
6808 KB |
Output is correct |
36 |
Correct |
100 ms |
6732 KB |
Output is correct |
37 |
Correct |
105 ms |
6616 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
122 ms |
6724 KB |
Output is correct |
40 |
Correct |
224 ms |
6832 KB |
Output is correct |
41 |
Correct |
310 ms |
10028 KB |
Output is correct |
42 |
Correct |
333 ms |
22272 KB |
Output is correct |