Description

如题,你需要维护这样的一个长度为 N 的数组,支持如下几种操作

  1. 在某个历史版本上修改某一个位置上的值

  2. 访问某个历史版本上的某一位置的值

此外,每进行一次操作(对于操作2,即为生成一个完全一样的版本,不作任何改动),就会生成一个新的版本。版本编号即为当前操作的编号(从1开始编号,版本0表示初始状态数组)

Input

输入的第一行包含两个正整数 N, M, 分别表示数组的长度和操作的个数。

第二行包含N个整数,依次为初始状态下数组各位的值(依次为 a_i,1≤i≤N)。

接下来M行每行包含3或4个整数,代表两种操作之一(ii为基于的历史版本号):

  1. 对于操作1,格式为v​i​​ 1 loc​i​​ value​i​​,即为在版本v_iv​i​​的基础上,将 a​loc​i​​​​ 修改为 value​i​​

  2. 对于操作2,格式为v​i​​ 2 loc​i​​ ,即访问版本 v​i​​ 中的 a​loc​i​​​​ 的值

Output

输出包含若干行,依次为每个操作2的结果。

Sample Input

5 10
59 46 14 87 41
0 2 1
0 1 1 14
0 1 1 57
0 1 1 88
4 2 4
0 2 5
0 2 4
4 2 1
2 2 2
1 1 5 91

Sample Output

59
87
41
87
88
46

HINT

数据规模:

对于30%的数据:1≤N,M≤10​3​​

对于50%的数据:1≤N,M≤10​4​​

对于70%的数据:1≤N,M≤10​5​​

对于100%的数据:1≤N,M≤10​6​​,1≤loc​i​​≤N,0≤v​i​​<i,−10​9​​≤a​i​​,value​i​​≤10​9​​

经测试,正常常数的可持久化数组可以通过,请各位放心

数据略微凶残,请注意常数不要过大

另,此题I/O量较大,如果实在TLE请注意I/O优化

样例说明:

一共11个版本,编号从0-10,依次为:

  • 0 : 59 46 14 87 41

  • 1 : 59 46 14 87 41

  • 2 : 14 46 14 87 41

  • 3 : 57 46 14 87 41

  • 4 : 88 46 14 87 41

  • 5 : 88 46 14 87 41

  • 6 : 59 46 14 87 41

  • 7 : 59 46 14 87 41

  • 8 : 88 46 14 87 41

  • 9 : 14 46 14 87 41

  • 10 : 59 46 14 87 91

题解

$rt$,当模板存着...

 //It is made by Awson on 2017.10.3
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define LL long long
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define sqr(x) ((x)*(x))
#define insert INSERT
using namespace std;
const int N = 1e6;
void read(int &x) {
char ch; bool flag = ;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || ); ch = getchar());
for (x = ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar());
x *= -*flag;
} struct node {
int key;
node *child[];
}sgm[N*+], *pos = sgm;
node* root[N+];
int n, m, a[N+];
int opt, v, loc, val; void build(node *o, int l, int r) {
if (l == r) {
o->key = a[l];
return;
}
int mid = (l+r)>>;
o->child[] = ++pos; build(o->child[], l, mid);
o->child[] = ++pos; build(o->child[], mid+, r);
}
void insert(node* &o, int l, int r, int loc, int val) {
node* tmp = o;
o = ++pos;
if (l == r) {
o->key = val;
return;
}else {
o->child[] = tmp->child[];
o->child[] = tmp->child[];
}
int mid = (l+r)>>;
if (loc <= mid) insert(o->child[], l, mid, loc, val);
else insert(o->child[], mid+, r, loc, val);
}
int query(node *o, int l, int r, int loc) {
if (l == r) return o->key;
int mid = (l+r)>>;
if (loc <= mid) return query(o->child[], l, mid, loc);
else return query(o->child[], mid+, r, loc);
}
void work() {
read(n), read(m);
for (int i = ; i <= n; i++) read(a[i]);
root[] = pos;
build(root[], , n);
for (int i = ; i <= m; i++) {
read(v), read(opt);
if (opt == ) {
read(loc), read(val);
root[i] = root[v];
insert(root[i], , n, loc, val);
}
else {
read(loc);
root[i] = root[v];
printf("%d\n", query(root[i], , n, loc));
}
}
}
int main() {
work();
return ;
}

[Luogu 3919]【模板】可持久化数组(可持久化线段树/平衡树)的更多相关文章

  1. luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树)(主席树)

    luogu P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目 #include<iostream> #include<cstdlib> #include< ...

  2. Luogu P3919【模板】可持久化数组(可持久化线段树/平衡树)

    题面:[模板]可持久化数组(可持久化线段树/平衡树) 不知道说啥,总之我挺喜欢自己打的板子的! #include<cstdio> #include<cstring> #incl ...

  3. 洛谷 P3919 【模板】可持久化数组(可持久化线段树/平衡树)-可持久化线段树(单点更新,单点查询)

    P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目背景 UPDATE : 最后一个点时间空间已经放大 标题即题意 有了可持久化数组,便可以实现很多衍生的可持久化功能(例如:可持久化并查集 ...

  4. 洛谷——P3919 【模板】可持久化数组(可持久化线段树/平衡树)

    P3919 [模板]可持久化数组(可持久化线段树/平衡树) 题目背景 UPDATE : 最后一个点时间空间已经放大 标题即题意 有了可持久化数组,便可以实现很多衍生的可持久化功能(例如:可持久化并查集 ...

  5. Luogu P3919 【模板】可持久化数组 可持久化线段树

    其实就是可持久化线段树的模板题线段树不会看这里 #include<bits/stdc++.h> ; using namespace std; ]; ],rc[N*],val[N*],cnt ...

  6. P3919 【模板】可持久化数组 -初步探究主席树

    本篇blog主要是给自己(大家)看的. 感谢longlongzhu123奆佬(此人初二LCT)的指点,使本蒟蒻可以快速开始主席树入门. what is 主席树? $        $主席树这个名字只不 ...

  7. 洛谷P3919 【模板】可持久化数组(可持久化线段树/平衡树)

    题目背景 UPDATE : 最后一个点时间空间已经放大 标题即题意 有了可持久化数组,便可以实现很多衍生的可持久化功能(例如:可持久化并查集) 题目描述 如题,你需要维护这样的一个长度为 N 的数组, ...

  8. P3919 【模板】可持久化数组(可持久化线段树/平衡树)

    题目描述 如题,你需要维护这样的一个长度为 N  的数组,支持如下几种操作 在某个历史版本上修改某一个位置上的值 访问某个历史版本上的某一位置的值 此外,每进行一次操作(对于操作2,即为生成一个完全一 ...

  9. LGOJ P3919【模板】可持久化数组(可持久化线段树/平衡树)

    代码 //可持久化线段树 #include <cstdio> using namespace std; struct node { node *Lnode,*Rnode; int val; ...

随机推荐

  1. bzoj 4373 算术天才⑨与等差数列

    4373: 算术天才⑨与等差数列 Time Limit: 10 Sec  Memory Limit: 128 MBhttp://www.lydsy.com/JudgeOnline/problem.ph ...

  2. [Android FrameWork 6.0源码学习] View的重绘ViewRootImpl的setView方法

    博客首页:http://www.cnblogs.com/kezhuang/p/ 本篇文章来分析一下WindowManager的后续工作,也就是ViewRootImpl的setView函数的工作 /i* ...

  3. 如何进行服务器Linux系统下的ext文件系统修复

    一.故障描述 服务器是dell 730系列服务器,存储阵列是MD3200系列存储5T的Lun,操作系统是Linux centos 7,文件系统类型是EXT4,因意外断电,导致系统不能正常启动,修复之后 ...

  4. Golang学习--开篇

    最近开始接收一个新项目,是使用Golang写的,需要重新捡起Golang来,于是就有了这个系列博客. Golang的环境配置,我就不说了,让我们直接开始. Golang官网:https://golan ...

  5. LeetCode & Q414-Third Maximum Number-Easy

    Array Math Description: Given a non-empty array of integers, return the third maximum number in this ...

  6. Mock API是如何在开发中发光发热的?

    在长期的服务过程中,我们经常会遇到前来咨询的用户与我们反馈以下这种情况:咨询者是一个前端人员,在项目开发的过程中需要与后端进行对接,遇到后端还没完成数据输出的情况下,他只好写静态模拟数据,在遇到大型项 ...

  7. centos6.5中rpm包安装mysql5.7(初始化出错如何解决)

    下载rpm包见:http://www.cnblogs.com/grey-wolf/p/7472680.html 1.rz上传到服务器,解压缩 rz [root@mini2 upload]# -.el6 ...

  8. Codeforces Round #441 (Div. 2, by Moscow Team Olympiad) C. Classroom Watch

    http://codeforces.com/contest/876/problem/C 题意: 现在有一个数n,它是由一个数x加上x每一位的数字得到的,现在给出n,要求找出符合条件的每一个x. 思路: ...

  9. SecureCRT安装

    第一步:下载SecureCRT&SecureCRT激活工具 首先下载SecureCRT安装包和SecureCRT激活工具,SecureCRT&SecureCRT激活工具下载地址:链接: ...

  10. Eclipse中JavaSwing图形插件安装

    1.在百度中搜索WindowBuilder,找到http://www.eclipse.org/windowbuilder/ 2.点击Download调转到页面: 因为我的eclipse版本是 3.点击 ...