#include <bits/stdc++.h>
#define int long long
using namespace std;
using pi = pair<int, int>;
const int mod = 1e9+7;
const int inv4 = 25e7+2;
int calc(int a, int b){
a %= mod; b %= mod;
return ((a*(a+1)%mod)*(b*(b+1)%mod)%mod)*inv4%mod;
}
int32_t main(){
int n; cin >> n;
vector<pi> data(n);
for (int i = 0; i < n; ++i) cin >> data[i].first;
for (int i = 0; i < n; ++i) cin >> data[i].second;
//the stack trick
stack<int> st;
vector<int> left(n), right(n), ind;
for (int i = 0; i < n; ++i){
while (!st.empty() && data[st.top()].first > data[i].first) st.pop();
if (st.empty() || data[st.top()].first != data[i].first) ind.push_back(i);
left[i] = (st.empty() ? -1 : st.top());
st.push(i);
}
st = stack<int>();
for (int i = n-1; i >= 0; --i){
while (!st.empty() && data[st.top()].first >= data[i].first) st.pop();
right[i] = (st.empty() ? n : st.top());
st.push(i);
}
//prefix sums (width)
vector<int> prefix(n+1);
prefix[0] = 0;
for (int i = 1; i <= n; ++i) prefix[i] = prefix[i-1] + data[i-1].second % mod;
//calculate the rectangles
int res = 0;
for (int i : ind){
int first = left[i] + 1, last = right[i] - 1;
int w = (prefix[last+1] - prefix[first] + mod) % mod, f = 0;
if (first != 0) f = max(f, data[first-1].first);
if (last != n-1) f = max(f, data[last+1].first);
res = (calc(data[i].first, w) - calc(f, w) + mod + res) % mod;
}
cout << res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 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 |
# |
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 |
33 ms |
2488 KB |
Output is correct |
4 |
Correct |
63 ms |
4932 KB |
Output is correct |
5 |
Correct |
67 ms |
4664 KB |
Output is correct |
6 |
Correct |
64 ms |
4412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
9 ms |
716 KB |
Output is correct |
3 |
Correct |
44 ms |
2232 KB |
Output is correct |
4 |
Correct |
94 ms |
4152 KB |
Output is correct |
5 |
Correct |
91 ms |
4152 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 |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
9 ms |
712 KB |
Output is correct |
4 |
Correct |
55 ms |
2216 KB |
Output is correct |
5 |
Correct |
115 ms |
4152 KB |
Output is correct |
6 |
Correct |
93 ms |
4148 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
12 ms |
972 KB |
Output is correct |
9 |
Correct |
48 ms |
3360 KB |
Output is correct |
10 |
Correct |
100 ms |
6828 KB |
Output is correct |
11 |
Correct |
105 ms |
6936 KB |
Output is correct |
12 |
Correct |
1 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 |
0 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 |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 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 |
33 ms |
2484 KB |
Output is correct |
12 |
Correct |
84 ms |
4932 KB |
Output is correct |
13 |
Correct |
84 ms |
4660 KB |
Output is correct |
14 |
Correct |
80 ms |
4420 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
9 ms |
720 KB |
Output is correct |
17 |
Correct |
44 ms |
2232 KB |
Output is correct |
18 |
Correct |
90 ms |
4160 KB |
Output is correct |
19 |
Correct |
92 ms |
4148 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
10 ms |
972 KB |
Output is correct |
22 |
Correct |
47 ms |
3344 KB |
Output is correct |
23 |
Correct |
90 ms |
6816 KB |
Output is correct |
24 |
Correct |
94 ms |
6940 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
10 ms |
916 KB |
Output is correct |
31 |
Correct |
10 ms |
952 KB |
Output is correct |
32 |
Correct |
50 ms |
3608 KB |
Output is correct |
33 |
Correct |
49 ms |
3644 KB |
Output is correct |
34 |
Correct |
89 ms |
6824 KB |
Output is correct |
35 |
Correct |
97 ms |
6816 KB |
Output is correct |
36 |
Correct |
95 ms |
7064 KB |
Output is correct |
37 |
Correct |
98 ms |
6968 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
95 ms |
6948 KB |
Output is correct |
40 |
Correct |
95 ms |
6948 KB |
Output is correct |
41 |
Correct |
96 ms |
6968 KB |
Output is correct |
42 |
Correct |
93 ms |
7316 KB |
Output is correct |