#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define pii pair<ll, ll>
#define fi first
#define se second
#define bit(x, i) ((x >> i) & 1)
#define SZ(x) ((int)(x.size()))
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (int i = (a); i >= (b); --i)
#define task "BRICKWALL"
using namespace std;
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r) { assert(l <= r); return uniform_int_distribution<ll>(l, r)(rd); }
const int MXN = 1e5 + 5;
const int MOD = 1e9 + 7;
const int BASE = 3137;
const int BL = 320;
int n;
int l[MXN], r[MXN], st[MXN], top;
ll pre[MXN], h[MXN], w[MXN], ans;
ll getsum(int l, int r) {
return (pre[r] - pre[l - 1] + MOD) % MOD;
}
ll T(ll a, ll b) {
return (a * (a + 1) / 2 % MOD) * (b * (b + 1) / 2 % MOD) % MOD;
}
void solution() {
cin >> n;
FOR(i, 1, n) cin >> h[i];
FOR(i, 1, n) {
cin >> w[i];
pre[i] = (pre[i - 1] + w[i]) % MOD;
}
st[top = 0] = 0;
FOR(i, 1, n) {
while (top && h[st[top]] > h[i]) top--;
l[i] = st[top] + 1;
st[++top] = i;
}
st[top = 0] = n + 1;
FORD(i, n, 1) {
while (top && h[st[top]] >= h[i]) top--;
r[i] = st[top] - 1;
st[++top] = i;
}
FOR(i, 1, n) (ans += (h[i] * (h[i] + 1) / 2 % MOD) * (w[i] * (w[i] + 1) / 2 % MOD)) %= MOD;
FOR(i, 1, n) {
(ans += T(h[i], getsum(l[i], r[i]))) %= MOD;
ans = (ans - T(h[i], getsum(l[i], i - 1)) + MOD) % MOD;
ans = (ans - T(h[i], getsum(i + 1, r[i])) + MOD) % MOD;
ans = (ans - T(h[i], w[i]) + MOD) % MOD;
}
cout << ans;
}
int32_t main() {
if (fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); }
ios::sync_with_stdio(0); cin.tie(0);
int ntest = 1; //cin >> ntest;
while (ntest--) solution();
cerr << "\n" << 1.0 * clock() / CLOCKS_PER_SEC << "s ";
}
Compilation message
fancyfence.cpp: In function 'int32_t main()':
fancyfence.cpp:68:42: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | if (fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:68:75: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | if (fopen(task".inp", "r")) { freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2556 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
9 ms |
3672 KB |
Output is correct |
4 |
Correct |
16 ms |
4988 KB |
Output is correct |
5 |
Correct |
17 ms |
4848 KB |
Output is correct |
6 |
Correct |
16 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2604 KB |
Output is correct |
2 |
Correct |
3 ms |
2904 KB |
Output is correct |
3 |
Correct |
10 ms |
4188 KB |
Output is correct |
4 |
Correct |
21 ms |
5704 KB |
Output is correct |
5 |
Correct |
20 ms |
5976 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
3 ms |
2908 KB |
Output is correct |
4 |
Correct |
10 ms |
4188 KB |
Output is correct |
5 |
Correct |
20 ms |
5856 KB |
Output is correct |
6 |
Correct |
23 ms |
5860 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
2908 KB |
Output is correct |
9 |
Correct |
10 ms |
4416 KB |
Output is correct |
10 |
Correct |
18 ms |
5724 KB |
Output is correct |
11 |
Correct |
19 ms |
5880 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 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 |
2544 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
2396 KB |
Output is correct |
11 |
Correct |
9 ms |
3676 KB |
Output is correct |
12 |
Correct |
20 ms |
4956 KB |
Output is correct |
13 |
Correct |
16 ms |
4700 KB |
Output is correct |
14 |
Correct |
15 ms |
4600 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
2 ms |
2908 KB |
Output is correct |
17 |
Correct |
10 ms |
4260 KB |
Output is correct |
18 |
Correct |
19 ms |
5924 KB |
Output is correct |
19 |
Correct |
19 ms |
5968 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
2780 KB |
Output is correct |
22 |
Correct |
10 ms |
4184 KB |
Output is correct |
23 |
Correct |
18 ms |
5724 KB |
Output is correct |
24 |
Correct |
19 ms |
5828 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2520 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 |
3 ms |
2628 KB |
Output is correct |
31 |
Correct |
3 ms |
2652 KB |
Output is correct |
32 |
Correct |
10 ms |
3932 KB |
Output is correct |
33 |
Correct |
11 ms |
3912 KB |
Output is correct |
34 |
Correct |
20 ms |
5212 KB |
Output is correct |
35 |
Correct |
20 ms |
5324 KB |
Output is correct |
36 |
Correct |
23 ms |
5468 KB |
Output is correct |
37 |
Correct |
21 ms |
5312 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
19 ms |
5468 KB |
Output is correct |
40 |
Correct |
19 ms |
5360 KB |
Output is correct |
41 |
Correct |
19 ms |
5464 KB |
Output is correct |
42 |
Correct |
19 ms |
5720 KB |
Output is correct |