#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 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
0 ms |
204 KB |
Output is correct |
4 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
0 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 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |