#include "railroad.h"
#include <set>
#include <algorithm>
#define INF 1000000007
std::set<long long> I;
std::vector<long long> Ic;
long long Fen[200005];
int Pa[200005];
int n;
typedef struct node node;
struct node
{
int u, v;
long long w;
};
int compareW(node a, node b)
{
return (a.w < b.w);
}
std::vector<node> E;
void update(int pos, int val)
{
for (; pos <= Ic.size(); pos += pos&-pos)
Fen[pos] += val;
}
long long query(int pos)
{
long long r = 0;
for (; pos > 0; pos -= pos&-pos)
r += Fen[pos];
return r;
}
int uf(int u)
{
return (Pa[u] == 0)?u:(Pa[u] = uf(Pa[u]));
}
void join(int u, int v)
{
// printf("Join %d (%d) %d (%d)\n", u, Ic[u - 1], v, Ic[v - 1]);
int pu = uf(u);
int pv = uf(v);
if (pu != pv)
Pa[pu] = pv;
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
n = (int) s.size();
int i;
I.insert(1);
I.insert(INF);
for (i = 0; i < n; i++)
{
I.insert(s[i]);
I.insert(t[i]);
}
std::set<long long>::iterator it;
for (it = I.begin(); it != I.end(); it++)
Ic.push_back(*it);
int u, v;
for (i = 0; i < n; i++)
{
u = std::lower_bound(Ic.begin(), Ic.end(), s[i]) - Ic.begin() + 1;
v = std::lower_bound(Ic.begin(), Ic.end(), t[i]) - Ic.begin() + 1;
update(u, 1);
update(v, -1);
join(u, v);
}
update(Ic.size(), 1);
update(1, -1);
join(Ic.size(), 1);
long long r, Ans = 0;
for (i = 1; i < Ic.size(); i++)
{
r = query(i);
// printf("r%d\n", r);
if (r > 0)
{
Ans += r*(Ic[i] - Ic[i - 1]);
}
if (r != 0)
{
join(i, i + 1);
}
E.push_back({i, i + 1, (Ic[i] - Ic[i - 1])});
}
std::sort(E.begin(), E.end(), compareW);
int pu, pv;
for (i = 0; i < E.size(); i++)
{
u = E[i].u;
v = E[i].v;
pu = uf(u);
pv = uf(v);
if (pu != pv)
{
// printf("Add %d (%d) %d (%d)\n", u, Ic[u - 1], v, Ic[v - 1]);
Pa[pu] = pv;
Ans += E[i].w;
}
}
return Ans;
}
Compilation message
railroad.cpp: In function 'void update(int, int)':
railroad.cpp:23:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (; pos <= Ic.size(); pos += pos&-pos)
^
railroad.cpp: In function 'long long int plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:71:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 1; i < Ic.size(); i++)
^
railroad.cpp:87:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < E.size(); i++)
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4284 KB |
n = 2 |
2 |
Correct |
0 ms |
4284 KB |
n = 2 |
3 |
Correct |
0 ms |
4284 KB |
n = 2 |
4 |
Correct |
0 ms |
4284 KB |
n = 2 |
5 |
Correct |
0 ms |
4284 KB |
n = 2 |
6 |
Correct |
0 ms |
4284 KB |
n = 2 |
7 |
Correct |
0 ms |
4284 KB |
n = 3 |
8 |
Correct |
0 ms |
4284 KB |
n = 3 |
9 |
Correct |
0 ms |
4284 KB |
n = 3 |
10 |
Correct |
0 ms |
4284 KB |
n = 8 |
11 |
Correct |
0 ms |
4284 KB |
n = 8 |
12 |
Correct |
0 ms |
4284 KB |
n = 8 |
13 |
Correct |
0 ms |
4284 KB |
n = 8 |
14 |
Correct |
0 ms |
4284 KB |
n = 8 |
15 |
Correct |
0 ms |
4284 KB |
n = 8 |
16 |
Correct |
0 ms |
4284 KB |
n = 8 |
17 |
Correct |
0 ms |
4284 KB |
n = 8 |
18 |
Correct |
0 ms |
4284 KB |
n = 8 |
19 |
Correct |
0 ms |
4284 KB |
n = 3 |
20 |
Correct |
0 ms |
4284 KB |
n = 7 |
21 |
Correct |
0 ms |
4284 KB |
n = 8 |
22 |
Correct |
0 ms |
4284 KB |
n = 8 |
23 |
Correct |
0 ms |
4284 KB |
n = 8 |
24 |
Correct |
0 ms |
4284 KB |
n = 8 |
25 |
Correct |
0 ms |
4284 KB |
n = 8 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4284 KB |
n = 2 |
2 |
Correct |
0 ms |
4284 KB |
n = 2 |
3 |
Correct |
0 ms |
4284 KB |
n = 2 |
4 |
Correct |
0 ms |
4284 KB |
n = 2 |
5 |
Correct |
0 ms |
4284 KB |
n = 2 |
6 |
Correct |
0 ms |
4284 KB |
n = 2 |
7 |
Correct |
0 ms |
4284 KB |
n = 3 |
8 |
Correct |
0 ms |
4284 KB |
n = 3 |
9 |
Correct |
0 ms |
4284 KB |
n = 3 |
10 |
Correct |
0 ms |
4284 KB |
n = 8 |
11 |
Correct |
0 ms |
4284 KB |
n = 8 |
12 |
Correct |
0 ms |
4284 KB |
n = 8 |
13 |
Correct |
0 ms |
4284 KB |
n = 8 |
14 |
Correct |
0 ms |
4284 KB |
n = 8 |
15 |
Correct |
0 ms |
4284 KB |
n = 8 |
16 |
Correct |
0 ms |
4284 KB |
n = 8 |
17 |
Correct |
0 ms |
4284 KB |
n = 8 |
18 |
Correct |
0 ms |
4284 KB |
n = 8 |
19 |
Correct |
0 ms |
4284 KB |
n = 3 |
20 |
Correct |
0 ms |
4284 KB |
n = 7 |
21 |
Correct |
0 ms |
4284 KB |
n = 8 |
22 |
Correct |
0 ms |
4284 KB |
n = 8 |
23 |
Correct |
0 ms |
4284 KB |
n = 8 |
24 |
Correct |
0 ms |
4284 KB |
n = 8 |
25 |
Correct |
0 ms |
4284 KB |
n = 8 |
26 |
Correct |
0 ms |
4284 KB |
n = 8 |
27 |
Correct |
0 ms |
4284 KB |
n = 8 |
28 |
Correct |
0 ms |
4284 KB |
n = 8 |
29 |
Correct |
0 ms |
4284 KB |
n = 16 |
30 |
Correct |
0 ms |
4284 KB |
n = 16 |
31 |
Correct |
0 ms |
4284 KB |
n = 16 |
32 |
Correct |
0 ms |
4284 KB |
n = 16 |
33 |
Correct |
0 ms |
4284 KB |
n = 16 |
34 |
Correct |
0 ms |
4284 KB |
n = 16 |
35 |
Correct |
0 ms |
4284 KB |
n = 16 |
36 |
Correct |
0 ms |
4284 KB |
n = 15 |
37 |
Correct |
0 ms |
4284 KB |
n = 8 |
38 |
Correct |
0 ms |
4284 KB |
n = 16 |
39 |
Correct |
0 ms |
4284 KB |
n = 16 |
40 |
Correct |
0 ms |
4284 KB |
n = 9 |
41 |
Correct |
0 ms |
4284 KB |
n = 16 |
42 |
Correct |
0 ms |
4284 KB |
n = 16 |
43 |
Correct |
0 ms |
4284 KB |
n = 16 |
44 |
Correct |
0 ms |
4284 KB |
n = 9 |
45 |
Correct |
0 ms |
4284 KB |
n = 15 |
46 |
Correct |
0 ms |
4284 KB |
n = 16 |
47 |
Correct |
0 ms |
4284 KB |
n = 16 |
48 |
Correct |
0 ms |
4284 KB |
n = 16 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
319 ms |
32392 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
4284 KB |
n = 2 |
2 |
Correct |
0 ms |
4284 KB |
n = 2 |
3 |
Correct |
0 ms |
4284 KB |
n = 2 |
4 |
Correct |
0 ms |
4284 KB |
n = 2 |
5 |
Correct |
0 ms |
4284 KB |
n = 2 |
6 |
Correct |
0 ms |
4284 KB |
n = 2 |
7 |
Correct |
0 ms |
4284 KB |
n = 3 |
8 |
Correct |
0 ms |
4284 KB |
n = 3 |
9 |
Correct |
0 ms |
4284 KB |
n = 3 |
10 |
Correct |
0 ms |
4284 KB |
n = 8 |
11 |
Correct |
0 ms |
4284 KB |
n = 8 |
12 |
Correct |
0 ms |
4284 KB |
n = 8 |
13 |
Correct |
0 ms |
4284 KB |
n = 8 |
14 |
Correct |
0 ms |
4284 KB |
n = 8 |
15 |
Correct |
0 ms |
4284 KB |
n = 8 |
16 |
Correct |
0 ms |
4284 KB |
n = 8 |
17 |
Correct |
0 ms |
4284 KB |
n = 8 |
18 |
Correct |
0 ms |
4284 KB |
n = 8 |
19 |
Correct |
0 ms |
4284 KB |
n = 3 |
20 |
Correct |
0 ms |
4284 KB |
n = 7 |
21 |
Correct |
0 ms |
4284 KB |
n = 8 |
22 |
Correct |
0 ms |
4284 KB |
n = 8 |
23 |
Correct |
0 ms |
4284 KB |
n = 8 |
24 |
Correct |
0 ms |
4284 KB |
n = 8 |
25 |
Correct |
0 ms |
4284 KB |
n = 8 |
26 |
Correct |
0 ms |
4284 KB |
n = 8 |
27 |
Correct |
0 ms |
4284 KB |
n = 8 |
28 |
Correct |
0 ms |
4284 KB |
n = 8 |
29 |
Correct |
0 ms |
4284 KB |
n = 16 |
30 |
Correct |
0 ms |
4284 KB |
n = 16 |
31 |
Correct |
0 ms |
4284 KB |
n = 16 |
32 |
Correct |
0 ms |
4284 KB |
n = 16 |
33 |
Correct |
0 ms |
4284 KB |
n = 16 |
34 |
Correct |
0 ms |
4284 KB |
n = 16 |
35 |
Correct |
0 ms |
4284 KB |
n = 16 |
36 |
Correct |
0 ms |
4284 KB |
n = 15 |
37 |
Correct |
0 ms |
4284 KB |
n = 8 |
38 |
Correct |
0 ms |
4284 KB |
n = 16 |
39 |
Correct |
0 ms |
4284 KB |
n = 16 |
40 |
Correct |
0 ms |
4284 KB |
n = 9 |
41 |
Correct |
0 ms |
4284 KB |
n = 16 |
42 |
Correct |
0 ms |
4284 KB |
n = 16 |
43 |
Correct |
0 ms |
4284 KB |
n = 16 |
44 |
Correct |
0 ms |
4284 KB |
n = 9 |
45 |
Correct |
0 ms |
4284 KB |
n = 15 |
46 |
Correct |
0 ms |
4284 KB |
n = 16 |
47 |
Correct |
0 ms |
4284 KB |
n = 16 |
48 |
Correct |
0 ms |
4284 KB |
n = 16 |
49 |
Runtime error |
319 ms |
32392 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
50 |
Halted |
0 ms |
0 KB |
- |