이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f3f3f3f3f;
int h[N],w[N],pre[N],l[N],r[N];
void add(int &a,int b) {
a += b;
while (a >= mod) a -= mod;
}
void sub(int &a,int b) {
a -= b;
while (a < 0) a += mod;
}
int sum(int l,int r) {
return (pre[r] - pre[l - 1] + mod) % mod;
}
int calc(int a,int b) {
int ans = (a * (a + 1) / 2) % mod;
ans = (ans * ((b * (b + 1) / 2) % mod)) % mod;
return ans;
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; i++) cin >> h[i];
pre[0] = 0;
for (int i = 1; i <= n; i++) {
cin >> w[i];
pre[i] = (pre[i - 1] + w[i]) % mod;
}
stack<int>st;
st.push(0);
for (int i = 1; i <= n; i++) {
while (!st.empty() && h[st.top()] > h[i]) st.pop();
l[i] = st.top() + 1;
st.push(i);
}
while (!st.empty()) st.pop();
st.push(n + 1);
for (int i = n; i > 0; i--) {
while (!st.empty() && h[st.top()] >= h[i]) st.pop();
r[i] = st.top() - 1;
st.push(i);
}
int ans = 0;
for (int i = 1; i <= n; i++) {
add(ans,calc(h[i],sum(l[i],r[i])));
sub(ans,calc(h[i],sum(l[i],i - 1)));
sub(ans,calc(h[i],sum(i + 1,r[i])));
}
cout << ans << "\n";
}
# | 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... |