#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ldb;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ldb,ldb> pdd;
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define fb(i,b,a) for(int i = b; i >= a; i--)
#define trav(a,x) for(auto& a : x)
#define sz(a) (int)(a).size()
#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
const ll mod = 1000000007;
const int inf = 1e9 + 5;
const int mxN = 200005;
int n;
array<int,3> niz[mxN];
bool bio[mxN];
int pom[mxN];
ll add(ll a, ll b){ a += b; if(a >= mod)a -= mod; return a; }
ll sub(ll a, ll b){ a -= b; if(a < 0)a += mod; return a; }
ll mul(ll a, ll b){ return (1ll * a * b) % mod; }
ll power(ll a, ll b){
if(!b)return 1;
ll p = power(a, b / 2);
p = mul(p, p);
if(b & 1)p = mul(p, a);
return p;
}
ll inv(int a){
return power(a, mod - 2);
}
ll len[mxN];
int par[mxN];
int L[mxN], R[mxN];
int findpar(int x){
return (x == par[x] ? x : par[x] = findpar(par[x]));
}
void unite(int x, int y){
x = findpar(x), y = findpar(y);
if(x == y)return;
if(len[x] < len[y])swap(x, y);
par[y] = x; len[x] += len[y];
L[x] = min(L[x], L[y]); R[x] = max(R[x], R[y]);
}
void init(){
ff(i,1,n){
par[i] = i;
len[i] = niz[i][1];
L[i] = i;
R[i] = i;
}
}
ll sum(ll a){
a %= mod;
return mul(mul(a, a + 1), inv(2));
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
ff(i,1,n)cin >> niz[i][0], pom[i] = niz[i][0];
ff(i,1,n)cin >> niz[i][1], niz[i][2] = i;
init();
sort(niz + 1, niz + n + 1);
int j = n; ll rez = 0;
fb(i,n,1){
int h = niz[i][0];
bio[niz[i][2]] = 1;
while(j - 1 >= 1 && niz[j - 1][0] == h){
j -= 1; bio[niz[j][2]] = 1;
}
ff(l,j,i){
int X = niz[l][2];
if(X > 1 && bio[X - 1] == 1)unite(X - 1, X);
if(X < n && bio[X + 1] == 1)unite(X, X + 1);
}
set<int> s;
ff(l,j,i)s.insert(findpar(niz[l][2]));
for(auto c : s){
int l = L[c];
int r = R[c];
ll d = len[c];
int mx = max(pom[l - 1], pom[r + 1]);
ll D = sum(d) % mod;
ll H = sum(h) % mod;
ll M = sum(mx) % mod;
ll kol = mul(D, sub(H, M));
rez = add(rez, kol);
}
i = j;
}
cout << rez << '\n';
return 0;
}
/*
// probati bojenje sahovski
*/
# |
결과 |
실행 시간 |
메모리 |
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 |
1 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 |
24 ms |
3244 KB |
Output is correct |
4 |
Correct |
42 ms |
5852 KB |
Output is correct |
5 |
Correct |
50 ms |
6144 KB |
Output is correct |
6 |
Correct |
48 ms |
5472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
856 KB |
Output is correct |
3 |
Correct |
27 ms |
3028 KB |
Output is correct |
4 |
Correct |
53 ms |
5908 KB |
Output is correct |
5 |
Correct |
47 ms |
5900 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 |
6 ms |
856 KB |
Output is correct |
4 |
Correct |
20 ms |
3084 KB |
Output is correct |
5 |
Correct |
39 ms |
5848 KB |
Output is correct |
6 |
Correct |
61 ms |
5964 KB |
Output is correct |
7 |
Correct |
2 ms |
344 KB |
Output is correct |
8 |
Correct |
8 ms |
852 KB |
Output is correct |
9 |
Correct |
41 ms |
3136 KB |
Output is correct |
10 |
Correct |
57 ms |
5680 KB |
Output is correct |
11 |
Correct |
61 ms |
5836 KB |
Output is correct |
12 |
Correct |
1 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 |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
2 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 |
380 KB |
Output is correct |
15 |
Correct |
2 ms |
344 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 |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
352 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
31 ms |
3292 KB |
Output is correct |
12 |
Correct |
50 ms |
5880 KB |
Output is correct |
13 |
Correct |
55 ms |
6272 KB |
Output is correct |
14 |
Correct |
60 ms |
5536 KB |
Output is correct |
15 |
Correct |
2 ms |
380 KB |
Output is correct |
16 |
Correct |
6 ms |
856 KB |
Output is correct |
17 |
Correct |
19 ms |
3088 KB |
Output is correct |
18 |
Correct |
46 ms |
5860 KB |
Output is correct |
19 |
Correct |
59 ms |
5928 KB |
Output is correct |
20 |
Correct |
1 ms |
416 KB |
Output is correct |
21 |
Correct |
7 ms |
912 KB |
Output is correct |
22 |
Correct |
30 ms |
3120 KB |
Output is correct |
23 |
Correct |
54 ms |
5744 KB |
Output is correct |
24 |
Correct |
69 ms |
5872 KB |
Output is correct |
25 |
Correct |
1 ms |
328 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
360 KB |
Output is correct |
29 |
Correct |
2 ms |
348 KB |
Output is correct |
30 |
Correct |
9 ms |
920 KB |
Output is correct |
31 |
Correct |
7 ms |
860 KB |
Output is correct |
32 |
Correct |
41 ms |
3024 KB |
Output is correct |
33 |
Correct |
37 ms |
3112 KB |
Output is correct |
34 |
Correct |
70 ms |
5704 KB |
Output is correct |
35 |
Correct |
89 ms |
5764 KB |
Output is correct |
36 |
Correct |
74 ms |
5900 KB |
Output is correct |
37 |
Correct |
79 ms |
5900 KB |
Output is correct |
38 |
Correct |
1 ms |
328 KB |
Output is correct |
39 |
Correct |
71 ms |
6140 KB |
Output is correct |
40 |
Correct |
66 ms |
5848 KB |
Output is correct |
41 |
Correct |
66 ms |
5804 KB |
Output is correct |
42 |
Correct |
66 ms |
5852 KB |
Output is correct |