Skip to content

Instantly share code, notes, and snippets.

@AntonieValentin
Created April 11, 2022 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AntonieValentin/29f144a0490ae1b5036a79fdde8dba9c to your computer and use it in GitHub Desktop.
Save AntonieValentin/29f144a0490ae1b5036a79fdde8dba9c to your computer and use it in GitHub Desktop.
CEOI 2020, Day 1, Fancy Fence
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
stack <pair<int,int>> S,S2;
int n,v[100005],i,dr[100005],st[100005],x,Max,st2[100005];
long long suma[100001],total1,val1, val2,total2,nr;
pair <int,int > sor[100005];
long long bin(long long x){
if(x > mod){
x %= mod;
}
return ((x*(x+1))/2)%mod;
}
long long rectCount(long long x, long long y){
return (bin(x)*bin(y))%mod;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
for(i=1;i<=n;i++){
cin>>v[i];
sor[i].first = v[i];
sor[i].second = i;
dr[i] = n + 1;
}
for(i=1;i<=n;i++){
cin>>x;
suma[i] = suma[i-1] + x;
}
for(i=1;i<=n;i++){
x = v[i];
while(S.empty() == false && x<S.top().first){
dr[S.top().second] = i;
S.pop();
}
S.push({x,i});
}
while(S.empty() == false){
S.pop();
}
for(i=n;i>=1;i--){
x = v[i];
while(S.empty() == false && x <S.top().first){
st[S.top().second] = i;
S.pop();
}
while(S2.empty() == false && x<=S2.top().first){
st2[S2.top().second] = i;
S2.pop();
}
S2.push({x,i});
S.push({x,i});
}
for(i=1;i<=n;i++){
nr = suma[dr[sor[i].second]-1] - suma[st[sor[i].second]];
total1 = (total1 + rectCount(nr,sor[i].first));
if(total1 > mod){
total1 -= mod;
}
Max = max(v[st2[sor[i].second]],v[dr[sor[i].second]]);
total2 = (total2 + rectCount(nr,Max)) % mod;
}
if(total1 < total2)
total1 += mod;
cout << total1 - total2;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment