Submission #389983

#TimeUsernameProblemLanguageResultExecution timeMemory
389983talant117408Fancy Fence (CEOI20_fancyfence)C++17
100 / 100
90 ms4640 KiB
/* Code written by Talant I.D. */ #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; #define precision(n) fixed << setprecision(n) #define pb push_back #define ub upper_bound #define lb lower_bound #define mp make_pair #define eps (double)1e-9 #define PI 2*acos(0.0) #define endl "\n" #define sz(v) int((v).size()) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() #define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); #define OK cout << "OK" << endl; const int mod = 1e9+7; ll mode(ll a) { a %= mod; if (a < 0) a += mod; return a; } ll subt(ll a, ll b) { return mode(mode(a)-mode(b)); } ll add(ll a, ll b) { return mode(mode(a)+mode(b)); } ll mult(ll a, ll b) { return mode(mode(a)*mode(b)); } ll binpow(ll a, ll b) { ll res = 1; while (b) { if (b&1) res = mult(res, a); a = mult(a, a); b >>= 1; } return res; } const int N = 1e5+7; ll h[N], w[N], dp[N]; int n; int main() { do_not_disturb cin >> n; for (int i = 1; i <= n; i++) { cin >> h[i]; } for (int i = 1; i <= n; i++) { cin >> w[i]; } stack <pii> st; ll ans = 0; ll sum = 0; for (int i = 1; i <= n; i++) { ll additional = 0; while (!st.empty() && st.top().first > h[i]) { additional = add(additional, st.top().second); sum = subt(sum, mult(mult(st.top().first, st.top().first+1), mult(st.top().second, binpow(2, mod-2)))); st.pop(); } ans = add(ans, mult(mult(mult(h[i], h[i]+1), mult(w[i], w[i]+1)), binpow(4, mod-2))); ans = add(ans, mult(sum, w[i])); ans = add(ans, mult(mult(additional, w[i]), mult(h[i], mult(h[i]+1, binpow(2, mod-2))))); sum = add(sum, mult(mult(h[i], mult(h[i]+1, binpow(2, mod-2))), add(additional, w[i]))); st.push(mp(h[i], add(additional, w[i]))); } cout << ans; 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...