Description
如题,你需要维护这样的一个长度为 N 的数组,支持如下几种操作
在某个历史版本上修改某一个位置上的值
- 访问某个历史版本上的某一位置的值
此外,每进行一次操作(对于操作2,即为生成一个完全一样的版本,不作任何改动),就会生成一个新的版本。版本编号即为当前操作的编号(从1开始编号,版本0表示初始状态数组)
Input
输入的第一行包含两个正整数 N, M, 分别表示数组的长度和操作的个数。
第二行包含N个整数,依次为初始状态下数组各位的值(依次为 a_i,1≤i≤N)。
接下来M行每行包含3或4个整数,代表两种操作之一(ii为基于的历史版本号):
对于操作1,格式为vi 1 loci valuei,即为在版本v_ivi的基础上,将 aloci 修改为 valuei
- 对于操作2,格式为vi 2 loci ,即访问版本 vi 中的 aloci 的值
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≤103
对于50%的数据:1≤N,M≤104
对于70%的数据:1≤N,M≤105
对于100%的数据:1≤N,M≤106,1≤loci≤N,0≤vi<i,−109≤ai,valuei≤109
经测试,正常常数的可持久化数组可以通过,请各位放心
数据略微凶残,请注意常数不要过大
另,此题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 ;
}