This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
Compilation message (stderr)
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:108:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
108 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |