/* Author : Mychecksdead */
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define PI 3.1415926535
#define pb push_back
#define setp() cout << setprecision(15)
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " is " << x << '\n';
const int N = 1e6+100, M = 1e5+10, F = 2147483646, K = 20;
int n, ls[N], rs[N];
ll w[N], h[N], pref[N];
ll comb2(ll x){
return (x*(x+1)/2) % MOD;
}
ll p(int l, int r){
if(l > 0)
return pref[r] - pref[l - 1];
return pref[r];
}
void solve(){
cin >> n;
for(int i = 0; i < n; ++i) cin >> h[i];
for(int i = 0; i < n; ++i) cin >> w[i];
pref[0] = w[0];
for(int i = 1; i < n; ++i) pref[i] = (pref[i - 1] + w[i]) % MOD;
deque<pair<int, int>> q;
ls[0] = -1;
q.pb({h[0], 0});
for(int i = 1; i < n; ++i){
while(!q.empty() && q.back().first >= h[i]) q.pop_back();
if(!q.empty()) ls[i] = q.back().second;
else ls[i] = -1;
q.pb({h[i], i});
}
q.clear();
rs[n - 1] = n;
q.pb({h[n - 1], n - 1});
for(int i = n - 2; i >= 0; --i){
while(!q.empty() && q.back().first > h[i]) q.pop_back();
if(!q.empty()) rs[i] = q.back().second;
else rs[i] = n;
q.pb({h[i], i});
}
ll ans = 0;
for(int i = 0; i < n; ++i){
ll a = (p(ls[i] + 1, i) * p(i, rs[i] - 1) % MOD);
(ans += (a - comb2(w[i] - 1)) * comb2(h[i])) %= MOD;
}
cout << (ans + MOD) % MOD;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int T = 1, aa;
// cin >> T;aa=T;
while(T--){
// cout << "Case #" << aa-T << ": ";
solve();
cout << '\n';
}
return 0;
}
Compilation message
fancyfence.cpp: In function 'int main()':
fancyfence.cpp:69:16: warning: unused variable 'aa' [-Wunused-variable]
69 | int T = 1, aa;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
11 ms |
2004 KB |
Output is correct |
4 |
Correct |
20 ms |
4016 KB |
Output is correct |
5 |
Correct |
29 ms |
3796 KB |
Output is correct |
6 |
Correct |
29 ms |
3528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
724 KB |
Output is correct |
3 |
Correct |
13 ms |
2276 KB |
Output is correct |
4 |
Correct |
28 ms |
4172 KB |
Output is correct |
5 |
Correct |
26 ms |
4172 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
724 KB |
Output is correct |
4 |
Correct |
12 ms |
2228 KB |
Output is correct |
5 |
Correct |
25 ms |
4176 KB |
Output is correct |
6 |
Correct |
28 ms |
4216 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
752 KB |
Output is correct |
9 |
Correct |
13 ms |
2072 KB |
Output is correct |
10 |
Correct |
24 ms |
3952 KB |
Output is correct |
11 |
Correct |
24 ms |
4056 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 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 |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 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 |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
9 ms |
2004 KB |
Output is correct |
12 |
Correct |
19 ms |
4112 KB |
Output is correct |
13 |
Correct |
19 ms |
3796 KB |
Output is correct |
14 |
Correct |
18 ms |
3540 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
724 KB |
Output is correct |
17 |
Correct |
12 ms |
2260 KB |
Output is correct |
18 |
Correct |
25 ms |
4296 KB |
Output is correct |
19 |
Correct |
26 ms |
4284 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
724 KB |
Output is correct |
22 |
Correct |
18 ms |
2132 KB |
Output is correct |
23 |
Correct |
31 ms |
3960 KB |
Output is correct |
24 |
Correct |
31 ms |
4032 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
3 ms |
596 KB |
Output is correct |
31 |
Correct |
3 ms |
596 KB |
Output is correct |
32 |
Correct |
12 ms |
1820 KB |
Output is correct |
33 |
Correct |
13 ms |
1876 KB |
Output is correct |
34 |
Correct |
23 ms |
3412 KB |
Output is correct |
35 |
Correct |
25 ms |
3468 KB |
Output is correct |
36 |
Correct |
24 ms |
3392 KB |
Output is correct |
37 |
Correct |
23 ms |
3412 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
22 ms |
3412 KB |
Output is correct |
40 |
Correct |
23 ms |
3436 KB |
Output is correct |
41 |
Correct |
22 ms |
3468 KB |
Output is correct |
42 |
Correct |
23 ms |
3864 KB |
Output is correct |