#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define Mp make_pair
#define sep ' '
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define kill(res) cout << res << '\n', exit(0);
#define set_dec(x) cout << fixed << setprecision(x);
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll N = 2e5 + 50;
const ll Mod = 1e9 + 7;
ll n, h[N], w[N], pre[N], L[N], R[N], inv2;
vector<int> jahat, vv;
ll power(ll a, ll b, ll md = Mod){
ll ans = 1;
for(; b; b /= 2, a = a * a % md){
if(b % 2) ans = ans * a % md;
}
return ans;
}
ll c2(ll x){
return x % Mod * ((x+Mod-1) % Mod) % Mod * inv2 % Mod;
}
bool cmp(int i, int j){
return (Mp(h[i], i) < Mp(h[j], j));
}
int main(){
fast_io;
inv2 = power(2, Mod-2);
cin >> n;
for(int i = 1; i <= n; i++) cin >> h[i];
for(int i = 1; i <= n; i++){
cin >> w[i]; pre[i] = pre[i-1] + w[i];
}
vector<int> vec; vec.pb(0);
for(int i = 1; i <= n; i++){
while(h[vec.back()] >= h[i]) vec.pop_back();
L[i] = vec.back(); vec.pb(i);
}
vec.clear(); vec.pb(n+1);
for(int i = n; i > 0; i--){
while(h[vec.back()] >= h[i]) vec.pop_back();
R[i] = vec.back(); vec.pb(i);
}
for(int i = 1; i <= n; i++) jahat.pb(i);
sort(all(jahat), cmp);
vv.pb(jahat[0]);
for(int i = 1; i < n; i++) if(h[jahat[i-1]] != h[jahat[i]] || L[jahat[i-1]] != L[jahat[i]]) vv.pb(jahat[i]);
ll res = 0;
for(int i: vv){
ll mx = max(h[L[i]], h[R[i]]);
ll th1 = (h[i] - mx) * mx + (h[i] - mx) + c2(h[i] - mx); th1 %= Mod;
ll th2 = c2(pre[R[i]-1] - pre[L[i]]) + pre[R[i]-1] - pre[L[i]]; th2 %= Mod;
res = (res + th1 * th2) % Mod;
}
cout << res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4556 KB |
Output is correct |
2 |
Correct |
1 ms |
4564 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
13 ms |
6308 KB |
Output is correct |
4 |
Correct |
21 ms |
8160 KB |
Output is correct |
5 |
Correct |
23 ms |
8148 KB |
Output is correct |
6 |
Correct |
22 ms |
8152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
12 ms |
6732 KB |
Output is correct |
4 |
Correct |
22 ms |
8916 KB |
Output is correct |
5 |
Correct |
28 ms |
9180 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4572 KB |
Output is correct |
3 |
Correct |
3 ms |
4956 KB |
Output is correct |
4 |
Correct |
11 ms |
6748 KB |
Output is correct |
5 |
Correct |
27 ms |
8912 KB |
Output is correct |
6 |
Correct |
28 ms |
8936 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
3 ms |
5060 KB |
Output is correct |
9 |
Correct |
12 ms |
6872 KB |
Output is correct |
10 |
Correct |
23 ms |
9700 KB |
Output is correct |
11 |
Correct |
24 ms |
9860 KB |
Output is correct |
12 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4440 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
1 ms |
4440 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4564 KB |
Output is correct |
10 |
Correct |
1 ms |
4444 KB |
Output is correct |
11 |
Correct |
12 ms |
6332 KB |
Output is correct |
12 |
Correct |
21 ms |
8160 KB |
Output is correct |
13 |
Correct |
27 ms |
8180 KB |
Output is correct |
14 |
Correct |
23 ms |
8148 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
3 ms |
4956 KB |
Output is correct |
17 |
Correct |
12 ms |
6728 KB |
Output is correct |
18 |
Correct |
21 ms |
8916 KB |
Output is correct |
19 |
Correct |
23 ms |
8832 KB |
Output is correct |
20 |
Correct |
2 ms |
4444 KB |
Output is correct |
21 |
Correct |
4 ms |
4956 KB |
Output is correct |
22 |
Correct |
12 ms |
6884 KB |
Output is correct |
23 |
Correct |
28 ms |
9560 KB |
Output is correct |
24 |
Correct |
24 ms |
9652 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4440 KB |
Output is correct |
28 |
Correct |
1 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
5 ms |
5092 KB |
Output is correct |
31 |
Correct |
4 ms |
4952 KB |
Output is correct |
32 |
Correct |
16 ms |
6932 KB |
Output is correct |
33 |
Correct |
16 ms |
6880 KB |
Output is correct |
34 |
Correct |
32 ms |
9040 KB |
Output is correct |
35 |
Correct |
34 ms |
9168 KB |
Output is correct |
36 |
Correct |
37 ms |
9424 KB |
Output is correct |
37 |
Correct |
44 ms |
9368 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
34 ms |
9428 KB |
Output is correct |
40 |
Correct |
29 ms |
9424 KB |
Output is correct |
41 |
Correct |
26 ms |
9420 KB |
Output is correct |
42 |
Correct |
23 ms |
9560 KB |
Output is correct |