洛谷 P1253 扶苏的问题 java题解+线段树

写了很久,WA了至少三十多次(全都是60分)。(一种植物),心态差点崩了,还好有java前辈的帮助,终于AC了

   这个题练习线段树确实不错,我借鉴了前辈们的pushdown函数,刚开始写了两个pushdown函数,分别传递两个懒标记嘛,但是错误就错在我这两个pushdown函数处理标记的时候出了问题,具体是哪里的问题,我也很头晕QWQ,应该就是没处理好覆盖标记与加法标记的影响。

  要注意几个问题:

  1. 记得要开long
  2. 覆盖标记要用一个特殊的值,不要用0,因为它的含义跟加法标记不一样
  3. 要处理好加法标记与覆盖标记之间的影响,尽量写在一个pushdown函数里,分开写两个pushdown函数的话可能不太好处理
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;

	static long[] tree, modify, add;

	static void init(int n) {

		a = new int[n + 1];

		tree = new long[5 * n | 1];//存储的值是区间最大值

		modify = new long[5 * n | 1];//区间修改懒标记

		add = new long[5 * n | 1];//加法懒标记
	}

	static int mark = -1;//1表示+在前面 -1表示same在前面

	static void build(int node, int start, int end) {
		if (start == end) {
			tree[node] = a[start];
		} else {
			int mid = (start + end) >> 1;
			build(2 * node + 1, start, mid);
			build(2 * node + 2, mid + 1, end);

			//更新区间最大值
			tree[node] = max(tree[2 * node + 1], tree[2 * node + 2]);

		}
	}


	//区间加上一个数
	static void Add(int node, int start, int end, int l, int r, int val) {

		if (start > end || start > r || end < l) return;

		//刚好在区间内
		if (start >= l && end <= r) {

			tree[node] += (val);

			add[node] += val;

			return;
		}

		pushDown(node, start, end);

		int mid = (start + end) >> 1;

		Add(2 * node + 1, start, mid, l, r, val);

		Add(2 * node + 2, mid + 1, end, l, r, val);


		tree[node] = max(tree[2 * node + 1], tree[2 * node + 2]);
	}


	static void pushDown(int node, int start, int end) {
		if (modify[node] != Long.MAX_VALUE) {
			tree[node * 2 + 1] = modify[node];
			tree[node * 2 + 2] = modify[node];

			modify[node * 2 + 1] = modify[node];
			modify[node * 2 + 2] = modify[node];

			add[node * 2 + 1] = 0;
			add[node * 2 + 2] = 0;

			modify[node] = Long.MAX_VALUE;
		}

		if (add[node] != 0) {

			tree[2 * node + 1] += add[node];
			tree[2 * node + 2] += add[node];

			add[node * 2 + 1] += add[node];
			add[node * 2 + 2] += add[node];

			add[node] = 0;
		}
	}

	static void modify(int node, int start, int end, int l, int r, int val) {

		if (start > end || start > r || end < l) return;

		//刚好在区间内
		if (start >= l && end <= r) {

			tree[node] = val;

			modify[node] = val;

			add[node] = 0;

			return;
		}

		pushDown(node, start, end);

		int mid = (start + end) >> 1;

		modify(2 * node + 1, start, mid, l, r, val);

		modify(2 * node + 2, mid + 1, end, l, r, val);


		tree[node] = max(tree[2 * node + 1], tree[2 * node + 2]);
	}


	static long query(int node, int start, int end, int l, int r) {





		if (start > end || start > r || end < l)
			return Long.MIN_VALUE;

		if (start >= l && end <= r) {
			return tree[node];
		}

		pushDown(node, start, end);

		int mid = (start + end) / 2;

		long p1 = query(2 * node + 1, start, mid, l, r);

		long p2 = query(2 * node + 2, mid + 1, end, l, r);

		return max(p1, p2);
	}

	static long max(long a, long b) {
		return a >= b ? a : b;
	}

	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();

		Arrays.fill(modify, Long.MAX_VALUE);

		build(1, 1, n);

//		//pr.write(query(1,1,n,1,3)+"\n");
		while (m-- > 0) {
			int opt = read.nextInt();
			int l = read.nextInt(), r = read.nextInt();
			if (opt == 1) {

				int x = read.nextInt();

				modify(1, 1, n, l, r, x);

			} else if (opt == 2) {

				int x = read.nextInt();
				Add(1, 1, n, l, r, x);

			} else {
				pr.write(query(1, 1, n, l, r) + "\n");
			}
		}

		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);
		}
	}
}

  • 18
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值