洛谷 P3919 【模板】可持久化线段树 1(可持久化数组)java语言

说明:代码借鉴了题解中的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);
		}
	}
}

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值