#include<bits/stdc++.h>
#define MOD 1000000007
#define SIZE 100010
using namespace std;
typedef long long ll;
struct st2
{
ll h,w,idx;
};
ll N,psum[SIZE],ans,seg2[SIZE<<2],lz[SIZE<<2];
pair<ll,ll>seg[SIZE<<2];
st2 A[SIZE];
vector<st2>v;
void init(ll l,ll r,ll node)
{
if(l==r){
seg[node]={v[l].h,v[l].h};
return;
}
ll mid=l+r>>1;
init(l,mid,node<<1);
init(mid+1,r,node<<1|1);
seg[node]={max(seg[node<<1].first,seg[node<<1|1].first),min(seg[node<<1].second,seg[node<<1|1].second)};
}
pair<ll,ll>query(ll l,ll r,ll node,ll s,ll e)
{
if(l>e||r<s||s>e)return {0,1e18};
if(l==r)return seg[node];
ll mid=l+r>>1;
pair<ll,ll>p=query(l,mid,node<<1,s,e),q=query(mid+1,r,node<<1|1,s,e);
return {max(p.first,q.first),min(p.second,q.second)};
}
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;
for(int 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;
}
init(1,N,1);
sort(v.begin(),v.end(),comp);
for(ll j=1;j<=N;j++){
ll i=v[j].idx;
ll l=1,r=i,mid,a,b,c,d,mx=0;
pair<ll,ll>t;
while(l<r){
mid=l+r>>1;
t=query(1,N,1,mid,i);
if(t.second>=v[j].h)r=mid;
else l=mid+1;
}
a=r;
l=i,r=N;
while(l<r){
mid=l+r+1>>1;
t=query(1,N,1,i,mid);
if(t.second>=v[j].h)l=mid;
else r=mid-1;
}
b=l;
l=1,r=i;
while(l<r){
mid=l+r>>1;
t=query(1,N,1,mid,i);
if(t.first<=v[j].h)r=mid;
else l=mid+1;
}
c=r;
l=i,r=N;
while(l<r){
mid=l+r+1>>1;
t=query(1,N,1,i,mid);
if(t.first<=v[j].h)l=mid;
else r=mid-1;
}
d=l;
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 init(ll, ll, ll)':
fancyfence.cpp:20:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
20 | ll mid=l+r>>1;
| ~^~
fancyfence.cpp: In function 'std::pair<long long int, long long int> query(ll, ll, ll, ll, ll)':
fancyfence.cpp:29:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
29 | ll mid=l+r>>1;
| ~^~
fancyfence.cpp: In function 'void upd(ll, ll, ll, ll, ll, ll)':
fancyfence.cpp:49:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
49 | ll mid=l+r>>1;
| ~^~
fancyfence.cpp: In function 'll que(ll, ll, ll, ll)':
fancyfence.cpp:58:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
58 | ll mid=l+r>>1;
| ~^~
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:107:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
107 | mid=l+r>>1;
| ~^~
fancyfence.cpp:115:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
115 | mid=l+r+1>>1;
| ~~~^~
fancyfence.cpp:123:18: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
123 | mid=l+r>>1;
| ~^~
fancyfence.cpp:131:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
131 | mid=l+r+1>>1;
| ~~~^~
fancyfence.cpp:104:28: warning: variable 'c' set but not used [-Wunused-but-set-variable]
104 | ll l=1,r=i,mid,a,b,c,d,mx=0;
| ^
fancyfence.cpp:104:30: warning: variable 'd' set but not used [-Wunused-but-set-variable]
104 | ll l=1,r=i,mid,a,b,c,d,mx=0;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
18 ms |
444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
316 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 |
1 ms |
320 KB |
Output is correct |
2 |
Correct |
16 ms |
424 KB |
Output is correct |
3 |
Execution timed out |
1087 ms |
3752 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
3 ms |
652 KB |
Output is correct |
3 |
Correct |
12 ms |
1676 KB |
Output is correct |
4 |
Correct |
21 ms |
2844 KB |
Output is correct |
5 |
Correct |
24 ms |
2804 KB |
Output is correct |
6 |
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 |
332 KB |
Output is correct |
3 |
Correct |
3 ms |
588 KB |
Output is correct |
4 |
Correct |
13 ms |
1588 KB |
Output is correct |
5 |
Correct |
21 ms |
2856 KB |
Output is correct |
6 |
Correct |
22 ms |
2840 KB |
Output is correct |
7 |
Correct |
65 ms |
460 KB |
Output is correct |
8 |
Execution timed out |
1088 ms |
1824 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
22 ms |
456 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
16 ms |
444 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
74 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
6 ms |
432 KB |
Output is correct |
13 |
Correct |
23 ms |
460 KB |
Output is correct |
14 |
Correct |
21 ms |
580 KB |
Output is correct |
15 |
Correct |
18 ms |
460 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 |
18 ms |
444 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
17 ms |
460 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 |
296 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
1 ms |
316 KB |
Output is correct |
10 |
Correct |
17 ms |
332 KB |
Output is correct |
11 |
Execution timed out |
1066 ms |
3672 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |