#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]);
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:84:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (i = 0; i < E.size(); i++)
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
4284 KB |
answer is not correct: 121762918 instead of 0 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
4284 KB |
answer is not correct: 121762918 instead of 0 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
503 ms |
32392 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
4284 KB |
answer is not correct: 121762918 instead of 0 |
2 |
Halted |
0 ms |
0 KB |
- |