#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define FOR(i, a, b, s) for (int i = (a); i < (b); i += s)
#define F0R(i, a) for (int i = 0; i < (a); i++)
ll mod = 1e9+7;
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n; cin >> n;
vector<ll> h(n+2,0),w(n+2,0);
unordered_map<int,int> bounds;
F0R(i,n) {cin >> h[i+1]; bounds[h[i+1]]=INT_MIN;}
F0R(i,n) {cin >> w[i+1]; w[i+1]=(w[i+1]+w[i])%mod;} // ps
vector<int> r(n+2,n+1),l(n+2,0);
// nearest smaller values on h (or 0 if it doesn't exist)
stack<int> s,s2; s.push(0); s2.push(n+1);
FOR(i,1,n+1,1) {
int j = n-i+1;
while (s.size() && h[s.top()]>=h[i]) {s.pop();}
l[i] = s.top(); s.push(i);
while (s2.size() && h[s2.top()]>=h[j]) {s2.pop();}
r[j] = s2.top(); s2.push(j);
}
// store a left right bound for all h (offline) to avoid equal heights recalc
// update bounds if disjoint
// otherwise find # of subgrids, and then subtract number of subgrids distinctly
// in the max(right,left) smallest values
ll ans = 0;
auto add = [&](ll a, ll b)->ll {return (a+b)%mod;};
auto sub = [&](ll a, ll b)->ll {return (a+mod-b)%mod;};
auto mult = [&](ll a, ll b)->ll {return a*b%mod;};
auto subgrids = [&](ll width, ll height)->ll {return mult(mult(width*width%mod+width%mod,height*height%mod+height%mod),250000002);};
FOR(i,1,n+1,1) {
if (l[i]>=bounds[h[i]]) {
bounds[h[i]]=r[i];
ll width = sub(w[r[i]-1],w[l[i]]);
ll height = h[i], extra = max(h[r[i]],h[l[i]]);
ans = add(ans,subgrids(width,height));
ans = sub(ans,subgrids(width,extra));
}
} cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
7 ms |
2232 KB |
Output is correct |
4 |
Correct |
14 ms |
3940 KB |
Output is correct |
5 |
Correct |
16 ms |
4000 KB |
Output is correct |
6 |
Correct |
14 ms |
3944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2 ms |
860 KB |
Output is correct |
3 |
Correct |
8 ms |
2488 KB |
Output is correct |
4 |
Correct |
19 ms |
4700 KB |
Output is correct |
5 |
Correct |
17 ms |
4700 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
860 KB |
Output is correct |
4 |
Correct |
8 ms |
2544 KB |
Output is correct |
5 |
Correct |
16 ms |
4700 KB |
Output is correct |
6 |
Correct |
16 ms |
4872 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
3 ms |
1116 KB |
Output is correct |
9 |
Correct |
11 ms |
3420 KB |
Output is correct |
10 |
Correct |
31 ms |
9320 KB |
Output is correct |
11 |
Correct |
33 ms |
9424 KB |
Output is correct |
12 |
Correct |
0 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
352 KB |
Output is correct |
5 |
Correct |
0 ms |
352 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
344 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
7 ms |
2136 KB |
Output is correct |
12 |
Correct |
18 ms |
3944 KB |
Output is correct |
13 |
Correct |
14 ms |
3944 KB |
Output is correct |
14 |
Correct |
13 ms |
3820 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
2 ms |
860 KB |
Output is correct |
17 |
Correct |
8 ms |
2396 KB |
Output is correct |
18 |
Correct |
16 ms |
4700 KB |
Output is correct |
19 |
Correct |
18 ms |
4700 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
3 ms |
1116 KB |
Output is correct |
22 |
Correct |
11 ms |
3520 KB |
Output is correct |
23 |
Correct |
31 ms |
9416 KB |
Output is correct |
24 |
Correct |
41 ms |
9424 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
428 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
3 ms |
1116 KB |
Output is correct |
31 |
Correct |
4 ms |
1116 KB |
Output is correct |
32 |
Correct |
10 ms |
2396 KB |
Output is correct |
33 |
Correct |
16 ms |
4716 KB |
Output is correct |
34 |
Correct |
42 ms |
9008 KB |
Output is correct |
35 |
Correct |
20 ms |
4444 KB |
Output is correct |
36 |
Correct |
43 ms |
9156 KB |
Output is correct |
37 |
Correct |
28 ms |
7268 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
25 ms |
6876 KB |
Output is correct |
40 |
Correct |
33 ms |
9164 KB |
Output is correct |
41 |
Correct |
31 ms |
9164 KB |
Output is correct |
42 |
Correct |
33 ms |
9424 KB |
Output is correct |