제출 #1231376

#제출 시각아이디문제언어결과실행 시간메모리
1231376tminhFancy Fence (CEOI20_fancyfence)C++20
0 / 100
1 ms328 KiB
#include "bits/stdc++.h" using namespace std; #define ll long long #define endl '\n' const ll mod = 1e9 + 7; const int N = 1e5 + 5; const ll inv2 = 500000004; int n; ll h[N], w[N]; ll pre[N]; stack<int> s; int l[N], r[N]; ll S(ll n) { n %= mod; ll res = n; res = (res * (n + 1)) % mod; res = (res * inv2) % mod; return res; } void solve() { pre[0] = 0; for (int i = 1; i <= n; ++i) { pre[i] = (pre[i - 1] + w[i]); } for (int i = 1; i <= n; ++i) { while (!s.empty() && h[s.top()] >= h[i]) s.pop(); l[i] = (s.empty() ? 0 : s.top()); s.push(i); } while (!s.empty()) s.pop(); for (int i = n; i >= 1; --i) { while (!s.empty() && h[s.top()] >= h[i]) s.pop(); r[i] = (s.empty() ? n + 1 : s.top()); s.push(i); } ll ans = 0; for (int i = 1; i <= n; ++i) { ll W = (pre[r[i] - 1] - pre[l[i]]); ll prev_h = 0; if (l[i] > 0) prev_h = max(prev_h, h[l[i]]); if (r[i] <= n) prev_h = max(prev_h, h[r[i]]); ll count_W = S(W); ll count_H_total = S(h[i]); ll count_H_prev = S(prev_h); ll count_H_new = (count_H_total - count_H_prev + mod) % mod; ll contribution = (count_W * count_H_new) % mod; ans = (ans + contribution) % mod; } cout << ans << endl; } void input() { cin >> n; for (int i = 1; i <= n; ++i) cin >> h[i]; for (int i = 1; i <= n; ++i) cin >> w[i]; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); input(); solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...