#include <bits/stdc++.h>
using namespace std;
#define int long long
#define double long double
#define FOR(i, l, r, d) for(int i=(l); i<=(r); i+=(d))
#define szof(x) ((int)(x).size())
#define vi vector<int>
#define pii pair<int, int>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mkp make_pair
const int INF = 2147483647;
const int LNF = INF*INF;
const int MOD = 1000000007;
const int mod = 998244353;
const double eps = 1e-12;
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
const int MAX = 100010;
int n;
int h[MAX], w[MAX];
bool vis[MAX];
int p[MAX], dep[MAX];
int wsum[MAX], l[MAX], r[MAX];
int res;
int findp(int v){
if(v == p[v]) return v;
return p[v] = findp(p[v]);
}
void uni(int u, int v){
int pu = findp(u);
int pv = findp(v);
if(pu == pv) return;
if(dep[pu] < dep[pv]) swap(pu, pv);
p[pv] = pu;
if(dep[pu] > dep[pv]) dep[pu]++;
wsum[pu] += wsum[pv];
l[pu] = min(l[pu], l[pv]);
r[pu] = max(r[pu], r[pv]);
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
//freopen("fancyfence_sample.txt", "r", stdin);
cin>>n;
FOR(i, 1, n, 1){
cin>>h[i];
}
FOR(i, 1, n, 1){
cin>>w[i];
}
FOR(i, 1, n, 1){
vis[i] = 0;
p[i] = i;
dep[i] = 0;
wsum[i] = w[i];
l[i] = r[i] = i;
}
vector<pii> owo;
FOR(i, 1, n, 1){
owo.eb(h[i], i);
}
sort(owo.begin(), owo.end());
reverse(owo.begin(), owo.end());
for(pii i : owo){
int id = i.S;
vis[id] = 1;
if(vis[id - 1]) uni(id - 1, id);
if(vis[id + 1]) uni(id, id + 1);
int U = h[id];
int D = max(h[l[findp(id)] - 1], h[r[findp(id)] + 1]) + 1;
int W = wsum[findp(id)] % MOD;
res += (((U + D) * (U - D + 1) / 2) % MOD) * ((W * (W + 1) / 2) % MOD);
res %= MOD;
//cerr<<"U = "<<U<<", D = "<<D<<", res += "<<(((U + D) * (U - D + 1) / 2) % MOD) * ((W * (W + 1) / 2) % MOD)<<endl;
}
cout<<res<<'\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
15 ms |
4876 KB |
Output is correct |
4 |
Correct |
40 ms |
9136 KB |
Output is correct |
5 |
Correct |
30 ms |
9236 KB |
Output is correct |
6 |
Correct |
32 ms |
9156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
3 ms |
1484 KB |
Output is correct |
3 |
Correct |
16 ms |
5324 KB |
Output is correct |
4 |
Correct |
37 ms |
9936 KB |
Output is correct |
5 |
Correct |
35 ms |
10036 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
4 ms |
1484 KB |
Output is correct |
4 |
Correct |
17 ms |
5224 KB |
Output is correct |
5 |
Correct |
35 ms |
9928 KB |
Output is correct |
6 |
Correct |
33 ms |
10088 KB |
Output is correct |
7 |
Correct |
1 ms |
352 KB |
Output is correct |
8 |
Correct |
3 ms |
1428 KB |
Output is correct |
9 |
Correct |
18 ms |
5300 KB |
Output is correct |
10 |
Correct |
41 ms |
9980 KB |
Output is correct |
11 |
Correct |
41 ms |
9928 KB |
Output is correct |
12 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
456 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
0 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
0 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 |
15 ms |
4816 KB |
Output is correct |
12 |
Correct |
28 ms |
9220 KB |
Output is correct |
13 |
Correct |
31 ms |
9216 KB |
Output is correct |
14 |
Correct |
34 ms |
9184 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
3 ms |
1484 KB |
Output is correct |
17 |
Correct |
16 ms |
5212 KB |
Output is correct |
18 |
Correct |
34 ms |
9920 KB |
Output is correct |
19 |
Correct |
36 ms |
10004 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
3 ms |
1464 KB |
Output is correct |
22 |
Correct |
16 ms |
5196 KB |
Output is correct |
23 |
Correct |
29 ms |
9800 KB |
Output is correct |
24 |
Correct |
28 ms |
9904 KB |
Output is correct |
25 |
Correct |
0 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
460 KB |
Output is correct |
28 |
Correct |
1 ms |
460 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
5 ms |
1484 KB |
Output is correct |
31 |
Correct |
5 ms |
1480 KB |
Output is correct |
32 |
Correct |
21 ms |
5176 KB |
Output is correct |
33 |
Correct |
21 ms |
5180 KB |
Output is correct |
34 |
Correct |
45 ms |
9744 KB |
Output is correct |
35 |
Correct |
44 ms |
9720 KB |
Output is correct |
36 |
Correct |
43 ms |
9936 KB |
Output is correct |
37 |
Correct |
62 ms |
9964 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
48 ms |
9976 KB |
Output is correct |
40 |
Correct |
37 ms |
9996 KB |
Output is correct |
41 |
Correct |
33 ms |
9936 KB |
Output is correct |
42 |
Correct |
28 ms |
10172 KB |
Output is correct |