当前位置:网站首页>【洛谷】P3919 【模板】可持久化线段树1(可持久化数组)
【洛谷】P3919 【模板】可持久化线段树1(可持久化数组)
2022-07-26 04:58:00 【记录算法题解】
题目地址:
https://www.luogu.com.cn/problem/P3919
题目背景:
标题即题意
有了可持久化数组,便可以实现很多衍生的可持久化功能(例如:可持久化并查集)
题目描述:
如题,你需要维护这样的一个长度为 N N N的数组,支持如下几种操作:
1.在某个历史版本上修改某一个位置上的值
2.访问某个历史版本上的某一位置的值
此外,每进行一次操作(对于操作 2 2 2,即为生成一个完全一样的版本,不作任何改动),就会生成一个新的版本。版本编号即为当前操作的编号(从 1 1 1开始编号,版本 0 0 0表示初始状态数组)
输入格式:
输入的第一行包含两个正整数 N , M N, M N,M, 分别表示数组的长度和操作的个数。
第二行包含 N N N个整数,依次为初始状态下数组各位的值(依次为 a i a_i ai, 1 ≤ i ≤ N 1 \leq i \leq N 1≤i≤N)。
接下来 M M M行每行包含 3 3 3或 4 4 4个整数,代表两种操作之一( i i i为基于的历史版本号):
1.对于操作 1 1 1,格式为 v i 1 l o c i v a l u e i v_i \ 1 \ {loc}_i \ {value}_i vi 1 loci valuei,即为在版本 v i v_i vi的基础上,将 a l o c i a_{ {loc}_i} aloci修改为 v a l u e i {value}_i valuei;
2.对于操作 2 2 2,格式为 v i 2 l o c i v_i \ 2 \ {loc}_i vi 2 loci,即访问版本 v i v_i vi中的 a l o c i a_{ {loc}_i} aloci的值,生成一样版本的对象应为 v i v_i vi
输出格式:
输出包含若干行,依次为每个操作 2 2 2的结果。
数据范围:
对于 30 % 30\% 30%的数据: 1 ≤ N , M ≤ 10 3 1 \leq N, M \leq {10}^3 1≤N,M≤103
对于 50 % 50\% 50%的数据: 1 ≤ N , M ≤ 10 4 1 \leq N, M \leq {10}^4 1≤N,M≤104
对于 70 % 70\% 70%的数据: 1 ≤ N , M ≤ 10 5 1 \leq N, M \leq {10}^5 1≤N,M≤105
对于 100 % 100\% 100%的数据: 1 ≤ N , M ≤ 10 6 , 1 ≤ l o c i ≤ N , 0 ≤ v i < i , − 10 9 ≤ a i , v a l u e i ≤ 10 9 1 \leq N, M \leq {10}^6, 1 \leq {loc}_i \leq N, 0 \leq v_i < i, -{10}^9 \leq a_i, {value}_i \leq {10}^9 1≤N,M≤106,1≤loci≤N,0≤vi<i,−109≤ai,valuei≤109
询问生成的版本是指你访问的那个版本的复制
可以用主席树,即可持久化线段树,来维护这个数组,参考https://blog.csdn.net/qq_46105170/article/details/119052276。这里只需要做单点修改和单点查询即可。代码如下:
#include <algorithm>
#include <iostream>
using namespace std;
const int N = 1e6 + 10;
int n, m;
int a[N];
struct Node {
int l, r, v;
} tr[(N << 2) + N * 20];
int root[N], idx;
int build(int l, int r) {
int p = ++idx;
if (l == r) tr[p].v = a[l];
else {
int mid = l + r >> 1;
tr[p].l = build(l, mid), tr[p].r = build(mid + 1, r);
}
return p;
}
// 更新数组的第k个位置为v,并产生新的版本,返回新版本的树根
int update(int p, int l, int r, int k, int v) {
int q = ++idx;
tr[q] = tr[p];
if (l == r) tr[q].v = v;
else {
int mid = l + r >> 1;
if (k <= mid) tr[q].l = update(tr[q].l, l, mid, k, v);
else tr[q].r = update(tr[q].r, mid + 1, r, k, v);
}
return q;
}
// 查询树根p对应的那个版本中第k个位置的数是多少
int query(int p, int l, int r, int k) {
if (l == r) return tr[p].v;
int mid = l + r >> 1;
if (k <= mid) return query(tr[p].l, l, mid, k);
else return query(tr[p].r, mid + 1, r, k);
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
root[0] = build(1, n);
for (int i = 1; i <= m; i++) {
int vi, op, loc, val;
scanf("%d%d%d", &vi, &op, &loc);
if (op == 1) {
scanf("%d", &val);
root[i] = update(root[vi], 1, n, loc, val);
} else {
printf("%d\n", query(root[vi], 1, n, loc));
// 查询完之后,新版本树根等于被查询版本的树根
root[i] = root[vi];
}
}
}
预处理时间复杂度 O ( N ) O(N) O(N),每次询问时间 O ( log n ) O(\log n) O(logn),空间 O ( N + M log N ) O(N+M\log N) O(N+MlogN)。
边栏推荐
- Excel VBA:按日期汇总计算输出结果(sumif)
- Codeforces Round #807 (Div. 2)
- Seata两阶段提交AT详解
- Fill in the vacancy, and fill in later
- How to build an automated testing framework?
- What is the real HTAP? (1) Background article
- Leetcode - monotone stack and monotone queue
- 【语义分割】2018-DeeplabV3+ ECCV
- C language -- string function, memory function collection and Simulation Implementation
- Vector explanation and iterator failure
猜你喜欢

Database startup message: ora-29702: error occurred in cluster group service

"Game engine light in light out" 4. shader

Switch to router technology: OSPF single zone configuration, OSPF multi zone and end zone
![[semantic segmentation] 2018-deeplabv3+ ECCV](/img/c9/d1e2d7e63df8db2a7fa2bde31b10f7.png)
[semantic segmentation] 2018-deeplabv3+ ECCV

UE4 switching of control rights of multiple roles

Codeforces Round #807 (Div. 2)

创建MySQL数据库的两种方式

Tonight! Stonedb is officially open source. Please check this strategy~

Axi protocol (4): signals on the Axi channel

Array sort 1
随机推荐
How to connect tdengine through idea database management tool?
Torch slice maintenance
ES6 modularization +commonjs
2022 Hangdian multi school DOS card (line segment tree)
Building blocks for domestic databases, stonedb integrated real-time HTAP database is officially open source!
【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(八)
关于负数表示的数值为什么比整数大1?
What are the restrictions on opening futures accounts? Where is the safest place to open an account?
时代潮流-云原生数据库的崛起
can 串口 can 232 can 485 串口转CANbus总线网关模块CAN232/485MB转换器CANCOM
QT compilation error sorting and remote module Download
Database startup message: ora-29702: error occurred in cluster group service
UE4 two ways to obtain player control
Rman-06031 cannot convert database keywords
Codeforces Round #807 (Div. 2)
2022河南萌新联赛第(三)场:河南大学 L - 合成游戏
UE4 displays text when it is close to the object, and disappears when it is far away
Codeforces Round #807 (Div. 2)
滑动窗口——leetcode题解
补位,稍后补上