题解:P11306 [COTS 2016] 搜索树 Jelka
对于 BST 而言,就是中序遍历单调不降。(学平衡树的时候都讲过这个吧)
然后我们就去维护这样一个中序遍历下的
我们观察到,中序遍历下的第
至于维护权值与
#include<bits/stdc++.h>
using namespace std;
#define void inline void
const int N = 2e5 + 5;
vector<int>G[N];
#define pb push_back
int ord[N], l[N], r[N], rev_ord[N];
int father[N], siz[N], son[N], dep[N];
int top[N], w[N], dfn[N], rev[N], num;
void dfs1(int u, int fa) {
father[u] = fa, siz[u] = 1, dep[u] = dep[fa] + 1;
for(auto v : G[u]) {
if(v == fa) continue;
dfs1(v, u); siz[u] += siz[v];
if(!son[u] || siz[v] > siz[son[u]]) son[u] = v;
}
}
void dfs2(int u, int topx) {
top[u] = topx; dfn[u] = ++num; rev[num] = u;
if(!son[u]) return;
dfs2(son[u], topx);
for(auto v : G[u]) {
if(v == father[u] || v == son[u]) continue;
dfs2(v, v);
}
}
struct node {
int l, r;
int sum, tag;
} tree[N << 2];
#define lc (k * 2)
#define rc (k * 2 + 1)
#define mid (tree[k].l + tree[k].r >> 1)
void pushup(int k) { if(tree[k].tag) tree[k].sum = 0; else tree[k].sum = tree[lc].sum + tree[rc].sum; }
void build(int k, int l, int r) {
tree[k].l = l, tree[k].r = r;
if(l == r) { tree[k].sum = 1; return; }
build(lc, l, mid), build(rc, mid + 1, r);
pushup(k);
}
void modify(int k, int l, int r, int v) {
if(tree[k].l >= l && tree[k].r <= r) {
tree[k].tag += v;
if(tree[k].l == tree[k].r) { tree[k].sum = !tree[k].tag; }
else pushup(k);
return;
}
if(r <= mid) modify(lc, l, r, v);
else if(l > mid) modify(rc, l, r, v);
else modify(lc, l, mid, v), modify(rc, mid + 1, r, v);
pushup(k);
}
int query(int k, int l, int r) {
if(tree[k].tag) return 0;
if(tree[k].l >= l && tree[k].r <= r) return tree[k].sum;
if(r <= mid) return query(lc, l, r);
else if(l > mid) return query(rc, l, r);
else return query(lc, l, mid) + query(rc, mid + 1, r);
}
int idx;
void dfs(int x) {
if(l[x]) dfs(l[x]); ord[++idx] = x; if(r[x]) dfs(r[x]);
}
int lca(int x, int y) {
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
x = father[top[x]];
}
return dep[x] < dep[y] ? x : y;
}
void modify_in_path(int x, int y, int z) {
while(top[x] != top[y]) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
modify(1, dfn[top[x]], dfn[x], z);
x = father[top[x]];
}
if(dep[x] > dep[y]) swap(x, y);
modify(1, dfn[x], dfn[y], z);
}
void solve() {
int n, q; cin >> n >> q;
for(int i = 1; i <= n; i++) {
cin >> l[i] >> r[i];
if(l[i]) G[i].push_back(l[i]), G[l[i]].push_back(i);
if(r[i]) G[i].push_back(r[i]), G[r[i]].push_back(i);
}
for(int i = 1; i <= n; i++) cin >> w[i];
dfs1(1, 0), dfs2(1, 1); build(1, 1, n);
dfs(1); for(int i = 1; i <= n; i++) rev_ord[ord[i]] = i;
for(int i = 1; i < n; i++)
if(w[ord[i]] > w[ord[i + 1]])
modify_in_path(lca(ord[i], ord[i + 1]), 1, 1);
for(int i = 1; i <= q; i++) {
int x, y; cin >> x >> y;
int pos = rev_ord[x];
if(pos > 1 && w[x] < w[ord[pos - 1]] && y >= w[ord[pos - 1]]) { modify_in_path(lca(x, ord[pos - 1]), 1, -1); }
if(pos < n && w[x] > w[ord[pos + 1]] && y <= w[ord[pos + 1]]) { modify_in_path(lca(x, ord[pos + 1]), 1, -1); }
if(pos > 1 && w[x] >= w[ord[pos - 1]] && y < w[ord[pos - 1]]) { modify_in_path(lca(x, ord[pos - 1]), 1, 1); }
if(pos < n && w[x] <= w[ord[pos + 1]] && y > w[ord[pos + 1]]) { modify_in_path(lca(x, ord[pos + 1]), 1, 1); }
w[x] = y;
cout << query(1, 1, n) << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t = 1;
//cin >> t;
while(t--) {
solve();
}
return 0;
}