20240715 数据结构小练
数据结构主要是码量较大,调试难度较高总是RE。
P1001 A + B problem
思路略
这个题可以用LCT做。这个题主要是用来找手感的吧。
#include <stdio.h>
int main() {
int a, b;
scanf("%d%d", &a, &b);
printf("%d", a + b);
return 0;
}P6327 区间加区间 sin 和
线段树模板。
由于
所以
区间 也可以像这样展开,因此我们用线段树维护区间 和与区间 和。
线段树打法不重要,关键看维护的思路,因此通过此题的代码采用了 atcoder 提供的 lazy_segtree 模板。
#include <bits/extc++.h>
struct S {
double sinsum, cossum;
};
S op(S a, S b) {
return S{a.sinsum + b.sinsum, a.cossum + b.cossum};
}
S e() {
return S{0, 0};
}
using F = double;
S mapping(F f, S x) {
return S{
sin(f) * x.cossum + cos(f) * x.sinsum,
cos(f) * x.cossum - sin(f) * x.sinsum};
}
F composition(F f, F g) {
return f + g;
}
F id() {
return 0;
}
std::vector<S> v;
int n, m;
double in;
int opt, l, r;
int main() {
std::cin >> n;
v.resize(n);
for (int i = 0; i < n; ++i) {
std::cin >> in;
v[i] = S{sin(in), cos(in)};
}
atcoder::lazy_segtree <S, op, e, F, mapping, composition, id> t(v);
std::cin >> m;
while (m--) {
std::cin >> opt >> l >> r;
--l;
if (opt == 1) {
std::cin >> in;
t.apply(l, r, in);
} else {
std::cout << std::fixed << std::setprecision(1) << t.prod(l, r).sinsum << std::endl;
}
}
return 0;
}我觉得有必要总结一下类似的题目的思路:什么样的信息可以用线段树维护?什么样的问题可以用线段树解决呢?
lazy_segtree 的定义方式就是一个提示。做这种题目时,我们首先需要明确我们每个 节点 要维护的信息 S,以及需要实现的 操作 的类型 F。分别对于 S 和 F,我们需要找到一个 合并函数 和一个 单位元,这其实也是线段树中 结合律 的一种体现。对于 S 和 F 之间,我们需要明确任意一个节点执行任意一种操作后会变成什么样子。如果这些问题能够自洽,那么这道题目就已经解决了。
P3402 可持久化并查集
回忆一下并查集是怎么存储的——其实它的主体是一个数组。一个并查集的所有信息都在一个数组里。可持久化数组是我们已经解决了的问题,可以用可持久化线段树维护(或者采用 rope,这里没有划掉,因为是真的很好用)。
rope 做法解析
这题我的可持久化线段树内存占用比 rope 大,可见 rope 还是非常优秀的。rope 虽然是块状链表,实际上实现了可持久化数组的功能。
注意,对于可持久化题目我们要尽可能减少修改操作的个数。因为可持久化数据结构的核心就在于统一记录相同的信息,如果相同的信息太少我们就无计可施了。
对于并查集,有的做法是在寻找祖先节点时进行路径压缩来实现树不退化为链的。但这种方法会产生大量的修改操作。这里我们尽量要让每一次并查集的合并操作只产生一两次数组修改,可以采用启发式合并的方法:在合并两个节点的时候就决定好谁当爹谁当儿子,以后就再也不改了。决定的方法很简单,一般让子树更大的当爹,这样可以让树更胖(感性理解),而不是更高,有利于复杂度优化。
其他的,将并查集的数组改为 rope,然后正常操作即可通过此题:
#include <bits/extc++.h>
using namespace __gnu_cxx;
using namespace std;
constexpr int maxn = 2e5 + 5;
unsigned f[maxn];
unsigned r[maxn];
void read(unsigned &x) {
x = 0;
char ch = getchar();
while (ch < 47)
ch = getchar();
while (ch > 47)
x = (x << 1) + (x << 3) + (ch & 15), ch = getchar();
}
unsigned find(rope<unsigned> *fa, const unsigned &i) {
unsigned f = (*fa)[i];
return f == i ? i : find(fa, f);
}
void merge(rope<unsigned> *fa, unsigned &a, unsigned &b) {
a = find(fa, a);
b = find(fa, b);
if (a == b) {
return;
}
if (r[a] > r[b]) {
fa->replace(b, a);
} else {
if (r[a] == r[b]) {
r[b]++;
}
fa->replace(a, b);
}
}
signed main() {
unsigned n, m, op[maxn], a[maxn], b[maxn], d[maxn];
read(n);
read(m);
rope<unsigned> *fa[m + 1];
for (unsigned i = 1; i <= n; i++) {
f[i] = i;
}
f[0] = 1;
fa[0] = new rope<unsigned>(f);
for (unsigned i = 1; i <= m; i++) {
read(op[i]);
if (op[i] == 2) {
read(a[i]);
++d[a[i]];
} else {
read(a[i]);
read(b[i]);
++d[i - 1];
}
}
for (unsigned i = 1; i <= m; i++) {
switch (op[i]) {
case 1:
fa[i] = new rope(*fa[i - 1]);
--d[i - 1];
if (!d[i - 1]) delete fa[i - 1];
merge(fa[i], a[i], b[i]);
break;
case 2:
fa[i] = new rope(*fa[a[i]]);
--d[a[i]];
if (!d[a[i]]) delete fa[a[i]];
break;
default:
fa[i] = new rope(*fa[i - 1]);
--d[i - 1];
if (!d[i - 1]) delete fa[i - 1];
putchar(find(fa[i], a[i]) == find(fa[i], b[i]) ? '1' : '0');
puts("");
}
}
return 0;
}注意:rope 的空间优势有一部分来源于实现了回收机制,因此对于不用的信息可以统计出来回收掉。
离线做法
有一句名言:对于一个可持久化数据结构题,如果它不强制在线,那么它就不是一道可持久化数据结构题。
什么意思呢?
我们考虑建立一棵 “时光树”,这棵树的每个节点代表操作序列中的某一个时刻,树上连边表示一次操作的结果会被另一次操作直接使用、作为底板。
好像有点抽象,详细来说。
-
比如对于不可持久化的题,每一个操作的副本其实都是前一次操作后的结果,那么我们建出来的树其实就是相邻的两次操作会连边,最后形成一个链表;我们在处理的时候也是按照从头到尾一遍扫完的顺序,其实也就是按照时光树的先序遍历来处理的。
-
同理,我们把这种思想利用到可持久化题目上。不过这回连边有些不同,比如,第 3 次操作是要在第 1 次操作的结果上叠加的,那么我们的时光树就会把 1 号节点和 3 号节点连边。在处理的时候,我们还是先序遍历这棵树,处理完 1 号节点的时候我们顺便跳到 3 号节点,把 3 号节点以及其子树的操作都处理好了之后,我们把 3 号节点的操作撤回,这样我们就可以再去处理其他由 1 号节点操作结果当底板的其他操作了。
由于需要建立时光树,我们的操作必须离线。
#include <bits/stdc++.h>
using namespace std;
constexpr int maxn = 300005;
int n, m;
int opt[maxn], a[maxn], b[maxn];
int ans[maxn];
struct DSU {
int fa[maxn], siz[maxn], sta[maxn], top;
void init() {
for (int i = 1; i <= n; ++i)
fa[i] = i, siz[i] = 1;
}
int getf(const int x) {
return x == fa[x] ? x : getf(fa[x]);
}
void merge(const int x, const int y) {
int fx = getf(x), fy = getf(y);
if (fx == fy) return;
if (siz[fx] < siz[fy]) swap(fx, fy);
siz[fx] += siz[fy];
fa[fy] = fx;
sta[++top] = fy;
}
void back() {
const int y = sta[top--];
siz[fa[y]] -= siz[y];
fa[y] = y;
}
void turn_back(const int t) {
while (top > t) back();
}
} T;
vector<int> edge[maxn];
void add(const int a, const int b) {
edge[a].push_back(b);
}
void dfs(const int x) {
const int pre = T.top;
if (opt[x] == 3) ans[x] = (T.getf(a[x]) == T.getf(b[x]));
if (opt[x] == 1) T.merge(a[x], b[x]);
for (const int &nxt: edge[x])
dfs(nxt);
T.turn_back(pre);
}
int main() {
cin >> n >> m;
T.init();
for (int i = 1; i <= m; ++i) {
cin >> opt[i] >> a[i];
if (opt[i] != 2) {
cin >> b[i];
add(i - 1, i);
} else {
add(a[i], i);
}
}
dfs(0);
for (int i = 1; i <= m; ++i) {
if (opt[i] == 3) {
cout << ans[i] << endl;
}
}
return 0;
}可持久化线段树维护数组
这是正解。也是唯一一个我没有通过的做法。
和上面用 rope 的写法类似,把数组可持久化,然后正常维护并查集即可。
代码还没调出来,还很长,就不贴了。主函数和并查集的部分其实和 rope 版本大同小异,采用启发式合并。
启示
这题给了我们两个启示:当遇到一个不那么直接的问题,可以把它转化成已经解决的问题来处理;对于一个数据结构的可持久化,我们其实是在考虑空间上的问题,也就是说我们要关注的其实是存储结构,就像并查集,我们本质上只关心那个数组,它的成员函数用不着我们可持久化。
P3380 【模板】树套树
顾名思义,就是一个树形结构的节点上维护另一个树。
这道题主流做法有很多,外部维护的树一般是线段树或者树状数组,毕竟这是一个树形结构;内部一般要维护一个平衡树、权值线段树或者vector,来维护一个数集,因为要查询第 k 小。
为了代码好写,下面提供三种最短搭配:
暴力最短 —— vector
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e4 + 5;
int n, a[maxn];
int m, opt[maxn], L[maxn], R[maxn], K[maxn];
int numset[2 * maxn], Hash_cnt;
int Hash(int x) {
return lower_bound(numset + 1, numset + Hash_cnt + 1, x) - numset;
}
int mp(int x) {
if (x > -2147483647 && x < 2147483647) {
return numset[x];
} else {
return x;
}
}
int nodecnt;
vector<int> root[maxn];
int lowbit(int x) {
return x & (-x);
}
int rk(int x, int l, int r) {
--l;
int ans = 0;
while (r) {
ans += lower_bound(root[r].begin(), root[r].end(), x) - root[r].begin();
r -= lowbit(r);
}
while (l) {
ans -= lower_bound(root[l].begin(), root[l].end(), x) - root[l].begin();
l -= lowbit(l);
}
return ans + 1;
}
int key(int p, int rangel, int ranger) {
if (p <= 0) return -2147483647;
if (p > (ranger - rangel + 1)) return 2147483647;
int l = 1, r = Hash_cnt, mid;
while (l != r) {
mid = ((l + r) >> 1) + 1;
if (rk(mid, rangel, ranger) <= p) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
int main(int argc, char const *argv[]) {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
numset[++Hash_cnt] = a[i];
}
for (int i = 1; i <= m; i++) {
cin >> opt[i] >> L[i];
if (opt[i] != 3) cin >> R[i];
cin >> K[i];
if (opt[i] != 2) {
numset[++Hash_cnt] = K[i];
}
}
sort(numset + 1, numset + Hash_cnt + 1);
Hash_cnt = unique(numset + 1, numset + Hash_cnt + 1) - numset - 1;
for (int i = 1; i <= n; i++) {
a[i] = Hash(a[i]);
}
for (int i = 1; i <= m; i++) {
if (opt[i] != 2) {
K[i] = Hash(K[i]);
}
}
for (int i = 1; i <= n; i++) {
int le = i - lowbit(i) + 1, ri = i;
for (int j = le; j <= ri; j++) {
root[i].insert(lower_bound(root[i].begin(), root[i].end(), a[j]), a[j]);
}
}
for (int i = 1; i <= m; i++) {
if (opt[i] == 1) {
cout << rk(K[i], L[i], R[i]) << endl;
} else if (opt[i] == 2) {
cout << mp(key(K[i], L[i], R[i])) << endl;
} else if (opt[i] == 3) {
int &ys = a[L[i]], hl = K[i];
for (int pos = L[i]; pos <= n; pos += lowbit(pos)) {
root[pos].erase(lower_bound(root[pos].begin(), root[pos].end(), ys));
root[pos].insert(lower_bound(root[pos].begin(), root[pos].end(), hl), hl);
}
ys = hl;
} else if (opt[i] == 4) {
cout << mp(key(rk(K[i], L[i], R[i]) - 1, L[i], R[i])) << endl;
} else {
cout << mp(key(rk(K[i] + 1, L[i], R[i]), L[i], R[i])) << endl;
}
}
return 0;
}
// 树状数组套 vector科技最短 —— 树状数组套 pbds
#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
const int maxn = 5e4 + 5;
int n, a[maxn];
int m, opt[maxn], L[maxn], R[maxn], K[maxn];
int numset[2 * maxn], Hash_cnt;
int Hash(int x) {
return lower_bound(numset + 1, numset + Hash_cnt + 1, x) - numset;
}
int mp(int x) {
if (x > -2147483647 && x < 2147483647) {
return numset[x];
} else {
return x;
}
}
int nodecnt;
__gnu_pbds::tree<
int,
__gnu_pbds::null_type,
std::less_equal<int>,
__gnu_pbds::rb_tree_tag,
__gnu_pbds::tree_order_statistics_node_update>
root[maxn];
int lowbit(int x) {
return x & (-x);
}
int rk(int x, int l, int r) {
--l;
int ans = 0;
while (r) {
ans += root[r].order_of_key(x);
r -= lowbit(r);
}
while (l) {
ans -= root[l].order_of_key(x);
l -= lowbit(l);
}
return ans + 1;
}
int key(int p, int rangel, int ranger) {
if (p <= 0) return -2147483647;
if (p > (ranger - rangel + 1)) return 2147483647;
int l = 1, r = Hash_cnt, mid;
while (l != r) {
mid = ((l + r) >> 1) + 1;
if (rk(mid, rangel, ranger) <= p) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
int main(int argc, char const *argv[]) {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
numset[++Hash_cnt] = a[i];
}
for (int i = 1; i <= m; i++) {
cin >> opt[i] >> L[i];
if (opt[i] != 3) cin >> R[i];
cin >> K[i];
if (opt[i] != 2) {
numset[++Hash_cnt] = K[i];
}
}
sort(numset + 1, numset + Hash_cnt + 1);
Hash_cnt = unique(numset + 1, numset + Hash_cnt + 1) - numset - 1;
for (int i = 1; i <= n; i++) {
a[i] = Hash(a[i]);
}
for (int i = 1; i <= m; i++) {
if (opt[i] != 2) {
K[i] = Hash(K[i]);
}
}
for (int i = 1; i <= n; i++) {
int le = i - lowbit(i) + 1, ri = i;
for (int j = le; j <= ri; j++) {
root[i].insert(a[j]);
}
}
for (int i = 1; i <= m; i++) {
if (opt[i] == 1) {
cout << rk(K[i], L[i], R[i]) << endl;
} else if (opt[i] == 2) {
cout << mp(key(K[i], L[i], R[i])) << endl;
} else if (opt[i] == 3) {
int &ys = a[L[i]], hl = K[i];
for (int pos = L[i]; pos <= n; pos += lowbit(pos)) {
root[pos].erase(root[pos].upper_bound(ys));
root[pos].insert(hl);
}
ys = hl;
} else if (opt[i] == 4) {
cout << mp(key(rk(K[i], L[i], R[i]) - 1, L[i], R[i])) << endl;
} else {
cout << mp(key(rk(K[i] + 1, L[i], R[i]), L[i], R[i])) << endl;
}
}
return 0;
}
// 树状数组套 pbds tree正解最短 —— 树状数组套权值线段树
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e4 + 5;
int n, a[maxn];
int m, opt[maxn], L[maxn], R[maxn], K[maxn];
int numset[2 * maxn], Hash_cnt;
int Hash(int x) {
return lower_bound(numset + 1, numset + Hash_cnt + 1, x) - numset;
}
struct Node {
int lson, rson;
int size;
} node[maxn * 256];
int nodecnt, root[maxn];
int lowbit(int x) {
return x & (-x);
}
int _rk(int x, int tr) {
int now = root[tr];
int l = 1, r = Hash_cnt, mid, ans = 0;
while (l != r && now != 0) {
mid = (l + r) >> 1;
if (x <= mid) {
r = mid;
now = node[now].lson;
} else {
ans += node[node[now].lson].size;
l = mid + 1;
now = node[now].rson;
}
}
return ans;
}
int rk(int x, int l, int r) {
--l;
int ans = 0;
while (r) {
ans += _rk(x, r);
r -= lowbit(r);
}
while (l) {
ans -= _rk(x, l);
l -= lowbit(l);
}
return ans + 1;
}
int key(int p, int rangel, int ranger) {
if (p <= 0) return -2147483647;
if (p > (ranger - rangel + 1)) return 2147483647;
int l = 1, r = Hash_cnt, mid;
while (l != r) {
mid = ((l + r) >> 1) + 1;
if (rk(mid, rangel, ranger) <= p) {
l = mid;
} else {
r = mid - 1;
}
}
return l;
}
void ins(int x, int tr, int gs) {
int l = 1, r = Hash_cnt, now = root[tr], mid;
node[now].size += gs;
while (l != r) {
mid = (l + r) >> 1;
int &nxtn =
(x <= mid) ?
(r = mid, node[now].lson) :
(l = mid + 1, node[now].rson);
if (nxtn == 0) {
nxtn = ++nodecnt;
node[nxtn] = {0, 0, 0};
}
node[nxtn].size += gs;
now = nxtn;
}
}
int mp(int x) {
if (x > -2147483647 && x < 2147483647) {
return numset[x];
} else {
return x;
}
}
int main(int argc, char const *argv[]) {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
cin >> a[i];
numset[++Hash_cnt] = a[i];
}
for (int i = 1; i <= m; i++) {
cin >> opt[i] >> L[i];
if (opt[i] != 3) cin >> R[i];
cin >> K[i];
if (opt[i] != 2) {
numset[++Hash_cnt] = K[i];
}
}
sort(numset + 1, numset + Hash_cnt + 1);
Hash_cnt = unique(numset + 1, numset + Hash_cnt + 1) - numset - 1;
for (int i = 1; i <= n; i++) {
a[i] = Hash(a[i]);
}
for (int i = 1; i <= m; i++) {
if (opt[i] != 2) {
K[i] = Hash(K[i]);
}
}
node[0] = {0, 0, 0};
for (int i = 1; i <= n; i++) {
root[i] = ++nodecnt;
node[root[i]] = {0, 0, 0};
int le = i - lowbit(i) + 1, ri = i;
for (int j = le; j <= ri; j++) {
ins(a[j], i, 1);
}
}
for (int i = 1; i <= m; i++) {
if (opt[i] == 1) {
cout << rk(K[i], L[i], R[i]) << endl;
} else if (opt[i] == 2) {
cout << mp(key(K[i], L[i], R[i])) << endl;
} else if (opt[i] == 3) {
int &ys = a[L[i]], hl = K[i];
for (int pos = L[i]; pos <= n; pos += lowbit(pos)) {
ins(ys, pos, -1);
ins(hl, pos, 1);
}
ys = hl;
} else if (opt[i] == 4) {
cout << mp(key(rk(K[i], L[i], R[i]) - 1, L[i], R[i])) << endl;
} else {
cout << mp(key(rk(K[i] + 1, L[i], R[i]), L[i], R[i])) << endl;
}
}
}
// 树状数组套权值线段树