#include <bits/stdc++.h>
#define int long long
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define kill(x) cout << x << "\n", exit(0);
#define pii pair<int, int>
#define pll pair<long long, long long>
#define endl "\n"
using namespace std;
typedef long long ll;
// typedef __int128_t lll;
typedef long double ld;
const int MAXN = (int)1e6 + 7;
const int MOD = (int)1e9 + 7;
const ll INF = (ll)1e9 + 7;
int n, m, k, tmp, t, tmp2, tmp3, tmp4, u, v, w, flag, q, ans, N, X, Y, inv2;
pair<int, pii> arr[MAXN];
int par[MAXN], sz[MAXN];
bool seen[MAXN];
int POW(int x, int y) {
if (y == 0) return 1;
if (y&1) return x*POW(x*x%MOD, y/2)%MOD;
return POW(x*x%MOD, y/2);
}
inline int get_par(int v) { return (par[v] == v? v : par[v] = get_par(par[v])); }
inline int f(int n) { return n%MOD*(n+1)%MOD*inv2%MOD; }
inline void merge(int u, int v) {
u = get_par(u); v = get_par(v);
if (u == v) return;
flag = (flag-(f(sz[u])+f(sz[v]))+10*MOD)%MOD;
par[u] = v; sz[v] = (sz[v]+sz[u])%MOD;
flag = (flag+f(sz[v]))%MOD;
}
void add(int ind) {
par[arr[ind].S.S] = arr[ind].S.S; sz[arr[ind].S.S] = arr[ind].S.F;
seen[arr[ind].S.S] = 1; flag = (flag+f(sz[arr[ind].S.S]))%MOD;
if (seen[arr[ind].S.S-1]) merge(arr[ind].S.S, arr[ind].S.S-1);
if (seen[arr[ind].S.S+1]) merge(arr[ind].S.S, arr[ind].S.S+1);
}
int32_t main() {
#ifdef LOCAL
freopen("inp2.txt", "r", stdin);
freopen("res.out", "w", stdout);
#else
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
inv2 = POW(2, MOD-2);
cin >> n;
for (int i=1; i<=n; i++) cin >> arr[i].F;
for (int i=1; i<=n; i++) cin >> arr[i].S.F;
for (int i=1; i<=n; i++) arr[i].S.S = i;
sort(arr+1, arr+n+1, greater<pair<int, pii>>());
ans = 0;
for (int i=1; i<=n;) {
int j = i;
while (j <= n && arr[j].F == arr[i].F) add(j++);
// cout << "! " << i << " " << j << " " << flag << " " << arr[i].F << endl;
tmp = (f(arr[i].F)-f(arr[j].F)+10*MOD)%MOD;
tmp = (tmp*flag)%MOD; ans = (ans+tmp)%MOD;
i = j;
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6488 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
12 ms |
7248 KB |
Output is correct |
4 |
Correct |
23 ms |
9820 KB |
Output is correct |
5 |
Correct |
29 ms |
9808 KB |
Output is correct |
6 |
Correct |
22 ms |
9820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
3 ms |
6724 KB |
Output is correct |
3 |
Correct |
12 ms |
7492 KB |
Output is correct |
4 |
Correct |
25 ms |
10588 KB |
Output is correct |
5 |
Correct |
27 ms |
10692 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
3 ms |
6748 KB |
Output is correct |
4 |
Correct |
12 ms |
7492 KB |
Output is correct |
5 |
Correct |
25 ms |
10588 KB |
Output is correct |
6 |
Correct |
26 ms |
10704 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
2 ms |
6748 KB |
Output is correct |
9 |
Correct |
12 ms |
7500 KB |
Output is correct |
10 |
Correct |
19 ms |
10560 KB |
Output is correct |
11 |
Correct |
18 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
12 ms |
7004 KB |
Output is correct |
12 |
Correct |
25 ms |
9800 KB |
Output is correct |
13 |
Correct |
24 ms |
9820 KB |
Output is correct |
14 |
Correct |
22 ms |
9932 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
3 ms |
6808 KB |
Output is correct |
17 |
Correct |
13 ms |
7620 KB |
Output is correct |
18 |
Correct |
28 ms |
10604 KB |
Output is correct |
19 |
Correct |
26 ms |
10700 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
3 ms |
6748 KB |
Output is correct |
22 |
Correct |
13 ms |
7616 KB |
Output is correct |
23 |
Correct |
26 ms |
10324 KB |
Output is correct |
24 |
Correct |
21 ms |
10580 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6620 KB |
Output is correct |
30 |
Correct |
3 ms |
6748 KB |
Output is correct |
31 |
Correct |
3 ms |
6904 KB |
Output is correct |
32 |
Correct |
13 ms |
7360 KB |
Output is correct |
33 |
Correct |
13 ms |
7492 KB |
Output is correct |
34 |
Correct |
24 ms |
10504 KB |
Output is correct |
35 |
Correct |
25 ms |
10332 KB |
Output is correct |
36 |
Correct |
27 ms |
10688 KB |
Output is correct |
37 |
Correct |
26 ms |
10580 KB |
Output is correct |
38 |
Correct |
1 ms |
6492 KB |
Output is correct |
39 |
Correct |
25 ms |
10692 KB |
Output is correct |
40 |
Correct |
23 ms |
10700 KB |
Output is correct |
41 |
Correct |
21 ms |
10552 KB |
Output is correct |
42 |
Correct |
23 ms |
10624 KB |
Output is correct |