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;
#define int long long
int n,m;
int mod=1e9+7;
int MOD(int x) {
return (x+mod)%mod;
}
int clc(int x, int y) {
x=MOD(x);
y=MOD(y);
return (((((x*(x+1)))/(long long)2)%mod)*((((y*(y+1)))/(long long)2)%mod))%mod; //non cest bon
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n;
vector<int> h(n+1);
vector<int> w(n+1);
int totalw=0;
for (int i = 1; i <= n; i++) cin>>h[i];
for (int i = 1; i <= n; i++) cin >> w[i];
h[0]=0;
stack<pair<int,int>> stck;
int outp=0;
stck.push({h[1],w[1]});
for (int i = 2; i <= n; i++)
{
int hght=stck.top().first;
int wdht=stck.top().second;
stck.pop();
if(hght==h[i]) {
wdht+=w[i];
stck.push({hght, wdht});
}
else if(hght<h[i]){
stck.push({hght, wdht});
stck.push({h[i], w[i]});
}else{
bool b=true;
vector<pair<int,int>> sb4;
int copyw=w[i];
while(hght>h[i]){
w[i]=(w[i]+wdht)%mod;
sb4.push_back({wdht,hght});
if(!b) stck.pop();
if(stck.empty()) break;
b=false;
hght=stck.top().first;
wdht=stck.top().second;
}
int totw=w[i]-copyw;
int last=0;
for (int j = sb4.size()-1; j >= 0; j--)
{
int height=sb4[j].second, width=sb4[j].first;
int side1=clc(totw,height)-clc(totw,last);
last=height;
side1=MOD(side1); //
outp=MOD(outp+side1); //jai pas compris
totw-=width;
}
int min=clc(MOD(w[i]-copyw),h[i]);
outp=MOD(outp-min);
stck.push({h[i], w[i]});
}
}
h.clear();
w.clear();
int newN=stck.size();
h.resize(newN);
w.resize(newN);
totalw=0;
for (int i = newN; i > 0; i--)
{
h[i]=stck.top().first;
w[i]=stck.top().second;
totalw=(totalw+w[i])%mod;
stck.pop();
}
h[0]=0;
for (int i = 1; i <= newN; i++)
{
int side1=clc(totalw,h[i])-clc(totalw,h[i-1]);
side1=MOD(side1); //
outp=MOD(outp+side1); //jai pas compris
totalw-=w[i];
}
cout << outp << "\n";
return 0;
}
# | 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... |