#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ff first
#define ss second
ll ttt;
const ll INF=1e18;
const ll MOD=1e9+7;
const ll N=1e5+7;
ll n,m,k;
ll p[N];
ll sz[N];
ll ww[N];
ll ans[N];
vector<ll>h(N,0);
vector<ll>pf(N,0);
vector<ll>w(N,0);
ll calc_ans(ll w, ll h){
ll res=1;
if(w%2==0){
res*=(w/2)%MOD;
res%=MOD;
res*=(w+1)%MOD;
res%=MOD;
}
else{
w++;
res*=(w/2)%MOD;
res%=MOD;
res*=(w-1)%MOD;
res%=MOD;
}
if(h%2==0){
res*=(h/2)%MOD;
res%=MOD;
res*=(h+1)%MOD;
res%=MOD;
}
else{
h++;
res*=(h/2)%MOD;
res%=MOD;
res*=(h-1)%MOD;
res%=MOD;
}
return res;
}
void make_set(int v){
p[v]=v;
sz[v]=1;
ww[v]=w[v];
ans[v]=calc_ans(w[v],h[v]);
}
int find_set(int v){
if(p[v]==v)return v;
return p[v]=find_set(p[v]);
}
void union_sets(int v, int u){
v=find_set(v);
u=find_set(u);
// cout<<v<<" "<<u<<endl;
if(u!=v){
if(sz[u]>sz[v])swap(u,v);
p[u]=v;
sz[v]+=sz[u];
sz[u]=0;
ww[v]+=ww[u];
ww[u]=0;
ans[v]+=ans[u];
ans[v]%=MOD;
ans[u]=0;
}
// cout<<"SYO"<<endl;
}
void upd_ans(int v, ll new_ans){
v=find_set(v);
ans[v]=new_ans;
ans[v]%=MOD;
}
ll get_ww(int v){
return ww[find_set(v)];
}
ll get_sum(int l, int r){
if(l>r)return 0;
if(l<0)l=0;
if(r>n+1)r=n+1;
if(l==0)return pf[r];
return pf[r]-pf[l];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen("input.txt", "r", stdin);
cin>>n;
for(int i=1;i<=n;i++){
cin>>h[i];
}
for(int i=1;i<=n;i++){
cin>>w[i];
pf[i]=pf[i-1]+w[i];
}
vector<int>ord;
for(int i=1;i<=n;i++){
ord.push_back(i);
}
sort(ord.begin(),ord.end(),[](int x, int y){
if(h[x]==h[y])return x<y;
return h[x]>h[y];
});
// cout<<"OK"<<endl;
for(int i:ord){
// cout<<i<<endl;
make_set(i);
ll cw=w[i];
ll cans=0;
if(h[i-1]>=h[i]){
cw+=get_ww(i-1);
cans-=calc_ans(get_ww(i-1),h[i]);
}
if(h[i+1]>h[i]){
cw+=get_ww(i+1);
cans-=calc_ans(get_ww(i+1),h[i]);
}
cans+=calc_ans(cw,h[i]);
// cout<<cans<<endl;
while(cans<0)cans+=MOD;
cans%=MOD;
upd_ans(i,cans);
// cout<<"OK"<<endl;
if(h[i-1]>=h[i]){
union_sets(i-1,i);
}
if(h[i+1]>h[i]){
union_sets(i+1,i);
}
}
cout<<ans[find_set(1)]<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
1 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
18 ms |
5068 KB |
Output is correct |
4 |
Correct |
33 ms |
7444 KB |
Output is correct |
5 |
Correct |
32 ms |
7408 KB |
Output is correct |
6 |
Correct |
32 ms |
7456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2724 KB |
Output is correct |
2 |
Correct |
5 ms |
3280 KB |
Output is correct |
3 |
Correct |
21 ms |
5452 KB |
Output is correct |
4 |
Correct |
31 ms |
8176 KB |
Output is correct |
5 |
Correct |
37 ms |
8308 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
5 ms |
3284 KB |
Output is correct |
4 |
Correct |
19 ms |
5456 KB |
Output is correct |
5 |
Correct |
35 ms |
8172 KB |
Output is correct |
6 |
Correct |
42 ms |
8332 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
5 ms |
3224 KB |
Output is correct |
9 |
Correct |
19 ms |
5584 KB |
Output is correct |
10 |
Correct |
31 ms |
8084 KB |
Output is correct |
11 |
Correct |
35 ms |
8140 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2644 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2728 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2644 KB |
Output is correct |
16 |
Correct |
1 ms |
2644 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2704 KB |
Output is correct |
4 |
Correct |
2 ms |
2772 KB |
Output is correct |
5 |
Correct |
2 ms |
2644 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
2 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
2 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
17 ms |
5080 KB |
Output is correct |
12 |
Correct |
32 ms |
7452 KB |
Output is correct |
13 |
Correct |
30 ms |
7372 KB |
Output is correct |
14 |
Correct |
30 ms |
7372 KB |
Output is correct |
15 |
Correct |
2 ms |
2772 KB |
Output is correct |
16 |
Correct |
5 ms |
3228 KB |
Output is correct |
17 |
Correct |
16 ms |
5464 KB |
Output is correct |
18 |
Correct |
32 ms |
8276 KB |
Output is correct |
19 |
Correct |
34 ms |
8304 KB |
Output is correct |
20 |
Correct |
2 ms |
2720 KB |
Output is correct |
21 |
Correct |
5 ms |
3284 KB |
Output is correct |
22 |
Correct |
19 ms |
5492 KB |
Output is correct |
23 |
Correct |
33 ms |
8140 KB |
Output is correct |
24 |
Correct |
32 ms |
8180 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2772 KB |
Output is correct |
28 |
Correct |
2 ms |
2716 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
6 ms |
3220 KB |
Output is correct |
31 |
Correct |
6 ms |
3220 KB |
Output is correct |
32 |
Correct |
24 ms |
5456 KB |
Output is correct |
33 |
Correct |
23 ms |
5560 KB |
Output is correct |
34 |
Correct |
44 ms |
8044 KB |
Output is correct |
35 |
Correct |
45 ms |
8012 KB |
Output is correct |
36 |
Correct |
44 ms |
8268 KB |
Output is correct |
37 |
Correct |
44 ms |
8268 KB |
Output is correct |
38 |
Correct |
2 ms |
2700 KB |
Output is correct |
39 |
Correct |
43 ms |
8168 KB |
Output is correct |
40 |
Correct |
37 ms |
8276 KB |
Output is correct |
41 |
Correct |
35 ms |
8232 KB |
Output is correct |
42 |
Correct |
41 ms |
8168 KB |
Output is correct |