说明:代码借鉴了题解中的C++大佬的思路,没有采用结构体的写法
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Character.isValidCodePoint;
import static java.lang.Math.*;
import static java.lang.System.in;
import static java.lang.System.out;
public class Main {
static final PrintWriter pr = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out)));
static final Writer writer = new BufferedWriter(pr);
static final BufferedReader buff = new BufferedReader(new InputStreamReader(in));
static Read read = new Read();
static int[] a, b, root;
static int[] sum, left, right, val;
static int cnt;
static void init(int n) {
a = new int[n + 1];
b = new int[n + 1];
root = new int[n << 5];
sum = new int[n << 5];
left = new int[n << 5];
val = new int[n << 5];
right = new int[n << 5];
}
static int build(int start, int end) {
int node = ++cnt;
//下面是普通建树
if (start == end) {
val[node] = a[start];
}
if (start < end) {
int mid = (start + end) >> 1;
left[node] = build(start, mid);
right[node] = build(mid + 1, end);
}
return node;
}
static int update(int pre, int l, int r, int x, int v) {
int node = ++cnt;
left[node] = left[pre];
right[node] = right[pre];
val[node] = val[pre];
if (l == r) {
val[node] = v;
return node;
}
int mid = (l + r) >> 1;
if (x <= mid)
left[node] = update(left[pre], l, mid, x, v);
else
right[node] = update(right[pre], mid + 1, r, x, v);
return node;
}
static int query(int node, int l, int r, int x) {
if (l == r)
return val[node];
int mid = (l + r) >> 1;
if (x <= mid)
return query(left[node], l, mid, x);
else
return query(right[node], mid + 1, r, x);
}
public static final void main(String[] args) throws Exception {
int n = read.nextInt(), m = read.nextInt();
init(n);
for (int i = 1; i <= n; ++i) a[i] = read.nextInt();
root[0] = build(1, n);
for (int i = 1; i <= m; ++i) {
int pre = read.nextInt(), opt = read.nextInt(), x = read.nextInt();
if (opt == 1) {
int v = read.nextInt();
root[i] = update(root[pre], 1, n, x, v);
} else {
pr.write(query(root[pre], 1, n, x) + "\n");
root[i] = root[pre];
}
}
pr.close();
}
static class Read {
private InputStream stream = in;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private int read() throws IOException {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read(buf);
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
public int nextInt() throws IOException {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() throws IOException {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') return res * pow(10, nextInt());
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') return res * pow(10, nextInt());
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String next() throws Exception {
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
if (isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private char nextChar() throws IOException {
int c = read();
while (isSpaceChar(c)) c = read();
return (char) c;
}
private boolean isSpaceChar(int c) {
if (filter != null) return filter.isSpaceChar(c);
return isWhitespace(c);
}
private boolean isSpaceChar2(int c) {
if (filter != null) {
return filter.isSpaceChar2(c);
}
return isWhitespace2(c);
}
private static boolean isWhitespace(int c) {
return c == '\n' || c == '\r' || c == '\t' || c == -1 || c == ' ';
}
private static boolean isWhitespace2(int c) {
return c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
public boolean isSpaceChar2(int ch);
}
}
}