#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;
}
ll p=0;
while(s.top().x>h[i]){
ll h1=s.top().x, w1=s.top().y;
s.pop();
ans = (ans+(hp(max((s.empty() ? h[i]:s.top().x), h[i]), h1)*c2(w1+1))%MOD+MOD)%MOD;
p = w1;
//cout << h1 << ' ' << w1 << '\n';
if(s.empty() || s.top().x<h[i]) break;
pi ff=s.top();
s.pop();
ff.y += w1;
ff.y %= MOD;
ff.y += MOD;
ff.y %= MOD;
s.push(ff);
}
//cout << ans << '\n';
if(!s.empty() && s.top().x==h[i]) {
pi ff=s.top();
s.pop();
ff.y += w[i];
ff.y %= MOD;
ff.y += MOD;
ff.y %= MOD;
s.push(ff);
}
else s.push({h[i], ((w[i]+p)%MOD+MOD)%MOD});
/*
stack<pi> gg=s;
while(!gg.empty()){
cout << gg.top().x << ' ' << gg.top().y << '\n';
gg.pop();
}*/
}
while(!s.empty()){
ll h1=s.top().x, w1=s.top().y;
s.pop();
ans = (ans+(hp((s.empty()?0:s.top().x), h1)*c2(w1+1))%MOD+MOD)%MOD;
//cout << h1 << ' ' << w1 << '\n';
if(s.empty()) break;
pi ff=s.top();
s.pop();
ff.y += w1;
ff.y %= MOD;
ff.y += MOD;
ff.y %= MOD;
s.push(ff);
}
cout << ans;
}
/*
10
1 3 2 4 2 1 3 5 6 7
2 3 5 4 6 2 3 5 6 7
3
2 3 1
3 1 2
5
2 3 3 4 1
2 4 5 1 2
660
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
300 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
29 ms |
1624 KB |
Output is correct |
4 |
Correct |
57 ms |
2896 KB |
Output is correct |
5 |
Correct |
70 ms |
2924 KB |
Output is correct |
6 |
Correct |
54 ms |
3012 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
9 ms |
468 KB |
Output is correct |
3 |
Correct |
38 ms |
1144 KB |
Output is correct |
4 |
Correct |
97 ms |
1932 KB |
Output is correct |
5 |
Correct |
82 ms |
1940 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
8 ms |
524 KB |
Output is correct |
4 |
Correct |
42 ms |
1100 KB |
Output is correct |
5 |
Correct |
79 ms |
1872 KB |
Output is correct |
6 |
Correct |
82 ms |
2016 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
9 ms |
724 KB |
Output is correct |
9 |
Correct |
39 ms |
1472 KB |
Output is correct |
10 |
Correct |
85 ms |
3596 KB |
Output is correct |
11 |
Correct |
81 ms |
3528 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
324 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
308 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
308 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
31 ms |
1616 KB |
Output is correct |
12 |
Correct |
51 ms |
2968 KB |
Output is correct |
13 |
Correct |
65 ms |
2904 KB |
Output is correct |
14 |
Correct |
59 ms |
3020 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
10 ms |
596 KB |
Output is correct |
17 |
Correct |
40 ms |
1980 KB |
Output is correct |
18 |
Correct |
74 ms |
3700 KB |
Output is correct |
19 |
Correct |
81 ms |
3868 KB |
Output is correct |
20 |
Correct |
1 ms |
320 KB |
Output is correct |
21 |
Correct |
9 ms |
716 KB |
Output is correct |
22 |
Correct |
42 ms |
2348 KB |
Output is correct |
23 |
Correct |
75 ms |
5280 KB |
Output is correct |
24 |
Correct |
82 ms |
5352 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
10 ms |
656 KB |
Output is correct |
31 |
Correct |
9 ms |
580 KB |
Output is correct |
32 |
Correct |
40 ms |
1948 KB |
Output is correct |
33 |
Correct |
43 ms |
1988 KB |
Output is correct |
34 |
Correct |
86 ms |
3532 KB |
Output is correct |
35 |
Correct |
74 ms |
3524 KB |
Output is correct |
36 |
Correct |
83 ms |
3796 KB |
Output is correct |
37 |
Correct |
87 ms |
3800 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
80 ms |
3836 KB |
Output is correct |
40 |
Correct |
79 ms |
3724 KB |
Output is correct |
41 |
Correct |
80 ms |
3908 KB |
Output is correct |
42 |
Correct |
104 ms |
4576 KB |
Output is correct |