This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
ll nc2(ll x){
return x * (x - 1) / 2 % mod;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int nums; cin >> nums;
vector<ll> wv(nums), hv(nums);
for (int i = 0; i < nums; i++) cin >> hv[i];
for (int i = 0; i < nums; i++) cin >> wv[i];
vector<tuple<ll, ll, ll>> st;
ll ans = 0, sum = 0;
for (int i = 0; i < nums; i++){
ans = (ans + nc2(hv[i] + 1) * nc2(wv[i] + 1) % mod) % mod;
ll wk = 0;
while (!st.empty() && get<0>(st.back()) >= hv[i]){
ll h, w, val; tie(h, w, val) = st.back();
wk = (wk + w) % mod; sum = (sum - val + mod) % mod;
st.pop_back();
}
ans = (ans + wk * nc2(hv[i] + 1) % mod * wv[i] % mod) % mod;
ans = (ans + sum * wv[i] % mod) % mod;
wk = (wk + wv[i]) % mod;
ll nval = wk * nc2(hv[i] + 1) % mod;
sum = (sum + nval) % mod;
st.push_back({hv[i], wk, nval});
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |