跳到正文

个人记录

P10218 魔法手帐

xyf约 9 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月28日
篇幅
约 9 分钟

版本

  1. v1当前

省选前做的,跟已知题解不太一样,写个另解。

先考虑一个 O(n⋅poly(k))O(n\cdot\text{poly}(k)) 的做法。考虑二分,由于 ai+x≥ai⊕xa_i+x\ge a_i\oplus x,答案可以 ≥B\ge B 的充要条件是存在 x∈[0,2k)x\in[0,2^k),使得 min⁡ai+x≥B\min a_i+x\ge B 且 ∑i=1n[ai⊕x<B]bi≤m\sum\limits_{i=1}^n[a_i\oplus x< B]b_i\le m。

这个时候可以直接维护,算出满足 ∑i=1n[ai⊕x<B]bi≤m\sum\limits_{i=1}^n[a_i\oplus x< B]b_i\le m 的 xx 的最大值 x′x',判断一下是否有 x′≥B−min⁡aix'\ge B-\min a_i 即可。问题的关键变为如何求出 x′x'。

可以考虑对于每个 ii,把满足 ai⊕x<Ba_i\oplus x< B 的 xx 加上 bib_i,这容易让人联想到数位 dp,使用数位 dp 与 01-Trie 将这样的 xx 拆成 O(k)O(k) 个子树,相当于在这几个子树的根上打一个 +bi+b_i 的标记,那么对于一个 xx,∑i=1n[ai⊕x<B]bi\sum\limits_{i=1}^n[a_i\oplus x< B]b_i 的值就是其根链上标记的和,共有 O(nk)O(nk) 个标记。假设 01-Trie 是满的,那么整个 01-Trie 上的叶子可以分为 O(nk)O(nk) 个子树,每个子树内叶子 xx 的 ∑i=1n[ai⊕x<B]bi\sum\limits_{i=1}^n[a_i\oplus x< B]b_i 值全部相同。那么可以做到 O(nk2)O(nk^2) 的复杂度。

后面就是比较意识流的一步了……回想数位 dp 的过程,我们是如何把 x⊕A<Bx\oplus A<B 的所有 xx 分成 kk 个子树的:对于第 ii 位,若 BB 的二进制的第 ii 位为 11,则对于所有满足下列条件的 xx 会有 x⊕A<Bx\oplus A<B:

  • xx 高于 ii 的位全部与 A⊕BA\oplus B 相同;
  • xx 的第 ii 位为 AA 的第 ii 位。

并且这样不会重复。还是有点抽象,再往 01-Trie 方向想一下:相当于 01-Trie 上 A⊕BA\oplus B 的根链上,对于 BB 的第 ii 位,若这一位不是 00,那么 A⊕BA\oplus B 在这一层上的祖先的兄弟需要打一个标记。

考虑逐位确定 BB,如果现在在试图确定 BB 的第 ii 位,那么总共有这样的 nn 条根链,目前延伸到第 ii 层,并且已经打了一些标记,想要确定在假设 BB 的第 ii 位是 11 情况下的 x′x' 值是多少。

在假设 BB 的第 ii 位是 11 时,会新增 nn 个同一层的标记,并且每个根链会向指定方向多延伸一段。此时直接求出 x′x' 是相当困难的,可以利用以前的信息。

注意到在确定完之前的位后,以后所有标记只有可能出现在所有根链延伸到的 O(n)O(n) 个点的子树内,其他的同层子树内不会再打标记了,也就是说除了这 O(n)O(n) 个点以外,剩下的同层的点的子树内部的 ∑i=1n[ai⊕x<B]bi\sum\limits_{i=1}^n[a_i\oplus x< B]b_i 值已经确定不可能再改变了,可以把其中的 ≤m\le m 的部分直接贡献到一个“不重要部分”的答案里面,记为 xdx_d。对于剩下的 O(n)O(n) 个点,暴力 O(n)O(n) 求出内部的 xax_a,则 x′=max⁡{xd,xa}x'=\max\{x_d,x_a\}。

在向下走一层的时候,一些点会失去部分子树,把这些子树内最大的点贡献到 xdx_d 里面。最终复杂度就只有 O(nk)O(nk),代码也没什么细节。

可能有点意识流,如果有看不懂的欢迎和我私信对线。

C++
#include <iostream>
#include <vector>
#include <algorithm>

using LLL = __int128_t;
LLL read(){
  LLL x;
	char c; bool f = 0;
	while(((c = getchar()) < '0' || c > '9') && c != '-');
	if(c == '-'){f = 1; c = getchar();}
	x = c - '0';
	while((c = getchar()) >= '0' && c <= '9')x = x * 10 + c - '0';
	if(f) x = -x;
  return x;
}
void write(LLL x){
	if(x < 0){putchar('-'); x = -x;}
	if(x > 9)write(x / 10);
	putchar(x % 10 + '0');
}

int n, k;
long long m;
LLL a[100007];
int b[100007];
std::pair<LLL, int> ab[100007];
struct vector {
  int l, r;
  vector() { l = n + 1, r = 0; }
  inline void push_back(int i) { l = std::min(l, i), r = std::max(r, i); }
  inline bool empty() { return l > r; }
};

int nodecnt;
vector id[100007];
vector nid[100007];
long long w[100007];
long long nw[100007];
LLL loc[100007];
LLL nloc[100007];
int H;
LLL max;
LLL chk() {
  auto ans = max;
  for (int i = 1; i <= nodecnt; ++i) {
    long long S[2] = {w[i], w[i]};
    for (int j = id[i].l; j <= id[i].r; ++j) {
      S[(a[j] >> H) & 1] += b[j];
    }
    auto s = loc[i];
    for (int j = 0; j < 2; ++j) {
      if (S[j] <= m) {
        ans = std::max(ans, s + (LLL(j + 1) << H) - 1);
      }
    }
  }
  return ans;
}
void nxt(int bit) {
  int nnodecnt = 0;
  for (int i = 1; i <= nodecnt; ++i) {
    vector son[2] = {vector(), vector()};
    long long S[2] = {w[i], w[i]};
    for (int j = id[i].l; j <= id[i].r; ++j) {
      son[((a[j] >> H) & 1) ^ bit].push_back(j);
      if (bit) {
        S[(a[j] >> H) & 1] += b[j];
      }
    }
    auto s = loc[i];
    for (int j = 0; j < 2; ++j) {
      if (!son[j].empty()) {
        ++nnodecnt;
        nid[nnodecnt] = son[j], nw[nnodecnt] = S[j];
        nloc[nnodecnt] = loc[i] ^ (LLL(j) << H);
      } else if (S[j] <= m) {
        max = std::max(max, s + (LLL(j + 1) << H) - 1);
      }
    }
  }
  nodecnt = nnodecnt;
  for (int i = 1; i <= nodecnt; ++i) {
    id[i] = nid[i], w[i] = nw[i];
    loc[i] = nloc[i];
  }
}
void solve() {
  n = read(), m = read(), k = read();
  nodecnt = 1;
  loc[1] = 0;
  w[1] = 0;
  id[1] = vector();
  for (int i = 1; i <= n; ++i) {
    a[i] = read();
  }
  long long ss = 0;
  for (int i = 1; i <= n; ++i) {
    b[i] = read();
    id[1].push_back(i);
    ss += b[i];
    ab[i] = {a[i], b[i]};
  }
  std::sort(ab + 1, ab + n + 1);
  for (int i = 1; i <= n; ++i) {
    a[i] = ab[i].first, b[i] = ab[i].second;
  }
  LLL min = a[1];
  if (ss <= m) {
    write(min + (LLL(1) << k) - 1), putchar('\n');
    return;
  }
  max = -1;
  LLL ans = 0;
  for (H = k - 1; H >= 0; --H) {
    auto c = chk();
    if (c != -1 && c + min >= ans + (LLL(1) << H)) {
      nxt(1);
      ans += LLL(1) << H;
    } else {
      nxt(0);
    }
  }
  write(ans), putchar('\n');
}

int main() {
  // std::freopen("xor.in", "r", stdin);
  // std::freopen("xor.out", "w", stdout);
  int t = read();
  t = read();
  while (t--) {
    solve();
  }
  return 0;
}
原文