#include <bits/stdc++.h>
#define MEM 111111
#define sanic ios_base::sync_with_stdio(0)
#define x first
#define y second
#define pf push_front
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz size()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pi;
const ll INF = 1e13+7;
const ll MOD = 1e9+7;
ll gcd(ll a, ll b){
if(a%b) return gcd(b, a%b);
return b;
}
ll c2(ll n){
ll ret = n*(n-1)/2LL;
ret %= MOD;
ret += MOD;
ret %= MOD;
return ret;
}
ll hp(ll l, ll r){
ll a=c2(l+1);
ll b=c2(r+1);
return ((b-a)%MOD+MOD)%MOD;
}
ll n,m,t,c;
ll h[MEM], w[MEM];
signed main(){
sanic; cin.tie(0); cout.tie(0);
cin >> n;
for(int i=0; i<n; i++)
cin >> h[i];
for(int i=0; i<n; i++)
cin >> w[i];
stack<pi> s;
ll ans=0;
for(int i=0; i<n; i++){
if(s.empty()) {
s.push({h[i], w[i]});
continue;
}
while(s.top().x>h[i]){
ll h1=s.top().x, w1=s.top().y;
s.pop();
if(s.empty()) break;
ans += ((hp(s.top().x, h1)*c2(w1+1))%MOD+MOD)%MOD;
ans %= MOD;
ans += MOD;
ans %= MOD;
pi ff=s.top();
s.pop();
ff.y += w1;
s.push(ff);
}
s.push({h[i], w[i]});
}
while(s.sz>1){
ll h1=s.top().x, w1=s.top().y;
s.pop();
ans += ((hp(s.top().x, h1)*c2(w1+1))%MOD+MOD)%MOD;
ans %= MOD;
ans += MOD;
ans %= MOD;
pi ff=s.top();
s.pop();
ff.y += w1;
s.push(ff);
}
//cout << ans << ' ';
ll h1=s.top().x, w1=s.top().y;
//cout << h1 << ' ' << w1 << '\n';
ans += ((hp(0, h1)*c2(w1+1))%MOD+MOD)%MOD;
ans %= MOD;
ans += MOD;
ans %= MOD;
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |