0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-09 16:38:47 +00:00

#2130. 「NOI2015」软件包管理器

https://loj.ac/s/1316640
This commit is contained in:
Baoshuo Ren 2021-12-04 23:22:32 +08:00
parent 8fcf4aa5bb
commit ee17746fb7
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68
41 changed files with 272 additions and 0 deletions

152
LibreOJ/2130/2130.cpp Normal file
View File

@ -0,0 +1,152 @@
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
const int N = 100005;
int n, q, a, x;
std::string op;
std::vector<int> g[N];
// Heavy Path Decomposition
int cnt, id[N], fa[N], dep[N], son[N], siz[N], top[N];
void dfs1(int, int);
void dfs2(int, int);
void modify_path(int, int, int);
void modify_tree(int, int);
// Segment Tree
void pushup(int);
void pushdown(int);
void build(int, int, int);
void modify(int, int, int, int);
int query(int, int, int);
int main() {
cin >> n;
for (int i = 2; i <= n; i++) {
cin >> x;
x++;
g[x].push_back(i);
g[i].push_back(x);
}
dfs1(1, 1);
dfs2(1, 1);
build(1, 1, n);
cin >> q;
while (q--) {
cin >> op >> x;
x++;
int before = query(1, 1, n);
if (op == "install") {
if (!query(1, id[x], id[x])) {
modify_path(1, x, 1);
}
} else {
if (query(1, id[x], id[x])) {
modify_tree(x, 0);
}
}
int after = query(1, 1, n);
cout << std::abs(before - after) << endl;
}
return 0;
}
// === Heavy Path Decomposition ===
void dfs1(int u, int f) {
dep[u] = dep[f] + 1;
fa[u] = f;
siz[u] = 1;
for (int v : g[u]) {
if (v == f) continue;
dfs1(v, u);
siz[u] += siz[v];
if (siz[son[u]] < siz[v]) son[u] = v;
}
}
void dfs2(int u, int t) {
id[u] = ++cnt;
top[u] = t;
if (!son[u]) return;
dfs2(son[u], t);
for (int v : g[u]) {
if (v == fa[u]) continue;
if (v == son[u]) continue;
dfs2(v, v);
}
}
void modify_path(int u, int v, int d) {
while (top[u] != top[v]) {
if (dep[top[u]] < dep[top[v]]) std::swap(u, v);
modify(1, id[top[u]], id[u], d);
u = fa[top[u]];
}
if (dep[u] < dep[v]) std::swap(u, v);
modify(1, id[v], id[u], d);
}
void modify_tree(int u, int d) {
modify(1, id[u], id[u] + siz[u] - 1, d);
}
// === Segment Tree ===
struct node {
int l, r, s, d;
node()
: l(0), r(0), s(0), d(-1) {}
node(int _l, int _r)
: l(_l), r(_r), s(0), d(-1) {}
} tr[N << 2];
inline void pushup(int u) {
tr[u].s = tr[u << 1].s + tr[u << 1 | 1].s;
}
inline void pushdown(int u) {
auto &root = tr[u], &left = tr[u << 1], &right = tr[u << 1 | 1];
if (root.d == -1) return;
left.s = (left.r - left.l + 1) * root.d;
left.d = root.d;
right.s = (right.r - right.l + 1) * root.d;
right.d = root.d;
root.d = -1;
}
void build(int u, int l, int r) {
tr[u] = node(l, r);
if (l == r) return;
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
}
void modify(int u, int l, int r, int d) {
if (l <= tr[u].l && tr[u].r <= r) {
tr[u].s = (tr[u].r - tr[u].l + 1) * d;
tr[u].d = d;
return;
}
int mid = tr[u].l + tr[u].r >> 1;
pushdown(u);
if (l <= mid) modify(u << 1, l, r, d);
if (r > mid) modify(u << 1 | 1, l, r, d);
pushup(u);
}
int query(int u, int l, int r) {
if (l <= tr[u].l && tr[u].r <= r) return tr[u].s;
int mid = tr[u].l + tr[u].r >> 1;
int sum = 0;
pushdown(u);
if (l <= mid) sum += query(u << 1, l, r);
if (r > mid) sum += query(u << 1 | 1, l, r);
return sum;
}

BIN
LibreOJ/2130/data/manager1.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager1.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager10.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager10.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager11.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager11.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager12.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager12.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager13.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager13.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager14.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager14.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager15.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager15.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager16.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager16.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager17.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager17.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager18.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager18.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager19.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager19.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager2.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager2.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager20.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager20.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager3.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager3.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager4.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager4.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager5.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager5.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager6.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager6.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager7.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager7.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager8.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager8.out (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager9.in (Stored with Git LFS) Normal file

Binary file not shown.

BIN
LibreOJ/2130/data/manager9.out (Stored with Git LFS) Normal file

Binary file not shown.