#include "railroad.h"
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pi;
typedef long long lint;
struct disj{
int pa[400005];
void init(int n){
for(int i=0; i<=n; i++) pa[i] = i;
}
int find(int x){
return pa[x] = (pa[x] == x ? x : find(pa[x]));
}
bool uni(int p, int q){
p = find(p);
q = find(q);
if(p == q) return 0;
pa[q] = p; return 1;
}
}disj;
struct edg{
int s, e, x;
bool operator<(const edg &e)const{
return x < e.x;
}
};
int dx[400005];
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
vector<int> crd;
int n = (int) s.size();
for(int i=0; i<n; i++){
crd.push_back(s[i]);
crd.push_back(t[i]);
}
sort(crd.begin(), crd.end());
crd.resize(unique(crd.begin(), crd.end()) - crd.begin());
disj.init(crd.size());
for(int i=0; i<n; i++){
s[i] = lower_bound(crd.begin(), crd.end(), s[i]) - crd.begin();
t[i] = lower_bound(crd.begin(), crd.end(), t[i]) - crd.begin();
dx[s[i]]++;
dx[t[i]]--;
disj.uni(s[i], t[i]);
}
int cur = 1;
lint ans = 0;
vector<edg> v;
for(int i=crd.size() - 1; i>=0; i--){
cur += dx[i];
if(cur < 0){
ans += -1ll * cur * (crd[i] - crd[i-1]);
dx[i-1] += cur;
disj.uni(i-1, i);
cur = 0;
}
if(cur > 0 && i-1 < crd.size()) disj.uni(i, i-1);
else if(cur == 0) v.push_back({i, i-1, crd[i] - crd[i-1]});
}
sort(v.begin(), v.end());
for(auto &i : v){
if(disj.uni(i.s, i.e)) ans += i.x;
}
return ans;
}
Compilation message
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:60:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(cur > 0 && i-1 < crd.size()) disj.uni(i, i-1);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
5156 KB |
n = 2 |
2 |
Correct |
0 ms |
5156 KB |
n = 2 |
3 |
Correct |
0 ms |
5156 KB |
n = 2 |
4 |
Correct |
0 ms |
5156 KB |
n = 2 |
5 |
Correct |
0 ms |
5156 KB |
n = 2 |
6 |
Correct |
0 ms |
5156 KB |
n = 2 |
7 |
Correct |
0 ms |
5156 KB |
n = 3 |
8 |
Correct |
0 ms |
5156 KB |
n = 3 |
9 |
Correct |
0 ms |
5156 KB |
n = 3 |
10 |
Correct |
0 ms |
5156 KB |
n = 8 |
11 |
Correct |
0 ms |
5156 KB |
n = 8 |
12 |
Correct |
0 ms |
5156 KB |
n = 8 |
13 |
Correct |
0 ms |
5156 KB |
n = 8 |
14 |
Correct |
0 ms |
5156 KB |
n = 8 |
15 |
Correct |
0 ms |
5156 KB |
n = 8 |
16 |
Correct |
0 ms |
5156 KB |
n = 8 |
17 |
Correct |
0 ms |
5156 KB |
n = 8 |
18 |
Correct |
0 ms |
5156 KB |
n = 8 |
19 |
Correct |
0 ms |
5156 KB |
n = 3 |
20 |
Correct |
0 ms |
5156 KB |
n = 7 |
21 |
Correct |
0 ms |
5156 KB |
n = 8 |
22 |
Correct |
0 ms |
5156 KB |
n = 8 |
23 |
Correct |
0 ms |
5156 KB |
n = 8 |
24 |
Correct |
0 ms |
5156 KB |
n = 8 |
25 |
Correct |
0 ms |
5156 KB |
n = 8 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
5156 KB |
n = 2 |
2 |
Correct |
0 ms |
5156 KB |
n = 2 |
3 |
Correct |
0 ms |
5156 KB |
n = 2 |
4 |
Correct |
0 ms |
5156 KB |
n = 2 |
5 |
Correct |
0 ms |
5156 KB |
n = 2 |
6 |
Correct |
0 ms |
5156 KB |
n = 2 |
7 |
Correct |
0 ms |
5156 KB |
n = 3 |
8 |
Correct |
0 ms |
5156 KB |
n = 3 |
9 |
Correct |
0 ms |
5156 KB |
n = 3 |
10 |
Correct |
0 ms |
5156 KB |
n = 8 |
11 |
Correct |
0 ms |
5156 KB |
n = 8 |
12 |
Correct |
0 ms |
5156 KB |
n = 8 |
13 |
Correct |
0 ms |
5156 KB |
n = 8 |
14 |
Correct |
0 ms |
5156 KB |
n = 8 |
15 |
Correct |
0 ms |
5156 KB |
n = 8 |
16 |
Correct |
0 ms |
5156 KB |
n = 8 |
17 |
Correct |
0 ms |
5156 KB |
n = 8 |
18 |
Correct |
0 ms |
5156 KB |
n = 8 |
19 |
Correct |
0 ms |
5156 KB |
n = 3 |
20 |
Correct |
0 ms |
5156 KB |
n = 7 |
21 |
Correct |
0 ms |
5156 KB |
n = 8 |
22 |
Correct |
0 ms |
5156 KB |
n = 8 |
23 |
Correct |
0 ms |
5156 KB |
n = 8 |
24 |
Correct |
0 ms |
5156 KB |
n = 8 |
25 |
Correct |
0 ms |
5156 KB |
n = 8 |
26 |
Correct |
0 ms |
5156 KB |
n = 8 |
27 |
Correct |
0 ms |
5156 KB |
n = 8 |
28 |
Correct |
0 ms |
5156 KB |
n = 8 |
29 |
Correct |
0 ms |
5156 KB |
n = 16 |
30 |
Correct |
0 ms |
5156 KB |
n = 16 |
31 |
Correct |
0 ms |
5156 KB |
n = 16 |
32 |
Correct |
0 ms |
5156 KB |
n = 16 |
33 |
Correct |
0 ms |
5156 KB |
n = 16 |
34 |
Correct |
0 ms |
5156 KB |
n = 16 |
35 |
Correct |
0 ms |
5156 KB |
n = 16 |
36 |
Correct |
0 ms |
5156 KB |
n = 15 |
37 |
Correct |
0 ms |
5156 KB |
n = 8 |
38 |
Correct |
0 ms |
5156 KB |
n = 16 |
39 |
Correct |
0 ms |
5156 KB |
n = 16 |
40 |
Correct |
0 ms |
5156 KB |
n = 9 |
41 |
Correct |
0 ms |
5156 KB |
n = 16 |
42 |
Correct |
0 ms |
5156 KB |
n = 16 |
43 |
Correct |
0 ms |
5156 KB |
n = 16 |
44 |
Correct |
0 ms |
5156 KB |
n = 9 |
45 |
Correct |
0 ms |
5156 KB |
n = 15 |
46 |
Correct |
0 ms |
5156 KB |
n = 16 |
47 |
Correct |
0 ms |
5156 KB |
n = 16 |
48 |
Correct |
0 ms |
5156 KB |
n = 16 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
246 ms |
11448 KB |
n = 199999 |
2 |
Correct |
263 ms |
21180 KB |
n = 199991 |
3 |
Correct |
326 ms |
21180 KB |
n = 199993 |
4 |
Correct |
163 ms |
10696 KB |
n = 152076 |
5 |
Correct |
109 ms |
8248 KB |
n = 93249 |
6 |
Correct |
206 ms |
11448 KB |
n = 199910 |
7 |
Correct |
223 ms |
11448 KB |
n = 199999 |
8 |
Correct |
216 ms |
11448 KB |
n = 199997 |
9 |
Correct |
193 ms |
11000 KB |
n = 171294 |
10 |
Correct |
163 ms |
10520 KB |
n = 140872 |
11 |
Correct |
189 ms |
11448 KB |
n = 199886 |
12 |
Correct |
236 ms |
11448 KB |
n = 199996 |
13 |
Correct |
189 ms |
11448 KB |
n = 200000 |
14 |
Correct |
219 ms |
16572 KB |
n = 199998 |
15 |
Correct |
203 ms |
16572 KB |
n = 200000 |
16 |
Correct |
219 ms |
16572 KB |
n = 199998 |
17 |
Correct |
263 ms |
22928 KB |
n = 200000 |
18 |
Correct |
206 ms |
16412 KB |
n = 190000 |
19 |
Correct |
176 ms |
11096 KB |
n = 177777 |
20 |
Correct |
89 ms |
8344 KB |
n = 100000 |
21 |
Correct |
243 ms |
11448 KB |
n = 200000 |
22 |
Correct |
263 ms |
16572 KB |
n = 200000 |
23 |
Correct |
263 ms |
16572 KB |
n = 200000 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
5156 KB |
n = 2 |
2 |
Correct |
0 ms |
5156 KB |
n = 2 |
3 |
Correct |
0 ms |
5156 KB |
n = 2 |
4 |
Correct |
0 ms |
5156 KB |
n = 2 |
5 |
Correct |
0 ms |
5156 KB |
n = 2 |
6 |
Correct |
0 ms |
5156 KB |
n = 2 |
7 |
Correct |
0 ms |
5156 KB |
n = 3 |
8 |
Correct |
0 ms |
5156 KB |
n = 3 |
9 |
Correct |
0 ms |
5156 KB |
n = 3 |
10 |
Correct |
0 ms |
5156 KB |
n = 8 |
11 |
Correct |
0 ms |
5156 KB |
n = 8 |
12 |
Correct |
0 ms |
5156 KB |
n = 8 |
13 |
Correct |
0 ms |
5156 KB |
n = 8 |
14 |
Correct |
0 ms |
5156 KB |
n = 8 |
15 |
Correct |
0 ms |
5156 KB |
n = 8 |
16 |
Correct |
0 ms |
5156 KB |
n = 8 |
17 |
Correct |
0 ms |
5156 KB |
n = 8 |
18 |
Correct |
0 ms |
5156 KB |
n = 8 |
19 |
Correct |
0 ms |
5156 KB |
n = 3 |
20 |
Correct |
0 ms |
5156 KB |
n = 7 |
21 |
Correct |
0 ms |
5156 KB |
n = 8 |
22 |
Correct |
0 ms |
5156 KB |
n = 8 |
23 |
Correct |
0 ms |
5156 KB |
n = 8 |
24 |
Correct |
0 ms |
5156 KB |
n = 8 |
25 |
Correct |
0 ms |
5156 KB |
n = 8 |
26 |
Correct |
0 ms |
5156 KB |
n = 8 |
27 |
Correct |
0 ms |
5156 KB |
n = 8 |
28 |
Correct |
0 ms |
5156 KB |
n = 8 |
29 |
Correct |
0 ms |
5156 KB |
n = 16 |
30 |
Correct |
0 ms |
5156 KB |
n = 16 |
31 |
Correct |
0 ms |
5156 KB |
n = 16 |
32 |
Correct |
0 ms |
5156 KB |
n = 16 |
33 |
Correct |
0 ms |
5156 KB |
n = 16 |
34 |
Correct |
0 ms |
5156 KB |
n = 16 |
35 |
Correct |
0 ms |
5156 KB |
n = 16 |
36 |
Correct |
0 ms |
5156 KB |
n = 15 |
37 |
Correct |
0 ms |
5156 KB |
n = 8 |
38 |
Correct |
0 ms |
5156 KB |
n = 16 |
39 |
Correct |
0 ms |
5156 KB |
n = 16 |
40 |
Correct |
0 ms |
5156 KB |
n = 9 |
41 |
Correct |
0 ms |
5156 KB |
n = 16 |
42 |
Correct |
0 ms |
5156 KB |
n = 16 |
43 |
Correct |
0 ms |
5156 KB |
n = 16 |
44 |
Correct |
0 ms |
5156 KB |
n = 9 |
45 |
Correct |
0 ms |
5156 KB |
n = 15 |
46 |
Correct |
0 ms |
5156 KB |
n = 16 |
47 |
Correct |
0 ms |
5156 KB |
n = 16 |
48 |
Correct |
0 ms |
5156 KB |
n = 16 |
49 |
Correct |
246 ms |
11448 KB |
n = 199999 |
50 |
Correct |
263 ms |
21180 KB |
n = 199991 |
51 |
Correct |
326 ms |
21180 KB |
n = 199993 |
52 |
Correct |
163 ms |
10696 KB |
n = 152076 |
53 |
Correct |
109 ms |
8248 KB |
n = 93249 |
54 |
Correct |
206 ms |
11448 KB |
n = 199910 |
55 |
Correct |
223 ms |
11448 KB |
n = 199999 |
56 |
Correct |
216 ms |
11448 KB |
n = 199997 |
57 |
Correct |
193 ms |
11000 KB |
n = 171294 |
58 |
Correct |
163 ms |
10520 KB |
n = 140872 |
59 |
Correct |
189 ms |
11448 KB |
n = 199886 |
60 |
Correct |
236 ms |
11448 KB |
n = 199996 |
61 |
Correct |
189 ms |
11448 KB |
n = 200000 |
62 |
Correct |
219 ms |
16572 KB |
n = 199998 |
63 |
Correct |
203 ms |
16572 KB |
n = 200000 |
64 |
Correct |
219 ms |
16572 KB |
n = 199998 |
65 |
Correct |
263 ms |
22928 KB |
n = 200000 |
66 |
Correct |
206 ms |
16412 KB |
n = 190000 |
67 |
Correct |
176 ms |
11096 KB |
n = 177777 |
68 |
Correct |
89 ms |
8344 KB |
n = 100000 |
69 |
Correct |
243 ms |
11448 KB |
n = 200000 |
70 |
Correct |
263 ms |
16572 KB |
n = 200000 |
71 |
Correct |
263 ms |
16572 KB |
n = 200000 |
72 |
Correct |
213 ms |
11448 KB |
n = 200000 |
73 |
Correct |
243 ms |
13496 KB |
n = 200000 |
74 |
Correct |
313 ms |
21180 KB |
n = 200000 |
75 |
Correct |
256 ms |
21180 KB |
n = 200000 |
76 |
Correct |
196 ms |
11448 KB |
n = 200000 |
77 |
Correct |
186 ms |
11448 KB |
n = 200000 |
78 |
Correct |
209 ms |
16572 KB |
n = 200000 |
79 |
Correct |
193 ms |
11192 KB |
n = 184307 |
80 |
Correct |
73 ms |
7976 KB |
n = 76040 |
81 |
Correct |
196 ms |
11448 KB |
n = 199981 |
82 |
Correct |
203 ms |
11448 KB |
n = 199994 |
83 |
Correct |
186 ms |
11448 KB |
n = 199996 |
84 |
Correct |
226 ms |
16572 KB |
n = 199998 |
85 |
Correct |
223 ms |
16572 KB |
n = 200000 |
86 |
Correct |
223 ms |
16572 KB |
n = 199998 |
87 |
Correct |
256 ms |
22932 KB |
n = 200000 |
88 |
Correct |
239 ms |
16716 KB |
n = 200000 |
89 |
Correct |
246 ms |
11448 KB |
n = 200000 |
90 |
Correct |
199 ms |
11448 KB |
n = 200000 |
91 |
Correct |
276 ms |
16572 KB |
n = 200000 |
92 |
Correct |
239 ms |
16572 KB |
n = 200000 |