#include<bits/stdc++.h>
#define MOD 1000000007
#define SIZE 100010
using namespace std;
typedef long long ll;
struct st1
{
ll l,r;
};
struct st2
{
ll h,w,idx;
};
ll N,psum[SIZE],ans,seg2[SIZE<<2],lz[SIZE<<2];
st1 sparse[SIZE][17];
st2 A[SIZE];
vector<st2>v;
void push(ll l,ll r,ll node)
{
if(lz[node]==0)return;
seg2[node]=lz[node];
if(l^r)lz[node<<1]=lz[node<<1|1]=lz[node];
lz[node]=0;
}
void upd(ll l,ll r,ll node,ll s,ll e,ll x)
{
push(l,r,node);
if(l>e||r<s)return;
if(s<=l&&r<=e){
lz[node]=x;
push(l,r,node);
return;
}
ll mid=l+r>>1;
upd(l,mid,node<<1,s,e,x);
upd(mid+1,r,node<<1|1,s,e,x);
}
ll que(ll l,ll r,ll node,ll s)
{
push(l,r,node);
if(l>s||r<s)return 0;
if(l==s&&r==s)return seg2[node];
ll mid=l+r>>1;
return max(que(l,mid,node<<1,s),que(mid+1,r,node<<1|1,s));
}
bool comp(st2 x,st2 y)
{
return x.h<y.h;
}
ll S(ll x)
{
x%=MOD;
return (x*(x+1)/2)%MOD;
}
int main()
{
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>N;
for(int i=1;i<=N;i++)cin>>A[i].h;
for(int i=1;i<=N;i++)cin>>A[i].w;
bool f=false;
st2 temp={0,0,0};
v.push_back(temp);
for(int i=1;i<=N;i++){
if(f){
if(temp.h==A[i].h)temp.w+=A[i].w;
else{
v.push_back(temp);
f=false;
}
}
if(!f){
temp.h=A[i].h;
temp.w=A[i].w;
f=true;
}
}
v.push_back(temp);
N=v.size()-1;
temp={0,MOD,N+1};
v.push_back(temp);
for(ll i=1;i<=N;i++){
v[i].idx=i;
//printf("[%lld %lld %lld]\n",v[i].h,v[i].w,v[i].idx);
psum[i]=psum[i-1]+v[i].w;
}
for(ll i=1;i<=N;i++){
sparse[i][0].r=v[i+1].h;
sparse[i][0].l=v[i-1].h;
//printf("[%lld %lld] ",sparse[i][0].l,sparse[i][0].r);
}
//printf("\n");
for(ll j=1;j<17;j++){
for(ll i=1;i<=N;i++){
sparse[i][j].r=min(sparse[i][j-1].r,sparse[min(N,i+(1<<(j-1)))][j-1].r);
sparse[i][j].l=min(sparse[i][j-1].l,sparse[max(1ll,i-(1<<(j-1)))][j-1].l);
//printf("[%lld %lld] ",sparse[i][j].l,sparse[i][j].r);
}
//printf("\n");
}
sort(v.begin(),v.end()-1,comp);
for(ll j=1;j<=N;j++){
ll i=v[j].idx;
ll l=1,r=i,mid,a,b,mx,x=i;
for(ll k=16;k>=0;k--)if(sparse[x][k].r>=v[j].h)x=min(N,x+(1<<k));
b=x;
x=i;
for(ll k=16;k>=0;k--)if(sparse[x][k].l>=v[j].h)x=max(1ll,x-(1<<k));
a=x;
mx=que(1,N,1,i);
upd(1,N,1,a,b,v[j].h);
//printf("%lld %lld %lld %lld %lld\n",a,b,i,v[j].h,mx);
ans=(ans+S(psum[b]-psum[a-1])*(S(v[j].h)-S(mx)+MOD))%MOD;
//printf("%lld\n",ans);
}
cout<<ans;
}
Compilation message
fancyfence.cpp: In function 'void upd(ll, ll, ll, ll, ll, ll)':
fancyfence.cpp:34:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
34 | ll mid=l+r>>1;
| ~^~
fancyfence.cpp: In function 'll que(ll, ll, ll, ll)':
fancyfence.cpp:43:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
43 | ll mid=l+r>>1;
| ~^~
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:104:12: warning: unused variable 'l' [-Wunused-variable]
104 | ll l=1,r=i,mid,a,b,mx,x=i;
| ^
fancyfence.cpp:104:16: warning: unused variable 'r' [-Wunused-variable]
104 | ll l=1,r=i,mid,a,b,mx,x=i;
| ^
fancyfence.cpp:104:20: warning: unused variable 'mid' [-Wunused-variable]
104 | ll l=1,r=i,mid,a,b,mx,x=i;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
23 ms |
10376 KB |
Output is correct |
4 |
Correct |
35 ms |
14364 KB |
Output is correct |
5 |
Correct |
46 ms |
20252 KB |
Output is correct |
6 |
Correct |
26 ms |
10136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
3 |
Correct |
10 ms |
1484 KB |
Output is correct |
4 |
Correct |
19 ms |
2604 KB |
Output is correct |
5 |
Correct |
25 ms |
2644 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
460 KB |
Output is correct |
4 |
Correct |
10 ms |
1484 KB |
Output is correct |
5 |
Correct |
18 ms |
2580 KB |
Output is correct |
6 |
Correct |
30 ms |
2652 KB |
Output is correct |
7 |
Correct |
1 ms |
716 KB |
Output is correct |
8 |
Correct |
9 ms |
4168 KB |
Output is correct |
9 |
Correct |
24 ms |
8656 KB |
Output is correct |
10 |
Correct |
131 ms |
36640 KB |
Output is correct |
11 |
Correct |
104 ms |
36600 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
716 KB |
Output is correct |
3 |
Correct |
1 ms |
296 KB |
Output is correct |
4 |
Correct |
0 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
456 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
744 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
716 KB |
Output is correct |
14 |
Correct |
2 ms |
716 KB |
Output is correct |
15 |
Correct |
1 ms |
716 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
716 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
716 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
24 ms |
10288 KB |
Output is correct |
12 |
Correct |
36 ms |
14376 KB |
Output is correct |
13 |
Correct |
48 ms |
20268 KB |
Output is correct |
14 |
Correct |
26 ms |
10148 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
3 ms |
716 KB |
Output is correct |
17 |
Correct |
11 ms |
2360 KB |
Output is correct |
18 |
Correct |
21 ms |
4176 KB |
Output is correct |
19 |
Correct |
23 ms |
4216 KB |
Output is correct |
20 |
Correct |
1 ms |
716 KB |
Output is correct |
21 |
Correct |
9 ms |
4296 KB |
Output is correct |
22 |
Correct |
27 ms |
9560 KB |
Output is correct |
23 |
Correct |
107 ms |
38240 KB |
Output is correct |
24 |
Correct |
102 ms |
38188 KB |
Output is correct |
25 |
Correct |
1 ms |
316 KB |
Output is correct |
26 |
Correct |
1 ms |
460 KB |
Output is correct |
27 |
Correct |
1 ms |
716 KB |
Output is correct |
28 |
Correct |
2 ms |
716 KB |
Output is correct |
29 |
Correct |
1 ms |
716 KB |
Output is correct |
30 |
Correct |
9 ms |
4296 KB |
Output is correct |
31 |
Correct |
10 ms |
4296 KB |
Output is correct |
32 |
Correct |
57 ms |
19256 KB |
Output is correct |
33 |
Correct |
63 ms |
19496 KB |
Output is correct |
34 |
Correct |
137 ms |
38184 KB |
Output is correct |
35 |
Correct |
134 ms |
37932 KB |
Output is correct |
36 |
Correct |
155 ms |
38268 KB |
Output is correct |
37 |
Correct |
139 ms |
38184 KB |
Output is correct |
38 |
Correct |
0 ms |
332 KB |
Output is correct |
39 |
Correct |
154 ms |
38192 KB |
Output is correct |
40 |
Correct |
124 ms |
38192 KB |
Output is correct |
41 |
Correct |
115 ms |
38272 KB |
Output is correct |
42 |
Correct |
100 ms |
38272 KB |
Output is correct |