跳到正文

算法·理论

Ynoi 好题分享

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

文章信息

发布
2026年2月1日
收录
2026年2月9日
更新
2026年11月17日
篇幅
约 9 分钟
获赞
1

版本

  1. v2当前
  2. v1

背景

我是萌新初学者,看注册时间应该也能看得出来。

这篇文章分享了自己的见解,并且持续更新。由于能力不足,还请大佬们指教!

好题分享

P9986 [Ynoi2079] r2pspc

题目大意

给定序列 a1,…,ana_1,\dots,a_n,共 mm 次查询,每次查询问 ∑i=lr2ai\sum \limits_{i = l} ^ r 2 ^ {a_i} 的二进制表示中 11 的个数。(popcount)

1≤n≤105,1≤m≤106,1≤ai≤109,1≤l≤r≤n1 \le n \le {10} ^ 5, 1 \le m \le {10} ^ 6, 1 \le a_i \le 10 ^ 9, 1 \le l \le r \le n。

分析 + 思路

提供一种暴力做法,虽然能过。

看的第一眼同桌说莫队,那显然是莫队,毕竟是无修改并且可以离线,然后区间扩展的维护感觉能够做到。

那么我们要去维护这样一个二进制数,支持单点加减 11。我们考虑这样对序列有什么形象直观的影响。这里给出一个例子,读者自行模拟一下。

数:000010001110120000100011101_2,从左往右是第 1313 到第 11 位。操作(并列):(2,1),(3,1),(9,−1),(6,−1)(2, 1), (3, 1), (9, -1), (6, -1),其中 (a,b)(a, b) 表示对第 aa 位的 bb 操作。

那么得出结论:加操作就是在左边找到第一个 00 位置(显然一定存在),然后赋值为 11,并将中间可能存在的连续 11 段赋值为 00,减法类似。那么我们可以暴力地去维护这样序列(暴力查找并修改),通过离散化缩小值域,这样的时间复杂度看似 O(nm⋅V)O(n \sqrt{m} \cdot V),但根据 lxl 老师和 solution 实则是 O(nm)O(nm) 的,需要进行摊还分析,感性理解的话:实际上看上去就跑不满,毕竟一次消一片。

可以通过压位技巧优化为 O(nmw)O(\frac{nm}{w}),实际上暴力都有 9494 分,应该能跑得飞快。

代码

我就实现了 O(nm)O(nm)。主要是因为这份代码能让读者更好地理解思路,毕竟压位技巧加上后就不好理解算法过程了。

提交记录:提交记录,核心代码如下。

C++
namespace solve {
    int tot;
    bitset <5000005> b;
    inline void add(const int x) {
        int p = ar[x];
        while (b[p]) --tot, b[p++] = 0;
        return ++tot, b[p] = 1, void();
    }
    inline void del(const int x) {
        int p = ar[x];
        while (!b[p]) ++tot, b[p++] = 1;
        return --tot, b[p] = 0, void();
    }
} using namespace solve;

int l = 1, r = 1;
tot = b[ar[1]] = 1;
for (const array <int, 3> &q : qry) {
    while (l > q[1]) add(--l);
    while (r < q[2]) add(++r);
    while (l < q[1]) del(l++);
    while (r > q[2]) del(r--);
    ans[q[0]] = tot;
}

// 最后输出答案

后记

感谢阅读!计划将 2079 年的 T2 给补了。等待更新!

P8511 TEST_68

题意简述

不再赘述。

题目个人分析

这里简述一下我的思路,完整阐述一下我的逻辑链。

首先观察到了每个点的答案都是在子树外任选的,于是考虑找到整棵树满足 as⊕ata_s \oplus a_t 最大的点对 (s,t)(s, t),那么除了 s,ts, t 到根路径上的点外其它点的答案就是这个。

以 1→s1 \to s 为例,考虑如何对这条链求出答案。我们模拟一个扫描线的过程从 11 扫到 ss,逐步确定答案。假设从 xx 到 yy,我们发现能对 xx 贡献的点对一定能对 yy 贡献,且增加的点是 xx 子树中除了 yy 以外的点。那么我们只需要维护如下过程:维护一个点集,支持加入点(显然次数是 O(n)O(n) 级别),并且查询最大两两异或值。

考虑维护一个 0101 trie,从高位往低位建立。那么新加进来一个值 xx 如何找到其中与 xx 异或最大的 yy?显然可以采取一个贪心的策略,从高位往低位每次能 11 则 11,否则为 00,发现这其实是一个在 trie 树上二分的过程,反应的是贪心匹配高位的本质。

此题便做完了,时间复杂度是 Θ(nlog⁡V)\Theta(n \log V),瓶颈在于对 trie 的维护和查询。

代码

这题细节颇多,我决定给出完整代码。关键部分我都有注释解释。

C++
#include <bits/stdc++.h>
#define r (d - 1) // 这个是个人习惯
using namespace std;
typedef long long ll;

const int N = 500005, T = 44000005; // 要开够!

int n, fa[N];
vector <int> G[N];
ll a[N], ans[N];
deque <int> pl, pr;
bool fl[N];

namespace Trie {
    constexpr int H = 63;
    
    int id, ch[T][2], tag;
    bitset <T> b;
    inline void nw(const int p) { // 动态开点
        if (!ch[p][0]) ch[p][0] = ++id;
        if (!ch[p][1]) ch[p][1] = ++id;
    }

    void insert(int p, int d, const ll val) {
        if (d < H) b[p] = 1;
        if (!d) return void();
        nw(p), insert(ch[p][val >> r & 1ll], r, val);
    }
    ll query(int p, int d, const ll val) {
        if (d < 0) return 0;
        nw(p);
        ll num = val >> r & 1ll ^ 1ll;
        if (b[ch[p][num]]) return query(ch[p][num], r, val) | (num << r);
        return query(ch[p][num ^ 1ll], r, val) | ((num ^ 1ll) << r);
    }
} using namespace Trie;

ll lst;
void dfs(int u, const int &lim) {
    if (u == lim) return void();
    if (tag) lst = max(lst, query(1, H, a[u]) ^ a[u]);
    // 实时更新 trie 树中最大两两异或和
    insert(1, H, a[u]), tag = 1;
    for (const int &v : G[u]) dfs(v, lim);
}
void solve(const deque <int> &pth) {
    id = 1, memset(ch, 0, sizeof ch), b.reset(), lst = tag = 0; // tag 用来维护 trie 树中是否有值
    for (int i = 0, nxt = 1; i < pth.size(); ++i, ++nxt) {
        int x = pth[i];
        if (x != pth[0]) ans[x] = lst;
        if (x != pth.back()) dfs(x, pth[nxt]);
        // 将 x 子树中不在 pth[nxt] 中的点插入进 trie 树,记得插入之前先进行查询
    }
}

int main() {
    cin.tie(0) -> sync_with_stdio(false);
    cin >> n;
    for (int i = 2; i <= n; ++i) cin >> fa[i], G[fa[i]].emplace_back(i);
    for (int i = 1; i <= n; ++i) cin >> a[i];
    int s = 1, t;
    // 对最大点对 (s, t) 的求解
    ll Xor = 0;
    id = 1, insert(1, H, a[1]);
    for (int i = 2; i <= n; ++i) {
        ll res = query(1, H, a[i]);
        if ((res ^ a[i]) > Xor) Xor = res ^ a[i], s = i;
        // 每次查询和 a[i] 异或最大的来更新 s
        insert(1, H, a[i]);
    }
    for (int i = 1; i <= n; ++i) if ((a[i] ^ a[s]) == Xor) t = i; // 确定 s 后再确定 t
    // 把两条链取出来分别求解
    while (s) fl[s] = true, pl.push_front(s), s = fa[s];
    while (t) fl[t] = true, pr.push_front(t), t = fa[t];
    for (int i = 1; i <= n; ++i) if (!fl[i]) ans[i] = Xor;
    solve(pl), solve(pr);
    for (int i = 1; i <= n; ++i) cout << ans[i] << '\n';
    return 0;
}

总结

关键在于 (s,t)(s, t) 的绝大部分贡献!

后记

希望大家能点一个赞,我真心地想为大家呈现优质的题目。

原文
存档评论2 条抓取于