#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int MOD = 1000000007;
#define lsb(x) ((x) & (-x))
int n;
const int MAXN = 100005;
int fw[MAXN];
void update(int X, int V){
X++;
while(X<=n){
fw[X] += V;
X += lsb(X);
}
}
int query(int R){
R++;
if(R==0) return 0;
int result = 0;
while(R){
result += fw[R];
R -= lsb(R);
}
return result;
}
inline int goat(int n, int m){
if(n <= 0 || m <= 0) return 0;
int x = (n%MOD)*((n+1)%MOD);
x >>= 1;
x %= MOD;
int y = (m%MOD)*((m+1)%MOD);
y >>= 1;
y %= MOD;
return (x*y)%MOD;
}
signed main(){
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> n;
vector<int> h(n), w(n), wp(n);
vector<pii> hs;
hs.reserve(n);
for(int i=0; i<n; i++){
cin >> h[i];
hs.emplace_back(h[i], i);
}
for(int i=0; i<n; i++){
cin >> w[i];
if(i==0) wp[0] = w[0];
else wp[i] = wp[i-1]+w[i];
}
sort(hs.begin(), hs.end());
set<int> dead;
dead.insert(-1);
dead.insert(n);
int ans=0;
for(int i=0; i<n; i++){
auto[ch, cs] = hs[i];
int cph = query(cs);
if(cph == ch){
dead.insert(cs);
continue;
}
auto pr = dead.upper_bound(cs);
auto pl = pr;
pl--;
int cfr = (*pr)-1, cfl = (*pl)+1;
int cfw = wp[cfr];
if(cfl > 0) cfw -= wp[cfl-1];
int uh = ch-cph;
ans += goat(ch, cfw);
ans -= goat(cph, cfw);
update(cfl, uh);
update(cfr+1, -uh);
dead.insert(cs);
ans += MOD;
ans %= MOD;
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
18 ms |
4956 KB |
Output is correct |
4 |
Correct |
37 ms |
9776 KB |
Output is correct |
5 |
Correct |
35 ms |
9720 KB |
Output is correct |
6 |
Correct |
34 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
3 ms |
1372 KB |
Output is correct |
3 |
Correct |
17 ms |
5408 KB |
Output is correct |
4 |
Correct |
34 ms |
10836 KB |
Output is correct |
5 |
Correct |
37 ms |
11088 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
3 ms |
1372 KB |
Output is correct |
4 |
Correct |
17 ms |
5600 KB |
Output is correct |
5 |
Correct |
33 ms |
10840 KB |
Output is correct |
6 |
Correct |
33 ms |
11088 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
4 ms |
1372 KB |
Output is correct |
9 |
Correct |
18 ms |
5748 KB |
Output is correct |
10 |
Correct |
42 ms |
11600 KB |
Output is correct |
11 |
Correct |
42 ms |
11584 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
448 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
476 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
528 KB |
Output is correct |
15 |
Correct |
1 ms |
476 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
392 KB |
Output is correct |
9 |
Correct |
0 ms |
600 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
18 ms |
5724 KB |
Output is correct |
12 |
Correct |
34 ms |
10844 KB |
Output is correct |
13 |
Correct |
36 ms |
10936 KB |
Output is correct |
14 |
Correct |
34 ms |
10836 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
3 ms |
1372 KB |
Output is correct |
17 |
Correct |
16 ms |
5724 KB |
Output is correct |
18 |
Correct |
33 ms |
10856 KB |
Output is correct |
19 |
Correct |
36 ms |
11160 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
4 ms |
1388 KB |
Output is correct |
22 |
Correct |
18 ms |
5724 KB |
Output is correct |
23 |
Correct |
40 ms |
11452 KB |
Output is correct |
24 |
Correct |
41 ms |
11600 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
6 ms |
1368 KB |
Output is correct |
31 |
Correct |
7 ms |
1628 KB |
Output is correct |
32 |
Correct |
28 ms |
6008 KB |
Output is correct |
33 |
Correct |
30 ms |
5972 KB |
Output is correct |
34 |
Correct |
65 ms |
11348 KB |
Output is correct |
35 |
Correct |
55 ms |
11604 KB |
Output is correct |
36 |
Correct |
65 ms |
11784 KB |
Output is correct |
37 |
Correct |
65 ms |
11700 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
62 ms |
11808 KB |
Output is correct |
40 |
Correct |
60 ms |
11604 KB |
Output is correct |
41 |
Correct |
46 ms |
11792 KB |
Output is correct |
42 |
Correct |
42 ms |
11604 KB |
Output is correct |