이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<map>
#include<string>
#include<algorithm>
#include<vector>
#include<string.h>
#include<utility>
#include<set>
#include<cmath>
#include<queue>
#include<deque>
#include<functional>
#include<stack>
#include<limits.h>
#include<iomanip>
#include<unordered_map>
using namespace std;
#define MOD 1000000007
#define int long long
#define endl '\n'
int cal(int a, int b){
int f=(a*(a+1))%MOD*((MOD+1)/2)%MOD;
int s=(b*(b+1))%MOD*((MOD+1)/2)%MOD;
return (f*s)%MOD;
}
void solve(){
int n; cin>>n;
vector<int> pref(n+1);
vector<int> h(n+1),w(n+1);
for(int i=1; i<=n; i++) cin>>h[i];
for(int i=1; i<=n; i++) cin>>w[i];
vector<int> up(n+1),dw(n+2);
for(int i=1; i<=n; i++) up[i]=(up[i-1]+w[i])%MOD;
for(int i=n; i>=1; i--) dw[i]=(dw[i+1]+w[i])%MOD;
stack<int> f;
f.push(0);
int ans=0;
for(int i=1; i<=n; i++){
while(f.size()>1&&h[f.top()]>=h[i]) f.pop();
ans=(ans+cal(up[i]-up[f.top()],h[i]))%MOD;
ans=(ans-cal(up[i]-up[f.top()]-w[i],h[i])+MOD)%MOD;
f.push(i);
}
stack<int> s;
s.push(n+1);
int cur=0;
for(int i=n; i>=1; i--){
while(s.size()>1&&h[s.top()]>=h[i]) s.pop();
if(h[i]==h[i+1]) cur=(cur+w[i])%MOD;
else cur=w[i];
if(dw[i]-dw[s.top()]!=cur){
ans=(ans-cal(cur,h[i])+MOD)%MOD;
ans=(ans+cal(dw[i]-dw[s.top()],h[i]))%MOD;
ans=(ans-cal(dw[i]-dw[s.top()]-w[i],h[i])+MOD)%MOD;
}
s.push(i);
}
cout<<ans;
}
signed main(){
// freopen("hps.in", "r", stdin);
// freopen("hps.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--){
solve();
cout<<endl;
}
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... |