#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<ll,ll>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=2e5;
const ll inf=1e18;
const ll mod=1e9+7;
ll get(ll l,ll r)
{
if(r<l) return 0;
ll sum=(r+l);
ll cnt=(r-l+1);
if(sum%2==0) sum/=2;
else cnt/=2;
sum%=mod;
cnt%=mod;
return sum*cnt%mod;
}
ll n,h[maxN];
deque<int> dq;
vector<pli>vec;
ll l[maxN],r[maxN],w[maxN],low[maxN],high[maxN];
void solve()
{
cin >> n;
for(int i=1;i<=n;i++) cin >> h[i];
for(int i=1;i<=n;i++) cin >> w[i];
ll last=0;
for(int i=1;i<=n;i++)
{
low[i]=last+1;
high[i]=last+w[i];
last+=w[i];
}
low[n+1]=last+1;
high[0]=0;
for(int i=1;i<=n;i++)
{
while(!dq.empty()&&h[i]<=h[dq.back()]) dq.pop_back();
if(dq.size()) l[i]=dq.back();
else l[i]=0;
vec.emplace_back(h[i],i);
dq.pb(i);
}
dq.clear();
ll ans=0;
ll sum=0;
for(int i=n;i>=1;i--)
{
while(!dq.empty()&&h[i]<h[dq.back()]) dq.pop_back();
if(dq.size()) r[i]=dq.back();
else r[i]=n+1;
ans+=h[i]*((high[i]-high[l[i]])%mod)%mod*((low[r[i]]-high[i])%mod)%mod;
ans+=h[i]*get(low[i],high[i]-1)%mod-h[i]*(high[l[i]]%mod)%mod*(w[i]-1)%mod;
ans%=mod;
if(ans<0) ans+=mod;
sum+=((high[i]-high[l[i]])%mod)*((low[r[i]]-high[i])%mod)%mod;
sum+=get(low[i],high[i]-1)-(high[l[i]]%mod)*(w[i]-1)%mod;
sum%=mod;
if(sum<0) sum+=mod;
dq.pb(i);
}
ll vc=ans;
sort(vec.begin(),vec.end());
ll cur=0;
for(int i=0;i<vec.size();i++)
{
ans+=(vec[i].first-cur)*vc%mod-get(1,vec[i].first-cur)*sum%mod;
vc-=(vec[i].fi-cur)*sum%mod;
ans=ans%mod;
if(ans<0) ans+=mod;
vc%=mod;
if(vc<0) vc+=mod;
ll x=vec[i].se;
sum-=((high[x]-high[l[x]])%mod)*((low[r[x]]-high[x])%mod)%mod;
sum-=get(low[x],high[x]-1)-high[l[x]]%mod*((high[x]-low[x])%mod)%mod;
sum%=mod;
if(sum<0) sum+=mod;
cur=vec[i].fi;
}
cout << ans;
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message
fancyfence.cpp: In function 'void solve()':
fancyfence.cpp:71:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
71 | for(int i=0;i<vec.size();i++)
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
17 ms |
3536 KB |
Output is correct |
4 |
Correct |
29 ms |
6880 KB |
Output is correct |
5 |
Correct |
30 ms |
6680 KB |
Output is correct |
6 |
Correct |
29 ms |
6664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
1112 KB |
Output is correct |
3 |
Correct |
17 ms |
3644 KB |
Output is correct |
4 |
Correct |
30 ms |
7032 KB |
Output is correct |
5 |
Correct |
35 ms |
7016 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
1112 KB |
Output is correct |
4 |
Correct |
15 ms |
3664 KB |
Output is correct |
5 |
Correct |
32 ms |
6980 KB |
Output is correct |
6 |
Correct |
33 ms |
6912 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
1272 KB |
Output is correct |
9 |
Correct |
18 ms |
4636 KB |
Output is correct |
10 |
Correct |
31 ms |
8792 KB |
Output is correct |
11 |
Correct |
30 ms |
8932 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
16 ms |
4160 KB |
Output is correct |
12 |
Correct |
29 ms |
8104 KB |
Output is correct |
13 |
Correct |
30 ms |
7884 KB |
Output is correct |
14 |
Correct |
29 ms |
7876 KB |
Output is correct |
15 |
Correct |
1 ms |
356 KB |
Output is correct |
16 |
Correct |
4 ms |
1272 KB |
Output is correct |
17 |
Correct |
17 ms |
4568 KB |
Output is correct |
18 |
Correct |
32 ms |
8900 KB |
Output is correct |
19 |
Correct |
40 ms |
9032 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
4 ms |
1304 KB |
Output is correct |
22 |
Correct |
17 ms |
4572 KB |
Output is correct |
23 |
Correct |
30 ms |
8776 KB |
Output is correct |
24 |
Correct |
31 ms |
8932 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
468 KB |
Output is correct |
28 |
Correct |
1 ms |
468 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
4 ms |
1240 KB |
Output is correct |
31 |
Correct |
4 ms |
1232 KB |
Output is correct |
32 |
Correct |
20 ms |
4432 KB |
Output is correct |
33 |
Correct |
20 ms |
4600 KB |
Output is correct |
34 |
Correct |
42 ms |
8504 KB |
Output is correct |
35 |
Correct |
41 ms |
8388 KB |
Output is correct |
36 |
Correct |
42 ms |
8644 KB |
Output is correct |
37 |
Correct |
40 ms |
8648 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
39 ms |
8656 KB |
Output is correct |
40 |
Correct |
35 ms |
8664 KB |
Output is correct |
41 |
Correct |
32 ms |
8708 KB |
Output is correct |
42 |
Correct |
34 ms |
8748 KB |
Output is correct |