写了很久,WA了至少三十多次(全都是60分)。(一种植物),心态差点崩了,还好有java前辈的帮助,终于AC了
这个题练习线段树确实不错,我借鉴了前辈们的pushdown函数,刚开始写了两个pushdown函数,分别传递两个懒标记嘛,但是错误就错在我这两个pushdown函数处理标记的时候出了问题,具体是哪里的问题,我也很头晕QWQ,应该就是没处理好覆盖标记与加法标记的影响。
要注意几个问题:
- 记得要开long
- 覆盖标记要用一个特殊的值,不要用0,因为它的含义跟加法标记不一样
- 要处理好加法标记与覆盖标记之间的影响,尽量写在一个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);
}
}
}