제출 #1289541

#제출 시각아이디문제언어결과실행 시간메모리
1289541liangjeremyFancy Fence (CEOI20_fancyfence)C++20
100 / 100
85 ms3548 KiB
#include<bits/stdc++.h> #include<bits/extc++.h> #define fi first #define se second #define int long long using namespace std; //using namespace __gnu_pbds; using db=double; using ll=int64_t; using sll=__int128; using lb=long double; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); template<int mod, int RT>struct mint{ int v; static constexpr mint rt(){ return RT; } explicit operator int() const { return v; } mint():v(0){} mint(int _v):v((int)(_v%mod)){ v+=(v<0)*mod; } mint& operator+=(mint o){ if((v+=o.v)>=mod)v-=mod; return *this; } mint& operator-=(mint o){ if((v-=o.v)<0)v+=mod; return *this; } mint& operator*=(mint o){ v=(int)((int)(v*o.v%mod)); return *this; } mint& operator/=(mint o){ *this*=inv(o); return *this; } friend mint mpow(mint a, int x){ if(x==0)return 1; mint t=mpow(a,x>>1); if(x%2==0)return t*t; return t*t*a; } friend mint inv(mint a){ return mpow(a,mod-2); } friend mint operator+(mint a, mint b){ return a+=b; } friend mint operator-(mint a, mint b){ return a-=b; } friend mint operator*(mint a, mint b){ return a*=b; } friend mint operator/(mint a, mint b){ return a/=b; } mint operator-() const { return mint(-v); } mint& operator++() { return *this+=1; } mint& operator--() { return *this-=1; } bool operator==(const mint& o) const { return v==o.v; } bool operator!=(const mint& o) const { return v!=o.v; } }; using mi=mint<(int)1e9+7,5>; int32_t main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); srand(time(0)); int n; cin>>n; vector<int>h(n+1); vector<int>w(n+1); for(int i=1; i<=n; i++){ cin>>h[i]; } for(int i=1; i<=n; i++){ cin>>w[i]; } mi ans=0; vector<int>prefix(n+2); for(int i=1; i<=n; i++){ mi hh=h[i]; mi ww=w[i]; ans+=hh*(hh+1)*ww*(ww+1)/4; prefix[i]=prefix[i-1]+w[i]; } stack<pair<int,mi>>stk; mi tot=0; for(int i=n; i>=1; i--){ while(stk.size() && h[i]<=h[stk.top().fi]){ tot-=stk.top().se; stk.pop(); } mi cur=w[i]; ans+=cur*tot; int idx=n; mi hh=h[i]; if(stk.size())idx=stk.top().fi-1; mi len=prefix[idx]-prefix[i]; ans+=cur*len*hh*(hh+1)/2; stk.push({i,(len+w[i])*hh*(hh+1)/2}); tot+=(len+w[i])*hh*(hh+1)/2; } cout<<(int)ans<<'\n'; } /* Overhead the albatross hangs motionless upon the air And deep beneath the rolling waves in labyrinths of coral caves The echo of a distant time comes willowing across the sand And everything is green and submarine And no one showed us to the land And no one knows the wheres or whys But something stirs and something tries And starts to climb towards the light */
#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...