Submission #573135

#TimeUsernameProblemLanguageResultExecution timeMemory
573135piOOEFancy Fence (CEOI20_fancyfence)C++17
100 / 100
31 ms5456 KiB
#include <bits/stdc++.h> using namespace std; #define sz(x) ((int)size(x)) #define all(x) begin(x), end(x) #define trace(x) cout << #x << ": " << (x) << endl; typedef long long ll; mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); int rand(int l, int r) { return (int) ((ll) rnd() % (r - l + 1)) + l; } const int N = 100001; ll mod = 1e9 + 7; int h[N], R[N], L[N]; ll w[N]; ll pref[N]; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; for (int i = 1; i <= n; ++i) { cin >> h[i]; } for (int i = 1; i <= n; ++i) { cin >> w[i]; } int id = 1; for (int i = 1; i <= n;) { int j = i; ll sum = 0; while (j <= n && h[j] == h[i]) { sum += w[j]; j += 1; } h[id] = h[i]; w[id] = sum; ++id; i = j; } n = id - 1; vector<int> st; for (int i = 1; i <= n; ++i) { while (!st.empty() && h[st.back()] > h[i]) { R[st.back()] = i; st.pop_back(); } if (st.empty()) { L[i] = 0; } else { L[i] = st.back(); } st.push_back(i); } for (int x : st) { R[x] = n + 1; } for (int i = 1; i <= n; ++i) { w[i] %= mod; pref[i] = pref[i - 1] + w[i]; } ll ans = 0; const ll inv2 = (mod + 1) / 2; for (int i = 1; i <= n; ++i) { ll sumR = (pref[R[i] - 1] - pref[i]) % mod; ll sumL = (pref[i - 1] - pref[L[i]]) % mod; ll x = (h[i] * (ll)(h[i] + 1)) % mod * inv2 % mod; ll now = (sumR * (sumL + w[i])) % mod; now = (now + (sumL + w[i]) * (sumL + w[i] + 1) % mod * inv2 % mod - sumL * (sumL + 1) % mod * inv2 % mod) % mod; now = (now + mod) % mod; ans = (ans + now * x) % mod; } 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...