이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "railroad.h"
#include <set>
#include <algorithm>
#define INF 1000000007
std::set<long long> I;
std::vector<long long> Ic;
long long Fen[400005];
int Pa[400005];
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;
}
컴파일 시 표준 에러 (stderr) 메시지
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++)
^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |