洛谷 P2146 [NOI2015]软件包管理器
题意
给定一棵 n n n 个结点的树,有 m m m 次操作,格式如下:
- 安装 x x x 软件包
- 卸载 x x x 软件包
- 安装软件包需要先安装所有依赖的软件包,即从根节点到当前节点的一条链。
- 卸载软件包需要先卸载所有依赖这个软件的软件包,即这个结点的所有子树。
每次操作需要先输出这次操作会改变多少软件包的状态,随后应用这个操作。
解法
- 每次需要维护一条链或者子树的和,并进行区间赋值操作。
- 使用树链剖分+线段树维护即可。区间赋值使用lazy tag标记赋值即可。
- 安装的时候就输出从当前结点到根节点的总数减已安装的数量,就是需要安装的数量,然后区间赋值。
- 卸载的时候就输出当前结点子树已安装的数量,然后区间赋值。
代码
#pragma region
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define tr t[root]
#define lson t[root << 1]
#define rson t[root << 1 | 1]
#define rep(i, a, n) for (int i = a; i <= n; ++i)
#define per(i, a, n) for (int i = n; i >= a; --i)
namespace fastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
//fread->R
bool IOerror = 0;
//inline char nc(){char ch=getchar();if(ch==-1)IOerror=1;return ch;}
inline char nc() {
static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
if (p1 == pend) {
p1 = buf;
pend = buf + fread(buf, 1, BUF_SIZE, stdin);
if (pend == p1) {
IOerror = 1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; }
template <class T>
inline bool R(T &x) {
bool sign = 0;
char ch = nc();
x = 0;
for (; blank(ch); ch = nc())
;
if (IOerror)
return false;
if (ch == '-')
sign = 1, ch = nc();
for (; ch >= '0' && ch <= '9'; ch = nc())
x = x * 10 + ch - '0';
if (sign)
x = -x;
return true;
}
inline bool R(double &x) {
bool sign = 0;
char ch = nc();
x = 0;
for (; blank(ch); ch = nc())
;
if (IOerror)
return false;
if (ch == '-')
sign = 1, ch = nc();
for (; ch >= '0' && ch <= '9'; ch = nc())
x = x * 10 + ch - '0';
if (ch == '.') {
double tmp = 1;
ch = nc();
for (; ch >= '0' && ch <= '9'; ch = nc())
tmp /= 10.0, x += tmp * (ch - '0');
}
if (sign)
x = -x;
return true;
}
inline bool R(char *s) {
char ch = nc();
for (; blank(ch); ch = nc())
;
if (IOerror)
return false;
for (; !blank(ch) && !IOerror; ch = nc())
*s++ = ch;
*s = 0;
return true;
}
inline bool R(char &c) {
c = nc();
if (IOerror) {
c = -1;
return false;
}
return true;
}
template <class T, class... U>
bool R(T &h, U &... tmp) { return R(h) && R(tmp...); }
#undef OUT_SIZE
#undef BUF_SIZE
}; // namespace fastIO
using namespace fastIO;
template <class T>
void _W(const T &x) { cout << x; }
void _W(const int &x) { printf("%d", x); }
void _W(const int64_t &x) { printf("%lld", x); }
void _W(const double &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template <class T, class U>
void _W(const pair<T, U> &x) { _W(x.F), putchar(' '), _W(x.S); }
template <class T>
void _W(const vector<T> &x) {
for (auto i = x.begin(); i != x.end(); _W(*i++))
if (i != x.cbegin()) putchar(' ');
}
void W() {}
template <class T, class... U>
void W(const T &head, const U &... tail) { _W(head), putchar(sizeof...(tail) ? ' ' : '\n'), W(tail...); }
#pragma endregion
const int maxn = 1e5 + 5;
int n, m, a[maxn];
vector<int> g[maxn];
int son[maxn], fa[maxn], dep[maxn], sz[maxn];
int cnt, id[maxn], wt[maxn], top[maxn];
void dfs1(int u, int f, int deep) {
fa[u] = f, dep[u] = deep, sz[u] = 1;
for (auto v : g[u]) {
if (v == f) continue;
dfs1(v, u, deep + 1);
sz[u] += sz[v];
if (sz[son[u]] < sz[v]) son[u] = v;
}
}
void dfs2(int u, int topf) {
id[u] = ++cnt, wt[cnt] = a[u], top[u] = topf;
if (!son[u]) return;
dfs2(son[u], topf);
for (auto v : g[u]) {
if (v == fa[u] || v == son[u]) continue;
dfs2(v, v);
}
}
struct node {
int l, r, val, lz0, lz1;
} t[maxn << 2];
void build(int root, int l, int r) {
tr.l = l, tr.r = r, tr.val = tr.lz0 = tr.lz1 = 0;
if (l == r) return;
int mid = (l + r) >> 1;
build(root << 1, l, mid);
build(root << 1 | 1, mid + 1, r);
}
void spread(int root) {
if (tr.lz0) {
lson.val = 0;
rson.val = 0;
lson.lz0 = 1, rson.lz0 = 1;
lson.lz1 = 0, rson.lz1 = 0;
tr.lz0 = tr.lz1 = 0;
} else if (tr.lz1) {
lson.val = lson.r - lson.l + 1;
rson.val = rson.r - rson.l + 1;
lson.lz1 = 1, rson.lz1 = 1;
lson.lz0 = 0, rson.lz0 = 0;
tr.lz0 = tr.lz1 = 0;
}
}
void update1(int root, int l, int r) {
if (l <= tr.l && tr.r <= r) {
tr.val = tr.r - tr.l + 1;
tr.lz0 = 0, tr.lz1 = 1;
return;
}
spread(root);
int mid = (tr.l + tr.r) >> 1;
if (l <= mid) update1(root << 1, l, r);
if (r > mid) update1(root << 1 | 1, l, r);
tr.val = lson.val + rson.val;
}
void update0(int root, int l, int r) {
if (l <= tr.l && tr.r <= r) {
tr.val = 0;
tr.lz0 = 1, tr.lz1 = 0;
return;
}
spread(root);
int mid = (tr.l + tr.r) >> 1;
if (l <= mid) update0(root << 1, l, r);
if (r > mid) update0(root << 1 | 1, l, r);
tr.val = lson.val + rson.val;
}
int query(int root, int l, int r) {
if (l <= tr.l && tr.r <= r) return tr.val;
spread(root);
int ans = 0;
int mid = (tr.l + tr.r) >> 1;
if (l <= mid) ans += query(root << 1, l, r);
if (r > mid) ans += query(root << 1 | 1, l, r);
return ans;
}
int install(int x) {
int ans = 0;
while (top[x] != 1) {
ans += id[x] - id[top[x]] + 1 - query(1, id[top[x]], id[x]);
update1(1, id[top[x]], id[x]);
x = fa[top[x]];
}
ans += id[x] - id[1] + 1 - query(1, id[1], id[x]);
update1(1, id[1], id[x]);
return ans;
}
int uninstall(int x) {
int ans = query(1, id[x], id[x] + sz[x] - 1);
update0(1, id[x], id[x] + sz[x] - 1);
return ans;
}
int main() {
R(n);
rep(i, 2, n) {
int f;
R(f);
g[f + 1].push_back(i);
g[i].push_back(f + 1);
}
dfs1(1, 0, 1);
dfs2(1, 1);
build(1, 1, n);
R(m);
while (m--) {
char op[20];
R(op + 1);
int x;
R(x);
++x;
if (op[1] == 'i') W(install(x));
if (op[1] == 'u') W(uninstall(x));
}
}