0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:05:26 +00:00

P6122 [NEERC2016]Mole Tunnels

https://www.luogu.com.cn/record/99781499
This commit is contained in:
Baoshuo Ren 2023-01-14 19:33:03 +08:00
parent 68704f8cb9
commit a0d0faf8be
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

96
Luogu/P6122/P6122.cpp Normal file
View File

@ -0,0 +1,96 @@
#include <iostream>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
int n, m, c[N], p[N], f[N << 1], g[N << 1], f2[N], ans;
void update(int u) {
f[u] = INF;
if (c[u]) {
f[u] = 0;
g[u] = u;
}
if (f[u] > f[u << 1] + (f2[u << 1] < 0 ? -1 : 1)) {
f[u] = f[u << 1] + (f2[u << 1] < 0 ? -1 : 1);
g[u] = g[u << 1];
}
if (f[u] > f[u << 1 | 1] + (f2[u << 1 | 1] < 0 ? -1 : 1)) {
f[u] = f[u << 1 | 1] + (f2[u << 1 | 1] < 0 ? -1 : 1);
g[u] = g[u << 1 | 1];
}
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
std::fill(std::begin(f), std::end(f), INF);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
for (int i = 1; i <= m; i++) {
cin >> p[i];
}
for (int i = n; i; i--) {
update(i);
}
for (int i = 1; i <= m; i++) {
int x = INF,
y = 0,
u = p[i],
t = 0,
v = 0;
while (u) {
if (x > f[u] + t) {
x = f[u] + t;
y = g[u];
v = u;
}
t += (f2[u] > 0 ? -1 : 1);
u >>= 1;
}
u = p[i];
ans += x;
while (u != v) {
f2[u]--;
update(u >>= 1);
}
c[y]--;
update(y);
while (y != v) {
f2[y]++;
update(y >>= 1);
}
while (v) {
update(v);
v >>= 1;
}
cout << ans << ' ';
}
cout << endl;
return 0;
}