#include <bits/stdc++.h>
#define ll long long
#define all(x) x.begin(), x.end()
#define Neco "Fancy Fence"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 2e5 + 7
using namespace std;
const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r)
{
return uniform_int_distribution<ll> (l, r)(rng);
}
int n;
ll h[maxn], w[maxn];
ll s[maxn], Sum[maxn];
int top, q[maxn];
ll md(ll x) { return ((x % mod) + mod) % mod; }
ll calc(ll x) { return md(x * (x + 1) / 2); }
void solve()
{
cin >> n;
fi(i, 1, n) cin >> h[i];
fi(i, 1, n) cin >> w[i];
fi(i, 1, n) s[i] = md(s[i - 1] + w[i]);
ll ans = 0;
fi(i, 1, n) {
while(top && h[i] < h[q[top]]) --top;
ll Len = md(s[i] - s[q[top]]);
ans = md(ans + md(calc(w[i]) * calc(h[i])));
ans = md( ans + md(md(md(Len - w[i]) * w[i]) * calc(h[i])) );
ans = md(ans + md(Sum[top] * w[i]));
q[++top] = i;
Sum[top] = md(Sum[top - 1] + md(Len * calc(h[i])));
}
cout << ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen(Neco".inp", "r")) {
freopen(Neco".inp", "r", stdin);
freopen(Neco".out", "w", stdout);
}
int nTest = 1;
// cin >> nTest;
while(nTest--)
{
solve();
}
return 0;
}
Compilation message
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:74:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | freopen(Neco".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:75:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
75 | freopen(Neco".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
7 ms |
2396 KB |
Output is correct |
4 |
Correct |
14 ms |
4696 KB |
Output is correct |
5 |
Correct |
13 ms |
4252 KB |
Output is correct |
6 |
Correct |
15 ms |
3932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
856 KB |
Output is correct |
3 |
Correct |
10 ms |
3160 KB |
Output is correct |
4 |
Correct |
19 ms |
5856 KB |
Output is correct |
5 |
Correct |
18 ms |
5980 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
980 KB |
Output is correct |
4 |
Correct |
10 ms |
3184 KB |
Output is correct |
5 |
Correct |
19 ms |
5752 KB |
Output is correct |
6 |
Correct |
19 ms |
5980 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
9 ms |
3240 KB |
Output is correct |
10 |
Correct |
18 ms |
5744 KB |
Output is correct |
11 |
Correct |
19 ms |
5716 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
480 KB |
Output is correct |
15 |
Correct |
0 ms |
484 KB |
Output is correct |
16 |
Correct |
0 ms |
472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
600 KB |
Output is correct |
11 |
Correct |
8 ms |
2396 KB |
Output is correct |
12 |
Correct |
15 ms |
4956 KB |
Output is correct |
13 |
Correct |
16 ms |
4436 KB |
Output is correct |
14 |
Correct |
15 ms |
3928 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
9 ms |
3172 KB |
Output is correct |
18 |
Correct |
20 ms |
5736 KB |
Output is correct |
19 |
Correct |
19 ms |
5980 KB |
Output is correct |
20 |
Correct |
0 ms |
380 KB |
Output is correct |
21 |
Correct |
2 ms |
852 KB |
Output is correct |
22 |
Correct |
12 ms |
3120 KB |
Output is correct |
23 |
Correct |
18 ms |
5720 KB |
Output is correct |
24 |
Correct |
19 ms |
5708 KB |
Output is correct |
25 |
Correct |
0 ms |
600 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
352 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
2 ms |
860 KB |
Output is correct |
31 |
Correct |
2 ms |
860 KB |
Output is correct |
32 |
Correct |
10 ms |
2328 KB |
Output is correct |
33 |
Correct |
9 ms |
2396 KB |
Output is correct |
34 |
Correct |
17 ms |
4448 KB |
Output is correct |
35 |
Correct |
17 ms |
4448 KB |
Output is correct |
36 |
Correct |
18 ms |
4732 KB |
Output is correct |
37 |
Correct |
20 ms |
4708 KB |
Output is correct |
38 |
Correct |
0 ms |
360 KB |
Output is correct |
39 |
Correct |
18 ms |
4696 KB |
Output is correct |
40 |
Correct |
18 ms |
4704 KB |
Output is correct |
41 |
Correct |
18 ms |
4712 KB |
Output is correct |
42 |
Correct |
18 ms |
5212 KB |
Output is correct |