Submission #898428

#TimeUsernameProblemLanguageResultExecution timeMemory
898428votranngocvyFancy Fence (CEOI20_fancyfence)C++14
100 / 100
22 ms5180 KiB
#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 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...