/*
~2021~
*/
# include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
typedef long long ll;
#define pii pair<int,int>
#define debug(x) cout << #x << '=' << x << endl;
const int N = 2e5 + 5;
const int INF = 1e9 + 5;
const ll mod = 1e9+7;
ll n,par[N],sz[N],ans,temp[N];
bool visited[N];
struct state{
int fi,se,idx;
};
state arr[N];
bool cmp(state a,state b){
if(a.fi == b.fi) return a.se > b.se;
return a.fi > b.fi;
}
ll sub(ll x,ll y){
return ((x % mod) - (y % mod) + mod) % mod;
}
ll add(ll x,ll y){
return ((x % mod) + (y % mod) ) % mod;
}
ll mul(ll x,ll y){
return ((x % mod) * (y % mod) ) % mod;
}
ll count(ll x){
// if(x % 2 == 0) return (x/2 * (x-1)) % mod;
// return ((x-1)/2 * x) % mod;
ll p=x%mod,q=(x-1)%mod;
return (((p * q)%mod) * 500000004)%mod;
}
int find(int u){
if(par[u] == u) return u;
return par[u] = find(par[u]);
}
void connect(int a,int b){
if(find(a) == find(b)) return;
sz[find(a)] += sz[find(b)];
par[find(b)] = find(a);
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
//freopen(,r,stdin);
//freopen(,w,stdout);
cin>>n;
for(int i = 1;i<=n;i++){
cin>>arr[i].fi;
temp[i] = arr[i].fi;
arr[i].idx = i;
}
for(int i = 1;i<=n;i++){
cin>>arr[i].se;
ans = add(ans,mul(count(arr[i].fi + 1),count(arr[i].se + 1)));
ans %= mod;
}
for(int i = 1;i<=n;i++){
par[i] = i;
sz[i] = arr[i].se;
}
sort(arr + 1,arr + n + 1,cmp);
visited[0] = 1;
visited[n + 1] = 1;
for(int i = 1;i<=n;i++){
int indexx = arr[i].idx;
ll totl = arr[i].se,totr = arr[i].se;
if(visited[indexx - 1]) totl = sz[find(indexx-1)] + arr[i].se;
if(visited[indexx + 1]) totr = (sz[find(indexx + 1)] + arr[i].se);
ans = add(ans,mul(mul(totl,totr),count(arr[i].fi + 1)));
ans %= mod;
ans =sub(ans,mul(mul(arr[i].se , arr[i].se),count(arr[i].fi + 1)));
if(indexx > 1 && visited[indexx-1]) connect(indexx,indexx-1);
if(indexx < n && visited[indexx + 1]) connect(indexx,indexx + 1);
visited[indexx] = 1;
}
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
24 ms |
2716 KB |
Output is correct |
4 |
Correct |
46 ms |
5016 KB |
Output is correct |
5 |
Correct |
46 ms |
5004 KB |
Output is correct |
6 |
Correct |
47 ms |
5004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
5 ms |
844 KB |
Output is correct |
3 |
Correct |
25 ms |
3052 KB |
Output is correct |
4 |
Correct |
52 ms |
5788 KB |
Output is correct |
5 |
Correct |
55 ms |
5956 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
5 ms |
844 KB |
Output is correct |
4 |
Correct |
25 ms |
3112 KB |
Output is correct |
5 |
Correct |
55 ms |
5780 KB |
Output is correct |
6 |
Correct |
55 ms |
5892 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
4 ms |
844 KB |
Output is correct |
9 |
Correct |
24 ms |
3084 KB |
Output is correct |
10 |
Correct |
37 ms |
5660 KB |
Output is correct |
11 |
Correct |
37 ms |
5828 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
456 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
23 ms |
2680 KB |
Output is correct |
12 |
Correct |
48 ms |
5100 KB |
Output is correct |
13 |
Correct |
46 ms |
5080 KB |
Output is correct |
14 |
Correct |
47 ms |
5060 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
5 ms |
844 KB |
Output is correct |
17 |
Correct |
32 ms |
3048 KB |
Output is correct |
18 |
Correct |
60 ms |
5784 KB |
Output is correct |
19 |
Correct |
55 ms |
5880 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
4 ms |
888 KB |
Output is correct |
22 |
Correct |
25 ms |
3100 KB |
Output is correct |
23 |
Correct |
38 ms |
5636 KB |
Output is correct |
24 |
Correct |
37 ms |
5836 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
5 ms |
896 KB |
Output is correct |
31 |
Correct |
5 ms |
840 KB |
Output is correct |
32 |
Correct |
24 ms |
2952 KB |
Output is correct |
33 |
Correct |
26 ms |
3040 KB |
Output is correct |
34 |
Correct |
50 ms |
5592 KB |
Output is correct |
35 |
Correct |
50 ms |
5572 KB |
Output is correct |
36 |
Correct |
48 ms |
5772 KB |
Output is correct |
37 |
Correct |
48 ms |
5852 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
47 ms |
5876 KB |
Output is correct |
40 |
Correct |
45 ms |
5828 KB |
Output is correct |
41 |
Correct |
41 ms |
5960 KB |
Output is correct |
42 |
Correct |
54 ms |
6488 KB |
Output is correct |