#include <bits/stdc++.h>
using namespace std;
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define endl "\n"
#define sp " "
#define pii pair<int, int>
#define st first
#define nd second
#define N 100005
#define L(node) node * 2
#define R(node) node * 2 + 1
#define int long long
const int INF = 1e9 + 7;
const int modulo = 1e9 + 7;
int h[N], w[N], pre[N];
pii tree[N * 4];
void build(int node, int l, int r)
{
if (l == r)
{
tree[node] = {h[l], l};
return;
}
int mid = (l + r) / 2;
build(L(node), l, mid);
build(R(node), mid + 1, r);
tree[node] = min(tree[L(node)], tree[R(node)]);
}
pii find(int node, int l, int r, int sl, int sr)
{
if (l > r || l > sr || r < sl) return {INF, INF};
if (l >= sl && r <= sr) return tree[node];
int mid = (l + r) / 2;
return min(find(L(node), l, mid, sl, sr), find(R(node), mid + 1, r, sl, sr));
}
int ans, n;
int add(int a, int b)
{
if (a + b < modulo) return a + b;
return a + b - modulo;
}
int mul(int a, int b)
{
return (a * b) % modulo;
}
int subs(int a, int b)
{
if (a < b) return a - b + modulo;
return a - b;
}
int fe(int a, int b)
{
if (b == 0) return 1;
if (b % 2) return mul(a, fe(a, b - 1));
int tmp = fe(a, b / 2);
return mul(tmp, tmp);
}
void f(int l, int r, int prev)
{
pii tmp = find(1, 1, n, l, r);
//cout<<l<<sp<<r<<sp<<tmp.st<<sp<<tmp.nd<<endl;
int maks = tmp.st;
int ind = tmp.nd;
int sum = subs(pre[r], pre[l - 1]);
int x = mul(sum, sum + 1);
//cout<<x<<sp;
x = mul(x, fe(2, modulo - 2));
//cout<<x<<sp;
int hig = mul(mul(maks, maks + 1), fe(2, modulo - 2));
int no = mul(mul(prev, prev + 1), fe(2, modulo - 2));
x = mul(x, subs(hig, no));
if (maks != prev) ans = add(ans, x);
if (ind > l) f(l, ind - 1, maks);
if (ind < r) f(ind + 1, r, maks);
}
int32_t main()
{
//fileio();
fastio();
cin>>n;
for (int i = 1; i <= n; i++)
cin>>h[i];
for (int j = 1; j <= n; j++)
{
cin>>w[j];
pre[j] = add(pre[j - 1], w[j]);
}
build(1, 1, n);
f(1, n, 0);
cout<<ans<<endl;
cerr<<"time taken : "<<(float)clock() / CLOCKS_PER_SEC<<" seconds\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
36 ms |
4144 KB |
Output is correct |
4 |
Correct |
83 ms |
7404 KB |
Output is correct |
5 |
Correct |
66 ms |
7432 KB |
Output is correct |
6 |
Correct |
60 ms |
7364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
10 ms |
1236 KB |
Output is correct |
3 |
Correct |
30 ms |
4520 KB |
Output is correct |
4 |
Correct |
60 ms |
8688 KB |
Output is correct |
5 |
Correct |
66 ms |
8812 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
7 ms |
1236 KB |
Output is correct |
4 |
Correct |
32 ms |
4524 KB |
Output is correct |
5 |
Correct |
62 ms |
8708 KB |
Output is correct |
6 |
Correct |
62 ms |
8780 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
7 ms |
1292 KB |
Output is correct |
9 |
Correct |
32 ms |
4564 KB |
Output is correct |
10 |
Correct |
61 ms |
8588 KB |
Output is correct |
11 |
Correct |
61 ms |
8616 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 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 |
372 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 |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
368 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
11 |
Correct |
29 ms |
4132 KB |
Output is correct |
12 |
Correct |
61 ms |
7928 KB |
Output is correct |
13 |
Correct |
72 ms |
7816 KB |
Output is correct |
14 |
Correct |
62 ms |
7892 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
1300 KB |
Output is correct |
17 |
Correct |
34 ms |
4528 KB |
Output is correct |
18 |
Correct |
68 ms |
8644 KB |
Output is correct |
19 |
Correct |
62 ms |
8812 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
7 ms |
1236 KB |
Output is correct |
22 |
Correct |
30 ms |
4544 KB |
Output is correct |
23 |
Correct |
60 ms |
8596 KB |
Output is correct |
24 |
Correct |
60 ms |
8684 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
7 ms |
1236 KB |
Output is correct |
31 |
Correct |
10 ms |
1236 KB |
Output is correct |
32 |
Correct |
32 ms |
4436 KB |
Output is correct |
33 |
Correct |
34 ms |
4644 KB |
Output is correct |
34 |
Correct |
63 ms |
8516 KB |
Output is correct |
35 |
Correct |
66 ms |
8528 KB |
Output is correct |
36 |
Correct |
73 ms |
8688 KB |
Output is correct |
37 |
Correct |
65 ms |
8696 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
66 ms |
8708 KB |
Output is correct |
40 |
Correct |
64 ms |
8680 KB |
Output is correct |
41 |
Correct |
70 ms |
9184 KB |
Output is correct |
42 |
Correct |
61 ms |
11168 KB |
Output is correct |