#include <bits/stdc++.h>
#define ve vector
#define ld long double
#define ll long long
#define all(x) x.begin(), x.end()
#define fi first
#define se second
using namespace std;
using namespace chrono;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#pragma GCC optimize("Ofast")
const int MAXN = 1e5+10;
const ll mod = 1e9+7;
struct rect{
ll h, w, id;
};
inline bool comp(const rect& r1, const rect& r2){
return r1.h < r2.h;
}
rect v[MAXN];
ll pref[MAXN];
int n;
ll binpow(ll a, ll n){
if(n == 0)
return 1;
if(n & 1)
return (a*binpow(a, n - 1))%mod;
ll t = binpow(a, n / 2);
return (t*t) % mod;
}
ll f(ll w){
ll csum = (w*(w - 1)+mod)%mod;
csum = (csum*binpow(2, mod - 2)) % mod;
csum = (csum + w) % mod;
return csum;
}
void solve(){
cin >> n;
set<int> del;
for(int i = 1; i <= n; i++)
cin >> v[i].h;
for(int i = 1; i <= n; i++)
cin >> v[i].w;
for(int i = 1; i <= n; i++)
v[i].id = i;
for(int i = 1; i <= n; i++)
pref[i] = (pref[i - 1] + v[i].w) % mod;
sort(v + 1, v + 1 + n, comp);
ll res = 0;
ll w = (pref[n] - pref[0] + mod) % mod;
ll csum = f(w);
for(int i = 1; i <= n; i++){
auto it = del.lower_bound(v[i].id);
ll l, r;
if(it == del.end())
r = n;
else r = *it - 1;
if(it == del.begin())
l = 0;
else
l = *prev(it);
ll ph;
if(i == 1)
ph = 0;
else ph = v[i - 1].h;
ll sum = ((ph + 1)+v[i].h) % mod;
sum = (sum*(v[i].h - ph) + mod)%mod;
sum = (sum*binpow(2, mod - 2)) % mod;
ll pr = (csum*sum) % mod;
res = (res + pr) % mod;
del.insert(v[i].id);
w = (pref[r] - pref[l] + mod) % mod;
csum = (csum - f(w) + mod) % mod;
w = (pref[v[i].id - 1] - pref[l] + mod) % mod;
csum = (csum + f(w)) % mod;
w = (pref[r] - pref[v[i].id] + mod) % mod;
csum = (csum + f(w)) % mod;
}
cout << res;
}
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
int T = 1;
/* cin >> T; */
while(T--){
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
27 ms |
4912 KB |
Output is correct |
4 |
Correct |
56 ms |
8016 KB |
Output is correct |
5 |
Correct |
58 ms |
8124 KB |
Output is correct |
6 |
Correct |
57 ms |
8224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
6 ms |
2908 KB |
Output is correct |
3 |
Correct |
28 ms |
4864 KB |
Output is correct |
4 |
Correct |
57 ms |
8116 KB |
Output is correct |
5 |
Correct |
57 ms |
8276 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
6 ms |
2908 KB |
Output is correct |
4 |
Correct |
28 ms |
4880 KB |
Output is correct |
5 |
Correct |
59 ms |
8020 KB |
Output is correct |
6 |
Correct |
59 ms |
8260 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
6 ms |
2904 KB |
Output is correct |
9 |
Correct |
29 ms |
4956 KB |
Output is correct |
10 |
Correct |
61 ms |
8064 KB |
Output is correct |
11 |
Correct |
60 ms |
8020 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2516 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2392 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2652 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2520 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
28 ms |
5412 KB |
Output is correct |
12 |
Correct |
55 ms |
9308 KB |
Output is correct |
13 |
Correct |
57 ms |
9232 KB |
Output is correct |
14 |
Correct |
55 ms |
9300 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
6 ms |
3164 KB |
Output is correct |
17 |
Correct |
28 ms |
5968 KB |
Output is correct |
18 |
Correct |
58 ms |
10096 KB |
Output is correct |
19 |
Correct |
57 ms |
10068 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
6 ms |
3164 KB |
Output is correct |
22 |
Correct |
30 ms |
5820 KB |
Output is correct |
23 |
Correct |
62 ms |
10064 KB |
Output is correct |
24 |
Correct |
66 ms |
10068 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2540 KB |
Output is correct |
29 |
Correct |
1 ms |
2392 KB |
Output is correct |
30 |
Correct |
8 ms |
3164 KB |
Output is correct |
31 |
Correct |
8 ms |
3160 KB |
Output is correct |
32 |
Correct |
36 ms |
5712 KB |
Output is correct |
33 |
Correct |
38 ms |
5968 KB |
Output is correct |
34 |
Correct |
81 ms |
9812 KB |
Output is correct |
35 |
Correct |
74 ms |
9924 KB |
Output is correct |
36 |
Correct |
81 ms |
10064 KB |
Output is correct |
37 |
Correct |
80 ms |
10200 KB |
Output is correct |
38 |
Correct |
1 ms |
2388 KB |
Output is correct |
39 |
Correct |
77 ms |
10064 KB |
Output is correct |
40 |
Correct |
75 ms |
10196 KB |
Output is correct |
41 |
Correct |
62 ms |
10064 KB |
Output is correct |
42 |
Correct |
62 ms |
10176 KB |
Output is correct |