当前位置:网站首页>【洛谷】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)。
边栏推荐
- ES6 modularization +commonjs
- data warehouse
- Ggjj, do you have a look at this problem? Does caching cause cross domain problems?
- Mysql主从同步及主从同步延迟解决方案
- Kubernetes 进阶训练营 调度器
- 有ggjj看看这个问题没,是否缓存导致跨域问题?
- What are the characteristics of the grammar of Russian documents in the translation of scientific papers
- Switch to router technology: OSPF single zone configuration, OSPF multi zone and end zone
- 10、 Interceptor
- 擅长C(暑假每日一题 6)
猜你喜欢

Phaser (I): platform jumping collection game

2022 Henan Mengxin League game (3): Henan University L - synthetic game

User defined type details
![[cloud native | 17] four network modes of container](/img/06/01204436c27b69a5ae6fcb04642c2a.jpg)
[cloud native | 17] four network modes of container

「游戏引擎 浅入浅出」4. 着色器

How to connect tdengine through idea database management tool?

UE4 displays text when it is close to the object, and disappears when it is far away

IEC61131 数据类型与 C#数据类型的对应

Face database collection summary

Principle of image nonlocal mean filtering
随机推荐
New knowledge in big homework
Phaser(一):平台跳跃收集游戏
columns in GROUP BY clause; this is incompatible with sql_ mode=only_ full_ group_ By mysql8.0 solution
AQS唤醒线程的时候为什么从后向前遍历,我懂了
clock_ gettime
To study the trend of open source and gain insight into the future of the industry, stonedb community and the China Academy of communications and communications released the Research Report on the dev
Axi protocol (4): signals on the Axi channel
Sliding window -- leetcode solution
异步时父子线程间的ThreadLocal传递问题
奥特学园ROS笔记--6
Autocomplete prevents the form from automatically filling in
滑动窗口——leetcode题解
2022河南萌新联赛第(三)场:河南大学 L - 合成游戏
What are the characteristics of the grammar of Russian documents in the translation of scientific papers
批量将PPM格式图片转化为JPG格式
Array sort 1
C语言——指针一点通※
[semantic segmentation] 2018-deeplabv3+ ECCV
Can serial port can 232 can 485 serial port to CANbus bus gateway module can232/485mb converter cancom
What are the restrictions on opening futures accounts? Where is the safest place to open an account?