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;
#define ll long long
const int md = 1e9 + 7;
struct mint{
int v;
mint() : v(0) {}
mint(ll v_) : v(int(v_ % md)){ if (v < 0) v += md; }
explicit operator int() const{ return v; }
friend std::ostream& operator << (std::ostream& out, const mint& n){ return out << int(n); }
friend std::istream& operator >> (std::istream& in, mint& n){ ll v_; in >> v_; n = mint(v_); return in; }
friend bool operator == (const mint& a, const mint& b){ return a.v == b.v; }
friend bool operator != (const mint& a, const mint& b){ return a.v != b.v; }
friend bool operator < (const mint& a, const mint& b){ return a.v < b.v; }
friend bool operator <= (const mint& a, const mint& b){ return a.v <= b.v; }
friend bool operator > (const mint& a, const mint& b){ return a.v > b.v; }
friend bool operator >= (const mint& a, const mint& b){ return a.v >= b.v; }
mint& operator += (const mint& o){ ((v += o.v) >= md) ? v -= md : 0; return *this; }
mint& operator -= (const mint& o){ ((v -= o.v) < 0) ? v += md : 0; return *this; }
mint& operator *= (const mint& o){ v = int((ll)v * o.v % md); return *this; }
mint& operator /= (const mint& o){ return (*this) *= inv(o); }
mint operator - () const{ return mint(-v); }
mint& operator ++ (){ return *this += 1; }
mint& operator -- (){ return *this -= 1; }
friend mint operator + (mint a, const mint& b){ return a += b; }
friend mint operator - (mint a, const mint& b){ return a -= b; }
friend mint operator * (mint a, const mint& b){ return a *= b; }
friend mint operator / (mint a, const mint& b){ return a /= b; }
friend mint pow(mint a, ll p){ return p == 0 ? 1 : pow(a * a, p / 2) * (p & 1 ? a : 1); }
friend mint inv(const mint& a){ return pow(a, md - 2); }
};
mint sum(mint x){ return x * (x + 1) / 2; }
int main(){
int n; cin >> n;
mint h[n + 1], w[n + 1], p[n + 1], dp[n + 1], ans;
for (int i = 1; i <= n; i++) cin >> h[i];
for (int i = 1; i <= n; i++) cin >> w[i];
for (int i = 1; i <= n; i++) p[i] = p[i - 1] + w[i];
stack<int> stk; stk.push(0);
for (int i = 1; i <= n; i++){
while (!stk.empty() and h[stk.top()] >= h[i]) stk.pop();
dp[i] = dp[stk.top()] + sum(h[i]) * (p[i] - p[stk.top()]);
ans += dp[stk.top()] * w[i] + sum(h[i]) * (p[i - 1] - p[stk.top()]) * w[i] + sum(h[i]) * sum(w[i]);
stk.push(i);
}
cout<<ans<<endl;
}
# | 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... |