#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 int mod = 1000000007;
const int inf = 1e9 + 5;
const int mxN = 200005;
int n;
array<int,3> niz[mxN];
bool bio[mxN];
int pom[mxN];
int add(int a, int b){ a += b; if(a >= mod)a -= mod; return a; }
int sub(int a, int b){ a -= b; if(a < 0)a += mod; return a; }
int mul(int a, int b){ return (1ll * a * b) % mod; }
int power(int a, int b){
if(!b)return 1;
int p = power(a, b / 2);
p = mul(p, p);
if(b & 1)p = mul(p, a);
return p;
}
int inv(int a){
return power(a, mod - 2);
}
int 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;
par[y] = x; len[x] = add(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;
}
}
int sum(int a){
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; int 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]);
int kol = mul(sum(d), sub(sum(h), sum(mx)));
rez = add(rez, kol);
}
i = j;
}
cout << rez << '\n';
return 0;
}
/*
// probati bojenje sahovski
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
23 ms |
2548 KB |
Output is correct |
4 |
Correct |
38 ms |
4324 KB |
Output is correct |
5 |
Correct |
46 ms |
4596 KB |
Output is correct |
6 |
Correct |
46 ms |
3976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
5 ms |
724 KB |
Output is correct |
3 |
Correct |
17 ms |
1932 KB |
Output is correct |
4 |
Correct |
37 ms |
3576 KB |
Output is correct |
5 |
Correct |
49 ms |
3484 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
4 ms |
596 KB |
Output is correct |
4 |
Correct |
19 ms |
1936 KB |
Output is correct |
5 |
Correct |
36 ms |
3576 KB |
Output is correct |
6 |
Correct |
39 ms |
3608 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
5 ms |
596 KB |
Output is correct |
9 |
Correct |
25 ms |
1960 KB |
Output is correct |
10 |
Correct |
52 ms |
3560 KB |
Output is correct |
11 |
Correct |
52 ms |
3556 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
0 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 |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
24 ms |
2444 KB |
Output is correct |
12 |
Correct |
39 ms |
4324 KB |
Output is correct |
13 |
Correct |
45 ms |
4700 KB |
Output is correct |
14 |
Correct |
41 ms |
3956 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
17 ms |
1984 KB |
Output is correct |
18 |
Correct |
35 ms |
3572 KB |
Output is correct |
19 |
Correct |
37 ms |
3512 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
6 ms |
596 KB |
Output is correct |
22 |
Correct |
21 ms |
1972 KB |
Output is correct |
23 |
Correct |
49 ms |
3556 KB |
Output is correct |
24 |
Correct |
52 ms |
3472 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
6 ms |
596 KB |
Output is correct |
31 |
Correct |
6 ms |
692 KB |
Output is correct |
32 |
Correct |
33 ms |
1932 KB |
Output is correct |
33 |
Correct |
29 ms |
1968 KB |
Output is correct |
34 |
Correct |
60 ms |
3464 KB |
Output is correct |
35 |
Correct |
69 ms |
3724 KB |
Output is correct |
36 |
Correct |
67 ms |
3468 KB |
Output is correct |
37 |
Correct |
66 ms |
3572 KB |
Output is correct |
38 |
Correct |
0 ms |
340 KB |
Output is correct |
39 |
Correct |
61 ms |
3804 KB |
Output is correct |
40 |
Correct |
55 ms |
3580 KB |
Output is correct |
41 |
Correct |
51 ms |
3668 KB |
Output is correct |
42 |
Correct |
49 ms |
4132 KB |
Output is correct |