#include <bits/stdc++.h>
#define int long long
using namespace std;
namespace std {
template <typename T> ostream &operator<<(ostream &out, const vector<T> &vec) {
out << "[";
for (int i = 0; i < (int)vec.size(); ++i) {
out << vec[i];
if (i + 1 < (int)vec.size())
out << ", ";
}
return out << "]";
}
} // namespace std
void dbg_out() { cout << endl; }
template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) {
cout << ' ' << H;
dbg_out(T...);
}
#ifdef DEBUG
#define dbg(...) cout << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif
template <const int32_t MOD> struct ModInt {
int32_t x;
ModInt() : x(0) {}
ModInt(long long u) : x(u % MOD) {
if (x < 0)
x += MOD;
}
friend bool operator==(const ModInt &a, const ModInt &b) {
return a.x == b.x;
}
friend bool operator!=(const ModInt &a, const ModInt &b) {
return a.x != b.x;
}
friend bool operator<(const ModInt &a, const ModInt &b) { return a.x < b.x; }
friend bool operator>(const ModInt &a, const ModInt &b) { return a.x > b.x; }
friend bool operator<=(const ModInt &a, const ModInt &b) {
return a.x <= b.x;
}
friend bool operator>=(const ModInt &a, const ModInt &b) {
return a.x >= b.x;
}
static ModInt sign(long long k) {
return ((k & 1) ? ModInt(MOD - 1) : ModInt(1));
}
ModInt &operator+=(const ModInt &m) {
x += m.x;
if (x >= MOD)
x -= MOD;
return *this;
}
ModInt &operator-=(const ModInt &m) {
x -= m.x;
if (x < 0LL)
x += MOD;
return *this;
}
ModInt &operator*=(const ModInt &m) {
x = (1LL * x * m.x) % MOD;
return *this;
}
friend ModInt operator-(const ModInt &a) {
ModInt res(a);
if (res.x)
res.x = MOD - res.x;
return res;
}
friend ModInt operator+(const ModInt &a, const ModInt &b) {
return ModInt(a) += ModInt(b);
}
friend ModInt operator-(const ModInt &a, const ModInt &b) {
return ModInt(a) -= ModInt(b);
}
friend ModInt operator*(const ModInt &a, const ModInt &b) {
return ModInt(a) *= ModInt(b);
}
static long long fp(long long u, long long k) {
long long res = 1LL;
while (k > 0LL) {
if (k & 1LL)
res = (res * u) % MOD;
u = (u * u) % MOD;
k /= 2LL;
}
return res;
}
static constexpr int mod() { return MOD; }
ModInt fastpow(long long k) { return ModInt(fp(x, k)); }
ModInt inv() {
assert(x);
return ModInt(fp(x, MOD - 2));
}
ModInt &operator/=(const ModInt &m) { return *this *= ModInt(m).inv(); }
friend ModInt operator/(const ModInt &a, const ModInt &b) {
return ModInt(a) *= ModInt(b).inv();
}
friend ostream &operator<<(ostream &out, const ModInt &a) {
return out << a.x;
}
friend istream &operator>>(istream &in, ModInt &a) { return in >> a.x; }
};
const int MOD = 1e9 + 7;
using Mint = ModInt<MOD>;
signed main(void) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
vector<int> h(N), w(N);
vector<int> prv(N), nxt(N);
for (int &x : h)
cin >> x;
for (int &x : w)
cin >> x;
vector<int> prefW(N + 1);
for (int i = 0; i < N; ++i)
prefW[i + 1] = prefW[i] + w[i];
for (int i = 0; i < N; ++i) {
prv[i] = i - 1;
while (prv[i] >= 0 and h[prv[i]] > h[i])
prv[i] = prv[prv[i]];
}
for (int i = N - 1; i >= 0; --i) {
nxt[i] = i + 1;
while (nxt[i] < N and h[nxt[i]] >= h[i])
nxt[i] = nxt[nxt[i]];
}
Mint sol = 0;
for (int i = 0; i < N; ++i) {
int L = prv[i] + 1;
int R = nxt[i];
// [L, R)
Mint cntL = prefW[i] - prefW[L];
Mint cntR = prefW[R] - prefW[i + 1];
sol += Mint(h[i]) * (h[i] + Mint(1)) / 2 *
((cntL + 1) * (cntR + 1) + (w[i] - 1) * (cntL + cntR + 2));
}
cout << sol << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
312 KB |
Output is correct |
2 |
Incorrect |
1 ms |
328 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |