背景
我是萌新初学者,看注册时间应该也能看得出来。
这篇文章分享了自己的见解,并且持续更新。由于能力不足,还请大佬们指教!
好题分享
P9986 [Ynoi2079] r2pspc
题目大意
给定序列 ,共 次查询,每次查询问 的二进制表示中 的个数。(popcount)
。
分析 + 思路
提供一种暴力做法,虽然能过。
看的第一眼同桌说莫队,那显然是莫队,毕竟是无修改并且可以离线,然后区间扩展的维护感觉能够做到。
那么我们要去维护这样一个二进制数,支持单点加减 。我们考虑这样对序列有什么形象直观的影响。这里给出一个例子,读者自行模拟一下。
数:,从左往右是第 到第 位。操作(并列):,其中 表示对第 位的 操作。
那么得出结论:加操作就是在左边找到第一个 位置(显然一定存在),然后赋值为 ,并将中间可能存在的连续 段赋值为 ,减法类似。那么我们可以暴力地去维护这样序列(暴力查找并修改),通过离散化缩小值域,这样的时间复杂度看似 ,但根据 lxl 老师和 solution 实则是 的,需要进行摊还分析,感性理解的话:实际上看上去就跑不满,毕竟一次消一片。
可以通过压位技巧优化为 ,实际上暴力都有 分,应该能跑得飞快。
代码
我就实现了 。主要是因为这份代码能让读者更好地理解思路,毕竟压位技巧加上后就不好理解算法过程了。
提交记录:提交记录,核心代码如下。
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
题意简述
不再赘述。
题目个人分析
这里简述一下我的思路,完整阐述一下我的逻辑链。
首先观察到了每个点的答案都是在子树外任选的,于是考虑找到整棵树满足 最大的点对 ,那么除了 到根路径上的点外其它点的答案就是这个。
以 为例,考虑如何对这条链求出答案。我们模拟一个扫描线的过程从 扫到 ,逐步确定答案。假设从 到 ,我们发现能对 贡献的点对一定能对 贡献,且增加的点是 子树中除了 以外的点。那么我们只需要维护如下过程:维护一个点集,支持加入点(显然次数是 级别),并且查询最大两两异或值。
考虑维护一个 trie,从高位往低位建立。那么新加进来一个值 如何找到其中与 异或最大的 ?显然可以采取一个贪心的策略,从高位往低位每次能 则 ,否则为 ,发现这其实是一个在 trie 树上二分的过程,反应的是贪心匹配高位的本质。
此题便做完了,时间复杂度是 ,瓶颈在于对 trie 的维护和查询。
代码
这题细节颇多,我决定给出完整代码。关键部分我都有注释解释。
#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;
}总结
关键在于 的绝大部分贡献!
后记
希望大家能点一个赞,我真心地想为大家呈现优质的题目。