#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF=2e9;
vector<ll> calculate_costs(vector<int> w,vector<int> a,vector<int> b,vector<int> qr){
int n=w.size();
int q=qr.size();
vector<int> ord(n);
iota(ord.begin(),ord.end(),0);
sort(ord.begin(),ord.end(),[&](int i,int j){
return w[i]<w[j];
});
auto work=[&](vector<int> &a){
vector<int> res(n);
for(int i=0;i<n;i++){
res[i]=a[ord[i]];
}
swap(a,res);
};
work(w);
work(a);
work(b);
vector<ll> ans(q);
vector<int> ord2(q);
iota(ord2.begin(),ord2.end(),0);
sort(ord2.begin(),ord2.end(),[&](int i,int j){
return qr[i]<qr[j];
});
vector<int> p(n);
iota(p.begin(),p.end(),0);
function<int(int)> fp=[&](int u){
return p[u]=u==p[u]?u:fp(p[u]);
};
vector<int> sz(n,1),mn(n,INF);
vector<array<int,2>> dp(n,{INF,INF});
for(int i=0;i<n;i++){
dp[i][i%2]=a[i]-b[i];
}
ll base=0;
for(auto x:a){
base+=x;
}
vector<pair<int,int>> event,event2;
for(int i=0;i<n-1;i++){
event.emplace_back(w[i+1]-w[i],i);
}
for(int i=1;i<n-1;i++){
event2.emplace_back(w[i+1]-w[i-1],i);
}
sort(event.rbegin(),event.rend());
sort(event2.rbegin(),event2.rend());
auto calc=[&](int i){
if(sz[i]%2==0){
return 0;
}
return min(mn[i],dp[i][i%2]);
};
for(auto i:ord2){
int d=qr[i];
while(!event.empty()&&event.back().first<=d){
int j=event.back().second;
event.pop_back();
int u=fp(j),v=fp(j+1);
base-=calc(u);
base-=calc(v);
p[v]=u;
mn[u]=min(mn[u],mn[v]);
sz[u]+=sz[v];
dp[u][0]=min(dp[u][0],dp[v][0]);
dp[u][1]=min(dp[u][1],dp[v][1]);
base+=calc(u);
}
while(!event2.empty()&&event2.back().first<=d){
int j=event2.back().second;
event2.pop_back();
int u=fp(j);
base-=calc(u);
mn[u]=min(mn[u],a[j]-b[j]);
base+=calc(u);
}
ans[i]=base;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
9912 KB |
Output is correct |
2 |
Correct |
28 ms |
9916 KB |
Output is correct |
3 |
Correct |
28 ms |
9888 KB |
Output is correct |
4 |
Correct |
28 ms |
9744 KB |
Output is correct |
5 |
Correct |
28 ms |
9912 KB |
Output is correct |
6 |
Correct |
28 ms |
9912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
8372 KB |
Output is correct |
2 |
Correct |
41 ms |
9020 KB |
Output is correct |
3 |
Correct |
45 ms |
8628 KB |
Output is correct |
4 |
Correct |
43 ms |
8636 KB |
Output is correct |
5 |
Correct |
42 ms |
8720 KB |
Output is correct |
6 |
Correct |
49 ms |
8628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
448 KB |
Output is correct |
7 |
Correct |
1 ms |
600 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
448 KB |
Output is correct |
7 |
Correct |
28 ms |
9912 KB |
Output is correct |
8 |
Correct |
28 ms |
9916 KB |
Output is correct |
9 |
Correct |
28 ms |
9888 KB |
Output is correct |
10 |
Correct |
28 ms |
9744 KB |
Output is correct |
11 |
Correct |
28 ms |
9912 KB |
Output is correct |
12 |
Correct |
28 ms |
9912 KB |
Output is correct |
13 |
Correct |
30 ms |
8372 KB |
Output is correct |
14 |
Correct |
41 ms |
9020 KB |
Output is correct |
15 |
Correct |
45 ms |
8628 KB |
Output is correct |
16 |
Correct |
43 ms |
8636 KB |
Output is correct |
17 |
Correct |
42 ms |
8720 KB |
Output is correct |
18 |
Correct |
49 ms |
8628 KB |
Output is correct |
19 |
Correct |
1 ms |
600 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
2 ms |
604 KB |
Output is correct |
25 |
Correct |
37 ms |
9920 KB |
Output is correct |
26 |
Correct |
50 ms |
11040 KB |
Output is correct |
27 |
Correct |
58 ms |
10164 KB |
Output is correct |
28 |
Correct |
54 ms |
10248 KB |
Output is correct |
29 |
Correct |
51 ms |
10016 KB |
Output is correct |
30 |
Correct |
58 ms |
10144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
8372 KB |
Output is correct |
2 |
Correct |
41 ms |
9020 KB |
Output is correct |
3 |
Correct |
45 ms |
8628 KB |
Output is correct |
4 |
Correct |
43 ms |
8636 KB |
Output is correct |
5 |
Correct |
42 ms |
8720 KB |
Output is correct |
6 |
Correct |
49 ms |
8628 KB |
Output is correct |
7 |
Correct |
55 ms |
11040 KB |
Output is correct |
8 |
Correct |
64 ms |
11112 KB |
Output is correct |
9 |
Correct |
71 ms |
11192 KB |
Output is correct |
10 |
Correct |
70 ms |
11304 KB |
Output is correct |
11 |
Correct |
69 ms |
11192 KB |
Output is correct |
12 |
Correct |
64 ms |
11172 KB |
Output is correct |
13 |
Correct |
66 ms |
11188 KB |
Output is correct |
14 |
Correct |
57 ms |
10884 KB |
Output is correct |
15 |
Correct |
68 ms |
11188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
448 KB |
Output is correct |
8 |
Correct |
28 ms |
9912 KB |
Output is correct |
9 |
Correct |
28 ms |
9916 KB |
Output is correct |
10 |
Correct |
28 ms |
9888 KB |
Output is correct |
11 |
Correct |
28 ms |
9744 KB |
Output is correct |
12 |
Correct |
28 ms |
9912 KB |
Output is correct |
13 |
Correct |
28 ms |
9912 KB |
Output is correct |
14 |
Correct |
30 ms |
8372 KB |
Output is correct |
15 |
Correct |
41 ms |
9020 KB |
Output is correct |
16 |
Correct |
45 ms |
8628 KB |
Output is correct |
17 |
Correct |
43 ms |
8636 KB |
Output is correct |
18 |
Correct |
42 ms |
8720 KB |
Output is correct |
19 |
Correct |
49 ms |
8628 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
2 ms |
604 KB |
Output is correct |
26 |
Correct |
37 ms |
9920 KB |
Output is correct |
27 |
Correct |
50 ms |
11040 KB |
Output is correct |
28 |
Correct |
58 ms |
10164 KB |
Output is correct |
29 |
Correct |
54 ms |
10248 KB |
Output is correct |
30 |
Correct |
51 ms |
10016 KB |
Output is correct |
31 |
Correct |
58 ms |
10144 KB |
Output is correct |
32 |
Correct |
55 ms |
11040 KB |
Output is correct |
33 |
Correct |
64 ms |
11112 KB |
Output is correct |
34 |
Correct |
71 ms |
11192 KB |
Output is correct |
35 |
Correct |
70 ms |
11304 KB |
Output is correct |
36 |
Correct |
69 ms |
11192 KB |
Output is correct |
37 |
Correct |
64 ms |
11172 KB |
Output is correct |
38 |
Correct |
66 ms |
11188 KB |
Output is correct |
39 |
Correct |
57 ms |
10884 KB |
Output is correct |
40 |
Correct |
68 ms |
11188 KB |
Output is correct |
41 |
Correct |
58 ms |
12448 KB |
Output is correct |
42 |
Correct |
68 ms |
12684 KB |
Output is correct |
43 |
Correct |
74 ms |
12620 KB |
Output is correct |
44 |
Correct |
72 ms |
12836 KB |
Output is correct |
45 |
Correct |
73 ms |
12824 KB |
Output is correct |
46 |
Correct |
72 ms |
12708 KB |
Output is correct |
47 |
Correct |
72 ms |
12724 KB |
Output is correct |
48 |
Correct |
65 ms |
12468 KB |
Output is correct |
49 |
Correct |
77 ms |
12728 KB |
Output is correct |