Submission #557920

#TimeUsernameProblemLanguageResultExecution timeMemory
557920DJ035Fancy Fence (CEOI20_fancyfence)C++17
100 / 100
104 ms5352 KiB
#include <bits/stdc++.h> #define MEM 111111 #define sanic ios_base::sync_with_stdio(0) #define x first #define y second #define pf push_front #define pb push_back #define all(v) v.begin(), v.end() #define sz size() using namespace std; typedef long long ll; typedef pair<ll, ll> pi; const ll INF = 1e13+7; const ll MOD = 1e9+7; ll gcd(ll a, ll b){ if(a%b) return gcd(b, a%b); return b; } ll c2(ll n){ ll ret = n*(n-1)/2LL; ret %= MOD; ret += MOD; ret %= MOD; return ret; } ll hp(ll l, ll r){ ll a=c2(l+1); ll b=c2(r+1); return ((b-a)%MOD+MOD)%MOD; } ll n,m,t,c; ll h[MEM], w[MEM]; signed main(){ //sanic; cin.tie(0); cout.tie(0); cin >> n; for(int i=0; i<n; i++) cin >> h[i]; for(int i=0; i<n; i++) cin >> w[i]; stack<pi> s; ll ans=0; for(int i=0; i<n; i++){ if(s.empty()){ s.push({h[i], w[i]}); continue; } ll p=0; while(s.top().x>h[i]){ ll h1=s.top().x, w1=s.top().y; s.pop(); ans = (ans+(hp(max((s.empty() ? h[i]:s.top().x), h[i]), h1)*c2(w1+1))%MOD+MOD)%MOD; p = w1; //cout << h1 << ' ' << w1 << '\n'; if(s.empty() || s.top().x<h[i]) break; pi ff=s.top(); s.pop(); ff.y += w1; ff.y %= MOD; ff.y += MOD; ff.y %= MOD; s.push(ff); } //cout << ans << '\n'; if(!s.empty() && s.top().x==h[i]) { pi ff=s.top(); s.pop(); ff.y += w[i]; ff.y %= MOD; ff.y += MOD; ff.y %= MOD; s.push(ff); } else s.push({h[i], ((w[i]+p)%MOD+MOD)%MOD}); /* stack<pi> gg=s; while(!gg.empty()){ cout << gg.top().x << ' ' << gg.top().y << '\n'; gg.pop(); }*/ } while(!s.empty()){ ll h1=s.top().x, w1=s.top().y; s.pop(); ans = (ans+(hp((s.empty()?0:s.top().x), h1)*c2(w1+1))%MOD+MOD)%MOD; //cout << h1 << ' ' << w1 << '\n'; if(s.empty()) break; pi ff=s.top(); s.pop(); ff.y += w1; ff.y %= MOD; ff.y += MOD; ff.y %= MOD; s.push(ff); } cout << ans; } /* 10 1 3 2 4 2 1 3 5 6 7 2 3 5 4 6 2 3 5 6 7 3 2 3 1 3 1 2 5 2 3 3 4 1 2 4 5 1 2 660 */
#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...