# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1209777 | cpdreamer | Fancy Fence (CEOI20_fancyfence) | C++17 | 4 ms | 9800 KiB |
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e17;
typedef long long ll;
const ll MOD=1e9+7;
#define P pair
#define S second
#define F first
#define pb push_back
#define V vector
void file() {
freopen("input.txt.txt", "r", stdin);
freopen("output.txt.txt", "w", stdout);
}
bool flag;
int state[(int)2e5+1];
set<int>adj[(int)2e5+1];
ll g( ll a){
if(a%2==0){
ll x=(a/2)%MOD;
ll y=(a+1)%MOD;
return (x*y)%MOD;
}
ll x=a%MOD;
ll y=((a+1)/2)%MOD;
return (x*y)%MOD;
}
ll f(ll a,ll b){
ll p=(g(a)%MOD)*(g(b)%MOD)%MOD;
return p;
}
ll md(ll x){
return ((x%MOD)+MOD)%MOD;
}
void solve(){
int n;
cin>>n;
ll h[n],w[n];
for(int i=0;i<n;i++){
cin>>h[i];
}
for(int i=0;i<n;i++){
cin>>w[i];
}
ll ans=0;
ll c=0;
for(int i=0;i<n;i++) {
ans += f(h[i], w[i]);
ans%= MOD;
}
stack<P<P<ll,ll>,ll>>st;
st.push({{0LL,0LL},0LL});
for(int i=0;i<n;i++){
ll s=0;
while(h[i]<st.top().F.F){
s+=st.top().F.S;
s%=MOD;
c-=((g(st.top().F.F)%MOD)*(st.top().F.S%MOD))%MOD;
c=md(c);
ans+=c*st.top().F.S;
c-=((g(st.top().F.F)%MOD)*(st.top().S%MOD))%MOD;
s+=st.top().S;
s%=MOD;
c=md(c);
st.pop();
}
c+=((s+w[i])%MOD)*(g(h[i])%MOD);
c%=MOD;
st.push({{h[i],w[i]},s});
}
while(st.size()>1){
c-=((g(st.top().F.F)%MOD)*(st.top().F.S%MOD))%MOD;
c=md(c);
ans+=c*st.top().F.S;
c-=((g(st.top().F.F)%MOD)*(st.top().S%MOD))%MOD;
c=md(c);
st.pop();
}
cout<<ans<<endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// file();
solve();
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |