Submission #1157069

#TimeUsernameProblemLanguageResultExecution timeMemory
1157069dostsFancy Fence (CEOI20_fancyfence)C++20
100 / 100
19 ms5192 KiB
#include <bits/stdc++.h> #pragma GCC optimize("O3,unroll-loops") #pragma GCC target("avx2") using namespace std; #define int long long #define pii pair<int,int> #define ff first #define ss second #define sp << " " << #define all(cont) cont.begin(),cont.end() #define vi vector<int> const int inf = 2e18,N = 5e5+1,MOD = 1e9+7; int add(int x,int y) { x%=MOD; y%=MOD; return ((x+y >= MOD) ? (x+y-MOD) : x+y); } int mult(int x,int y) { return ((x%MOD)*(y%MOD))%MOD; } int sub(int x,int y) { y%=MOD; x%=MOD; if (x < y) return (MOD+x)-y; return x-y; } void solve() { int n; cin >> n; vi h(n+1),w(n+1); for (int i=1;i<=n;i++) cin >> h[i]; for (int i=1;i<=n;i++) cin >> w[i]; vi L(n+1),R(n+1); stack<int> stk; for (int i=1;i<=n;i++) { while (!stk.empty() && h[stk.top()] >= h[i]) stk.pop(); if (stk.empty()) L[i] = 1; else L[i] = stk.top()+1; stk.push(i); } while (!stk.empty()) stk.pop(); for (int i = n;i>=1;i--) { while (!stk.empty() && h[stk.top()] > h[i]) stk.pop(); if (stk.empty()) R[i] = n; else R[i] = stk.top()-1; stk.push(i); } vi pref(n+1,0); for (int i=1;i<=n;i++) pref[i] = pref[i-1]+w[i]; int ans = 0,anss = 0; for (int i=1;i<=n;i++) { ans = 0; int tot = add(pref[R[i]],MOD-pref[L[i]-1]); ans = add(ans,(tot*(tot+1)/2)); int left = sub(pref[i-1],pref[L[i]-1]); ans = sub(ans,(left*(left+1)/2)); int right = sub(pref[R[i]],pref[i]); ans = sub(ans,(right*(right+1)/2)); ans = mult(ans,h[i]*(h[i]+1)/2); anss = add(anss,ans); } cout << anss << '\n'; } int32_t main() { ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); #ifdef Dodi freopen("in.txt","r",stdin); freopen("out.txt","w",stdout); #endif int t = 1; //cin >> t; while (t --> 0) solve(); }
#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...