Submission #1175817

#TimeUsernameProblemLanguageResultExecution timeMemory
1175817InvMODFancy Fence (CEOI20_fancyfence)C++20
100 / 100
14 ms2376 KiB
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define gcd __gcd #define sz(v) (int) v.size() #define pb push_back #define pi pair<int,int> #define all(v) (v).begin(), (v).end() #define compact(v) (v).erase(unique(all(v)), (v).end()) #define FOR(i, a, b) for(int i = (a); i <= (b); i++) #define REV(i, a, b) for(int i = (a); i >= (b); i--) #define dbg(x) "[" #x " = " << (x) << "]" ///#define int long long using ll = long long; using ld = long double; using ull = unsigned long long; template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;} template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;} const int N = 2e5+5; const ll MOD = 1e9+7; const ll inv2 = 500000004; int add(int x, int y){ return (x + y < MOD ? x + y : x + y - MOD); } int sub(int x, int y){ return add(x - y, MOD); } int mul(int x, int y){ return (1ll * x * y) % MOD; } int bpow(int x, int y){ int ans = 1; while(y){ if(y & 1) ans = mul(ans, x); x = mul(x, x); y >>= 1; } return ans; } int sumN(int x){ return mul(mul(x, add(x, 1)), inv2); } void solve() { int n; cin >> n; vector<int> h(n + 1), w(n + 1); FOR(i, 1, n) cin >> h[i]; FOR(i, 1, n) cin >> w[i]; vector<int> pref(n + 1); FOR(i, 1, n) pref[i] = add(w[i], pref[i - 1]); vector<int> dp(n + 1); stack<int> st; st.push(0); int answer = 0; FOR(i, 1, n){ while(h[i] <= h[st.top()]) st.pop(); int rect_sum = mul(sub(pref[i - 1], pref[st.top()]), mul(w[i], sumN(h[i]))); rect_sum = add(rect_sum, mul(sumN(w[i]), sumN(h[i]))); answer = add(answer, rect_sum); answer = add(answer, mul(dp[st.top()], w[i])); dp[i] = mul(sub(pref[i], pref[st.top()]), sumN(h[i])); dp[i] = add(dp[i], dp[st.top()]); st.push(i); } cout << answer << "\n"; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define name "InvMOD" if(fopen(name".INP", "r")){ freopen(name".INP","r",stdin); freopen(name".OUT","w",stdout); } int t = 1; //cin >> t; while(t--) solve(); return 0; }

Compilation message (stderr)

fancyfence.cpp: In function 'int main()':
fancyfence.cpp:94:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   94 |         freopen(name".INP","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
fancyfence.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   95 |         freopen(name".OUT","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
#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...