이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "books.h"
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
using ll = long long;
bool seen[1000005];
vector<int> P;
vector<int> t;
struct Node
{
int mn, mx;
};
Node nodes[8000005];
int gmn[1000005], gmx[1000005];
void upd(int i, int l, int r, int p, int mn, int mx)
{
auto &node = nodes[i];
if (p < l or p > r)
return;
if (l == r)
{
node.mn = min(node.mn, mn);
node.mx = max(node.mx, mx);
return;
}
int m = (l + r) / 2;
upd(2 * i, l, m, p, mn, mx);
upd(2 * i + 1, m + 1, r, p, mn, mx);
node.mn = min(nodes[2 * i].mn, nodes[2 * i + 1].mn);
node.mx = max(nodes[2 * i].mx, nodes[2 * i + 1].mx);
}
pair<int, int> query(int i, int l, int r, int ql, int qr)
{
auto &node = nodes[i];
if (qr < l or ql > r)
return {1e9, 0};
if (ql <= l and qr >= r)
return {node.mn, node.mx};
int m = (l + r) / 2;
auto A = query(2 * i, l, m, ql, qr);
auto B = query(2 * i + 1, m + 1, r, ql, qr);
return {min(A.first, B.first), max(A.second, B.second)};
}
ll dfs(int i)
{
if (seen[i])
return 0;
seen[i] = true;
t.push_back(i);
return abs(i - P[i]) + dfs(P[i]);
}
ll minimum_walk(vector<int> p, int s)
{
P = p;
ll sum = 0;
int n = p.size();
vector<pair<int, int>> q;
vector<pair<pair<int, int>, int>> Q, Q2;
q.push_back({s, s});
bool lol = false;
for (auto &n : nodes)
n.mn = 1e9;
for (auto &q : gmn)
q = 1e9;
for (int i = 0; i < n; i++)
{
if (seen[i] or i == p[i])
continue;
t.clear();
sum += dfs(i);
sort(t.begin(), t.end());
q.push_back({t.front(), t.back()});
auto [a, b] = q.back();
for (int x : t)
Q.push_back({{a, b}, x});
for (int x : t)
Q2.push_back({{b, a}, x});
// for (int x : t)
// upd(1, 0, n, x, a, b);
// if (a <= s and s <= b)
// lol = true;
}
sort(q.begin(), q.end());
int st = 0;
int lt = -1;
int pl, pr;
for (auto [a, b] : q)
{
if (lt == -1)
{
st = a;
lt = b;
}
if (a > lt)
{
if (st <= s and s <= lt)
{
lol = true;
pl = st, pr = lt;
}
sum += 2 * (a - lt);
lt = b;
st = a;
}
else
{
lt = max(lt, b);
}
}
if (st <= s and s <= lt)
{
lol = true;
pl = st, pr = lt;
}
if (pl == pr)
lol = false;
sort(Q.begin(), Q.end());
sort(Q2.begin(), Q2.end(), greater<>());
for (auto [p, i] : Q)
{
auto [a, b] = p;
auto [x, y] = query(1, 0, n, a, b);
upd(1, 0, n, i, min(x, a), y);
gmn[i] = min(x, a);
}
for (auto [p, i] : Q2)
{
auto [b, a] = p;
auto [x, y] = query(1, 0, n, a, b);
upd(1, 0, n, i, x, max(y, b));
gmx[i] = max(y, b);
}
// for (int p = 0; p < 21; p++)
// {
// for (int i = 0; i < n; i++)
// {
// auto [a, b] = query(1, 0, n, i, i);
// auto [x, y] = query(1, 0, n, a, b);
// // printf("%d %d %d\n", i, x, y);
// upd(1, 0, n, i, x, y);
// }
// }
int mn = 1e9;
for (int i = 0; i < n; i++)
{
// auto [a, b] = query(1, 0, n, i, i);
auto a = gmn[i], b = gmx[i];
if (a <= pl and pr <= b)
mn = min(mn, abs(i - s));
}
#ifdef DEBUG
printf("%d %d\n", lol, mn);
#endif
if (lol)
sum += 2 * mn;
return sum;
}
컴파일 시 표준 에러 (stderr) 메시지
books.cpp: In function 'll minimum_walk(std::vector<int>, int)':
books.cpp:127:2: warning: 'pr' may be used uninitialized in this function [-Wmaybe-uninitialized]
127 | if (pl == pr)
| ^~
books.cpp:127:2: warning: 'pl' may be used uninitialized in this function [-Wmaybe-uninitialized]
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |