力大堆飞 P3261(五)

· · 个人记录

书接上文。路径对折。

一个废物。记下来是因为它成立。

我们注意到所有 (i+\lfloor\log_2(i)\rfloor-\operatorname{popcnt}(i)-\operatorname{ctz}(i),i+\lfloor\log_2(i)\rfloor-\operatorname{popcnt}(i)] 覆盖每个正整数至多一次,其中没覆盖到的正整数恰为每个 2 的幂。我们记之为 (l(i),r(i)]。这个结论的证明只需要说明对于所有奇数 x 有 l(x)=r(x) 且对于所有偶数 x 有 r(x)\le l(x+2) 即可。比较平凡。

于是我们考虑从每个点向上预处理若干个长度为 2 的幂的路径区间。设该点深度为 d(根的深度为 1),子树高度为 h(叶子高度为 0),则最长的区间长度为:

2^{\max(0,\operatorname{ctz}(d)-\max(\lfloor\log_2(d)\rfloor-\operatorname{popcnt}(d)-h,0))}

从这个点向上长度为不超过这个数的所有 2 的幂的路径区间将被预处理。那么我们可以证明两个事情:所有点的区间总长线性于节点总数 n;一条路径可以拆分为不超过 \log n 个区间的不交并。

先来处理第一个结论。我们考虑一个动态加叶子的过程。除了叶子 p 本身自带一个长度为 1 的路径区间外,我们要让某个节点产生更多的路径区间需要满足以下事实:

  1. 该节点 x 为 p 的祖先;

  2. 设 p 的深度为 d_p,x 的深度为 d_x,则 d_p\in(d_x+\lfloor\log_2(d_x)\rfloor-\operatorname{popcnt}(d_x)-\operatorname{ctz}(d_x),d_x+\lfloor\log_2(d_x)\rfloor-\operatorname{popcnt}(d_x)]]。

由于 (i+\lfloor\log_2(i)\rfloor-\operatorname{popcnt}(i)-\operatorname{ctz}(i),i+\lfloor\log_2(i)\rfloor-\operatorname{popcnt}(i)] 覆盖每个正整数至多一次,故每个 p 至多对一个 x 产生贡献。于是路径区间的总个数是线性的。

再来处理第二个结论。我们采用如下策略刻画一条从一个点到其祖先的路径:从当前点开始,每步转移到最浅的、在这条路径上的、该节点的某个路径区间的祖先。我们把这个过程分为两个阶段:

  1. 子树高度小于 \lfloor\log_2(n)\rfloor

这一阶段显然只会跳不超过 O(\log(n)) 步。

  1. 子树高度不小于 \lfloor\log_2(n)\rfloor

此时深度为 x 的节点最长路径区间为 \operatorname{lowbit}(x)。我们类似 BIT 的跳法,最多只会跳 O(\log n) 步就会遇到路径端点在当前节点的 BIT 区间内,然后每次看在区间内的前一半还是后一半决定是否跳一个较短的路径区间即可,类似倍增。

于是它的复杂度是对的。代码还算好写,但是太魔怔。

#include <bits/stdc++.h>
#define int long long
using namespace std;
constexpr int N = 3e5 + 9, A = 1e18;
int n, m, fa[N], d[N], rk[N], ans[N], cnt[N], len[N];
vector<int> es[N];
struct func {
  int xl, xr, yl, yr;
  int getk() const { return xl == xr ? 1 : (yr - yl) / (xr - xl); }
  int operator()(int x) const { return (x - xl) * getk() + yl; }
  friend func operator+(const func& a, const func& b) {
    static auto mnov = [](int x, int y) { return x < 0 ? 0 : (x + y - 1) / y; };
    if (a.xl > a.xr) return a;
    if (b.xl > b.xr) return b;
    int k = a.getk();
    func c;
    c.xl = mnov(b.xl - a.yl, k) + a.xl;
    c.xr = a.xr - mnov(a.yr - b.xr, k);
    if (c.xl > c.xr) return {1, 0, 0, 0};
    c.yl = b(a(c.xl)), c.yr = b(a(c.xr));
    return c;
  }
  func& operator+=(const func& to) { return *this = *this + to; }
} vl[N];
vector<func> tr[N];
vector<int> fth[N];
int dfs1(int x) {
  int p = d[x] = d[fa[x]] + 1, ret = 0;
  for (int y : es[x]) fa[y] = x, ret = max(ret, dfs1(y));
  len[x] =
      max(__builtin_ctz(p) -
              max(__builtin_ia32_bsrsi(p) - __builtin_popcount(p) - ret, 0ll),
          0ll);
  return ret + 1;
}
void dfs2(int x) {
  int m = len[x];
  fth[x].resize(m + 1), tr[x].resize(m + 1, {1, 0, 0, 0});
  int f = fth[x][0] = fa[x];
  func g = tr[x][0] = vl[x];
  for (int j = 0; j < m && f; ++j)
    tr[x][j + 1] = g += tr[f][j], fth[x][j + 1] = f = fth[f][j];
  for (int y : es[x]) dfs2(y);
}
void init() {
  int h[N];
  for (int i = 1; i <= n; ++i) cin >> h[i];
  vl[1] = {h[1], A, h[1], A};
  for (int i = 2, a, v; i <= n; ++i) {
    cin >> fa[i] >> a >> v;
    if (es[fa[i]].push_back(i); !a) {
      int xl = max(-A - min(v, 0ll), h[i]), xr = A - max(v, 0ll);
      vl[i] = {xl, xr, xl + v, xr + v};
    } else {
      int xl = h[i], xr = A / v;
      vl[i] = {xl, xr, xl * v, xr * v};
    }
  }
}
int calc(int x, int h) {
  while (x) {
    if (func f = tr[x].back(); h >= f.xl && h <= f.xr) {
      h = f(h), x = fth[x].back();
      continue;
    }
    for (int i = len[x] - 1; ~i && x; --i)
      if (func f = tr[x][i]; h >= f.xl && h <= f.xr) h = f(h), x = fth[x][i];
    break;
  }
  return x;
}
signed main() {
  cin.tie(nullptr)->sync_with_stdio(false);
  cin >> n >> m, init(), dfs1(1), dfs2(1);
  for (int i = 1, s, c; i <= m; ++i) {
    cin >> s >> c;
    int x = calc(c, s);
    ++cnt[x], ans[i] = d[c] - d[x];
  }
  for (int i = 1; i <= n; ++i) cout << cnt[i] << '\n';
  for (int i = 1; i <= m; ++i) cout << ans[i] << '\n';
  return cout << flush, 0;
}

__builtin_popcount 在现代硬件上可以视为 O(1)。有 WRM 洁癖的话可以通过预处理解决。

魔怔的背后,有老哥能看出这东西的心路与来源吗(

其实是把某个挺典的东西包装了一下,然后就变得非常奇异搞笑!!1