#include "bits/stdc++.h"
#include "nile.h"
using namespace std;
const int INF = 1e9;
struct DSU{
vector <int> e;
vector <int> c;
vector <int> mni;
vector<array<int, 3>> mnc;
long long tot = 0;
DSU(int n, vector<int> c) : e(n ,-1), c(c), mnc(n), mni(n) {
for(int i = 0; i < n; i++){
mni[i] = i;
mnc[i] = {INF, INF, INF};
mnc[i][i % 2] = c[i];
tot += c[i];
}
}
int find(int x) { return e[x]<0? x : e[x]=find(e[x]); }
int size(int x) { return -e[find(x)]; }
int cost(int x) {
int idx = mnc[x][mni[x] % 2] < mnc[x][2]? (mni[x] % 2) : 2;
return mnc[x][idx] * (size(x) % 2);
}
bool join(int i ,int j){
int x = find(i) ,y = find(j);
if(x == y){
tot -= cost(x);
mnc[x][2] = min(mnc[x][2], c[(i + j) / 2]);
tot += cost(x);
return false;
}
tot -= cost(x);
tot -= cost(y);
if(e[x] > e[y]) swap(x ,y);
e[x] += e[y]; e[y] = x;
for(int p = 0; p < 3; p++)
mnc[x][p] = min(mnc[x][p], mnc[y][p]);
mni[x] = min(mni[x], mni[y]);
tot += cost(x);
return true;
}
};
vector<long long> calculate_costs(vector<int> W, vector<int> A, vector<int> B, vector<int> E) {
int n = W.size();
int q = E.size();
vector<int> ordW(n);
iota(ordW.begin() ,ordW.end() ,0);
sort(ordW.begin() ,ordW.end() ,[&](int i ,int j){
return W[i] < W[j];
});
vector <int> ordE(q);
iota(ordE.begin() ,ordE.end() ,0);
sort(ordE.begin() ,ordE.end() ,[&](int i ,int j){
return E[i] < E[j];
});
vector<int> w(n), c(n);
for(int i = 0; i < n; i++){
int j = ordW[i];
w[i] = W[j];
c[i] = A[j] - B[j];
}
vector<array<int, 3>> edges;
for(int i = 0; i < n; i++){
if(i+1 < n) edges.push_back({w[i+1] - w[i], i, i+1});
if(i+2 < n) edges.push_back({w[i+2] - w[i], i, i+2});
}
sort(edges.rbegin(), edges.rend());
DSU d(n, c);
vector<long long> R(q, accumulate(B.begin(), B.end(), 0LL));
for(int i = 0; i < q; i++){
while(!edges.empty() && edges.back()[0] <= E[ordE[i]]){
auto [_, x, y] = edges.back(); edges.pop_back();
d.join(x, y);
}
R[ordE[i]] += d.tot;
}
return R;
}
Compilation message
nile.cpp: In constructor 'DSU::DSU(int, std::vector<int>)':
nile.cpp:11:27: warning: 'DSU::mnc' will be initialized after [-Wreorder]
11 | vector<array<int, 3>> mnc;
| ^~~
nile.cpp:10:18: warning: 'std::vector<int> DSU::mni' [-Wreorder]
10 | vector <int> mni;
| ^~~
nile.cpp:13:5: warning: when initialized here [-Wreorder]
13 | DSU(int n, vector<int> c) : e(n ,-1), c(c), mnc(n), mni(n) {
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
11304 KB |
Output is correct |
2 |
Correct |
47 ms |
11456 KB |
Output is correct |
3 |
Correct |
46 ms |
11452 KB |
Output is correct |
4 |
Correct |
45 ms |
11196 KB |
Output is correct |
5 |
Correct |
45 ms |
11452 KB |
Output is correct |
6 |
Correct |
48 ms |
11388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
9924 KB |
Output is correct |
2 |
Correct |
46 ms |
10436 KB |
Output is correct |
3 |
Correct |
58 ms |
10172 KB |
Output is correct |
4 |
Correct |
57 ms |
10172 KB |
Output is correct |
5 |
Correct |
57 ms |
10176 KB |
Output is correct |
6 |
Correct |
60 ms |
10136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
592 KB |
Output is correct |
8 |
Correct |
2 ms |
592 KB |
Output is correct |
9 |
Correct |
2 ms |
592 KB |
Output is correct |
10 |
Correct |
2 ms |
592 KB |
Output is correct |
11 |
Correct |
2 ms |
592 KB |
Output is correct |
12 |
Correct |
2 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
592 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
1 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
592 KB |
Output is correct |
7 |
Correct |
56 ms |
11304 KB |
Output is correct |
8 |
Correct |
47 ms |
11456 KB |
Output is correct |
9 |
Correct |
46 ms |
11452 KB |
Output is correct |
10 |
Correct |
45 ms |
11196 KB |
Output is correct |
11 |
Correct |
45 ms |
11452 KB |
Output is correct |
12 |
Correct |
48 ms |
11388 KB |
Output is correct |
13 |
Correct |
51 ms |
9924 KB |
Output is correct |
14 |
Correct |
46 ms |
10436 KB |
Output is correct |
15 |
Correct |
58 ms |
10172 KB |
Output is correct |
16 |
Correct |
57 ms |
10172 KB |
Output is correct |
17 |
Correct |
57 ms |
10176 KB |
Output is correct |
18 |
Correct |
60 ms |
10136 KB |
Output is correct |
19 |
Correct |
2 ms |
592 KB |
Output is correct |
20 |
Correct |
2 ms |
592 KB |
Output is correct |
21 |
Correct |
2 ms |
592 KB |
Output is correct |
22 |
Correct |
2 ms |
592 KB |
Output is correct |
23 |
Correct |
2 ms |
592 KB |
Output is correct |
24 |
Correct |
2 ms |
592 KB |
Output is correct |
25 |
Correct |
63 ms |
11708 KB |
Output is correct |
26 |
Correct |
52 ms |
11892 KB |
Output is correct |
27 |
Correct |
59 ms |
11712 KB |
Output is correct |
28 |
Correct |
63 ms |
10776 KB |
Output is correct |
29 |
Correct |
64 ms |
11448 KB |
Output is correct |
30 |
Correct |
69 ms |
11708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
9924 KB |
Output is correct |
2 |
Correct |
46 ms |
10436 KB |
Output is correct |
3 |
Correct |
58 ms |
10172 KB |
Output is correct |
4 |
Correct |
57 ms |
10172 KB |
Output is correct |
5 |
Correct |
57 ms |
10176 KB |
Output is correct |
6 |
Correct |
60 ms |
10136 KB |
Output is correct |
7 |
Correct |
77 ms |
12640 KB |
Output is correct |
8 |
Correct |
69 ms |
12732 KB |
Output is correct |
9 |
Correct |
82 ms |
12732 KB |
Output is correct |
10 |
Correct |
82 ms |
12868 KB |
Output is correct |
11 |
Correct |
78 ms |
12732 KB |
Output is correct |
12 |
Correct |
80 ms |
11192 KB |
Output is correct |
13 |
Correct |
82 ms |
12848 KB |
Output is correct |
14 |
Correct |
75 ms |
12476 KB |
Output is correct |
15 |
Correct |
95 ms |
12724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
2 ms |
592 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
1 ms |
592 KB |
Output is correct |
7 |
Correct |
2 ms |
592 KB |
Output is correct |
8 |
Correct |
56 ms |
11304 KB |
Output is correct |
9 |
Correct |
47 ms |
11456 KB |
Output is correct |
10 |
Correct |
46 ms |
11452 KB |
Output is correct |
11 |
Correct |
45 ms |
11196 KB |
Output is correct |
12 |
Correct |
45 ms |
11452 KB |
Output is correct |
13 |
Correct |
48 ms |
11388 KB |
Output is correct |
14 |
Correct |
51 ms |
9924 KB |
Output is correct |
15 |
Correct |
46 ms |
10436 KB |
Output is correct |
16 |
Correct |
58 ms |
10172 KB |
Output is correct |
17 |
Correct |
57 ms |
10172 KB |
Output is correct |
18 |
Correct |
57 ms |
10176 KB |
Output is correct |
19 |
Correct |
60 ms |
10136 KB |
Output is correct |
20 |
Correct |
2 ms |
592 KB |
Output is correct |
21 |
Correct |
2 ms |
592 KB |
Output is correct |
22 |
Correct |
2 ms |
592 KB |
Output is correct |
23 |
Correct |
2 ms |
592 KB |
Output is correct |
24 |
Correct |
2 ms |
592 KB |
Output is correct |
25 |
Correct |
2 ms |
592 KB |
Output is correct |
26 |
Correct |
63 ms |
11708 KB |
Output is correct |
27 |
Correct |
52 ms |
11892 KB |
Output is correct |
28 |
Correct |
59 ms |
11712 KB |
Output is correct |
29 |
Correct |
63 ms |
10776 KB |
Output is correct |
30 |
Correct |
64 ms |
11448 KB |
Output is correct |
31 |
Correct |
69 ms |
11708 KB |
Output is correct |
32 |
Correct |
77 ms |
12640 KB |
Output is correct |
33 |
Correct |
69 ms |
12732 KB |
Output is correct |
34 |
Correct |
82 ms |
12732 KB |
Output is correct |
35 |
Correct |
82 ms |
12868 KB |
Output is correct |
36 |
Correct |
78 ms |
12732 KB |
Output is correct |
37 |
Correct |
80 ms |
11192 KB |
Output is correct |
38 |
Correct |
82 ms |
12848 KB |
Output is correct |
39 |
Correct |
75 ms |
12476 KB |
Output is correct |
40 |
Correct |
95 ms |
12724 KB |
Output is correct |
41 |
Correct |
76 ms |
13992 KB |
Output is correct |
42 |
Correct |
82 ms |
12940 KB |
Output is correct |
43 |
Correct |
91 ms |
14272 KB |
Output is correct |
44 |
Correct |
91 ms |
14268 KB |
Output is correct |
45 |
Correct |
87 ms |
14380 KB |
Output is correct |
46 |
Correct |
94 ms |
14380 KB |
Output is correct |
47 |
Correct |
95 ms |
14248 KB |
Output is correct |
48 |
Correct |
79 ms |
14120 KB |
Output is correct |
49 |
Correct |
97 ms |
14528 KB |
Output is correct |