#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];
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 = mn;
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;
q.push_back({s, s});
bool lol = false;
for (auto &n : nodes)
n.mn = 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)
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;
}
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);
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);
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
62804 KB |
3rd lines differ - on the 1st token, expected: '6', found: '2000000006' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
62804 KB |
3rd lines differ - on the 1st token, expected: '6', found: '2000000006' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
62804 KB |
3rd lines differ - on the 1st token, expected: '6', found: '2000000006' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
30 ms |
62856 KB |
3rd lines differ - on the 1st token, expected: '3304', found: '3590' |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
62804 KB |
3rd lines differ - on the 1st token, expected: '6', found: '2000000006' |
2 |
Halted |
0 ms |
0 KB |
- |