题解:P15952 [ICPC 2018 Jakarta R] Rotating Gears
首先将点染色,可以简单地消除顺逆反转问题;然后考虑这就是每次禁用点和恢复,然后修改操作是对
首先不妨定根,找到这个未禁用连通块的最浅的节点
- 在
t 子树内。 - 设
d_i 表示到根路径上的禁用点数量,d_v=d_t 。
一次禁用点
一件非常惊奇的事是这时候已经可以用线段树维护了。考虑节点上放的标记 add[i], tag[i] 分别表示 tag[i]。每个节点的 add[i] 都来自于区间的 add[i] 后,tag[i] 的下传独立:由于 add[i] 的操作作用于整个区间,区间内部的 add[i] 对于儿子内部无影响。
再考虑如何维护极大连通块的大小 ,维护一个 cnt[i],当儿子的 cnt[i]。
我们不需要区间维护 add 标记永久化即可。所有操作的复杂度均为单
:::info[代码]
#include <bits/stdc++.h>
#define rep(i, l, r) for (int i = l; i <= r; ++i)
#define drep(i, r, l) for (int i = r; i >= l; --i)
#define LL long long
#define FASTIO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define FRE(p) freopen(#p".in", "r", stdin), freopen(#p".out", "w", stdout)
#define eb emplace_back
#define PII pair<int,int>
#define coni const int
using namespace std;
constexpr int N = 2e5+5, M = 5e5+5, RD = 360;
int n, m;
vector<int> G[N];
int fa[N], sz[N], dep[N], son[N], col[N], dfn[N], dfc, nfd[N], tp[N];
void dfs1(int x, int fa) {
sz[x] = 1, ::fa[x] = fa;
for (auto y : G[x]) if (y ^ fa) {
dep[y] = dep[x]+1, col[y] = -col[x], dfs1(y, x);
sz[x] += sz[y];
if (!son[x] || sz[son[x]] < sz[y]) son[x] = y;
}
}
void dfs2(int x, int top) {
dfn[x] = ++dfc, nfd[dfc] = x, tp[x] = top;
if (son[x]) dfs2(son[x], top);
for (auto y : G[x]) if (!dfn[y]) dfs2(y, y);
}
set<int> st[N];
inline int becoder(int x) {
int r = x;
while(x > 0 && (st[tp[x]].empty() || *st[tp[x]].begin() > dfn[x])) r = tp[x], x = fa[tp[x]];
if (!x) return 1;
auto it = st[tp[x]].upper_bound(dfn[x]); --it;
return *it==dfn[x] ? r : nfd[*it+1];
}
struct SGT {
int cnt[N<<2], ad[N<<2], tg[N<<2];
#define lc (p<<1)
#define rc (p<<1|1)
inline void pd(int p) {
if (tg[p]) {
if (!ad[lc]) tg[lc] += tg[p];
if (!ad[rc]) tg[rc] += tg[p];
tg[p] = 0;
}
}
inline void pushup(int p) {
cnt[p] = 0;
if (!ad[lc]) cnt[p] += cnt[lc];
if (!ad[rc]) cnt[p] += cnt[rc];
}
void build(int p=1, int l=1, int r=n) {
cnt[p] = (r-l+1), ad[p] = tg[p] = 0;
if (l == r) return ;
coni mid = (l+r)>>1;
build(lc, l, mid), build(rc, mid+1, r);
}
void modify(int L, int R, int op, int p=1, int l=1, int r=n) {
if (L <= l && r <= R) return ad[p]+=op, void();
coni mid = (l+r)>>1; pd(p);
if (L <= mid) modify(L, R, op, lc, l, mid);
if (mid < R) modify(L, R, op, rc, mid+1, r);
pushup(p);
}
int update(int L, int R, int v, int cur, int p=1, int l=1, int r=n) {
if (cur < 0) return 0;
if (L <= l && r <= R) {
if (cur^ad[p]) return 0;
return tg[p]+=v, cnt[p];
}
coni mid = (l+r)>>1; int re = 0; pd(p);
if (L <= mid) re += update(L, R, v, cur-ad[p], lc, l, mid);
if (mid < R) re += update(L, R, v, cur-ad[p], rc, mid+1, r);
return pushup(p), re;
}
int ask(int x, int p=1, int l=1, int r=n) {
if (l == r) return ad[p];
coni mid = (l+r)>>1; pd(p);
return (x <= mid ? ask(x, lc, l, mid) : ask(x, rc, mid+1, r))+ad[p];
}
int query(int p=1, int l=1, int r=n) {
if (l == r) {
int x = nfd[l];
return (tg[p]%RD*col[x]+RD)%RD;
}
coni mid = (l+r)>>1; pd(p);
return query(lc, l, mid)+query(rc, mid+1, r);
}
} t;
signed main() {
FASTIO;
#ifdef LOCAL
FRE(test);
#endif
cin >> n;
rep(i, 2, n) {
int u, v;
cin >> u >> v;
G[u].eb(v), G[v].eb(u);
}
col[1] = 1, dfs1(1, 0), dfs2(1, 1);
t.build();
cin >> m;
for (int op, x, alp; m--; ) {
cin >> op >> x;
if (op <= 2) {
t.modify(dfn[x], dfn[x]+sz[x]-1, op&1?1:-1);
if (op&1) st[tp[x]].insert(dfn[x]); else st[tp[x]].erase(dfn[x]);
}
else {
cin >> alp; const int u = becoder(x);
cout << t.update(dfn[u], dfn[u]+sz[u]-1, (alp*col[x]+RD)%RD, t.ask(dfn[u]))*alp << '\n';
}
}
cout << t.query() << '\n';
return 0;
}
:::
然而这个做法太聪明了,有没有更 naive 的做法?
考虑每一个禁用点的贡献。对