結果

問題 No.1234 典型RMQ
ユーザー CuriousFairy315CuriousFairy315
提出日時 2020-09-18 22:00:04
言語 Java19
(openjdk 21)
結果
AC  
実行時間 969 ms / 2,000 ms
コード長 12,552 bytes
コンパイル時間 4,573 ms
コンパイル使用メモリ 91,204 KB
実行使用メモリ 80,596 KB
最終ジャッジ日時 2023-08-08 18:41:46
合計ジャッジ時間 21,736 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
50,016 KB
testcase_01 AC 53 ms
48,252 KB
testcase_02 AC 54 ms
49,932 KB
testcase_03 AC 53 ms
49,992 KB
testcase_04 AC 55 ms
49,960 KB
testcase_05 AC 52 ms
49,844 KB
testcase_06 AC 897 ms
77,208 KB
testcase_07 AC 571 ms
64,804 KB
testcase_08 AC 969 ms
80,172 KB
testcase_09 AC 811 ms
67,056 KB
testcase_10 AC 943 ms
78,628 KB
testcase_11 AC 874 ms
77,124 KB
testcase_12 AC 773 ms
66,900 KB
testcase_13 AC 609 ms
66,820 KB
testcase_14 AC 791 ms
67,516 KB
testcase_15 AC 739 ms
67,380 KB
testcase_16 AC 928 ms
80,596 KB
testcase_17 AC 780 ms
66,856 KB
testcase_18 AC 478 ms
58,324 KB
testcase_19 AC 954 ms
79,272 KB
testcase_20 AC 449 ms
74,028 KB
testcase_21 AC 887 ms
76,984 KB
testcase_22 AC 479 ms
64,476 KB
testcase_23 AC 481 ms
64,444 KB
testcase_24 AC 477 ms
64,548 KB
testcase_25 AC 473 ms
64,468 KB
testcase_26 AC 482 ms
64,620 KB
testcase_27 AC 53 ms
50,024 KB
testcase_28 AC 53 ms
50,000 KB
testcase_29 AC 53 ms
50,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #


public class Main {

	public static void main(String[] args) {
		new Main();
	}

	public Main() {
		FastScanner fs = new FastScanner();
		java.io.PrintWriter out = new java.io.PrintWriter(System.out);
		solve(fs, out);
		out.flush();
	}

	public void solve(FastScanner fs, java.io.PrintWriter out) {
		int N = fs.nextInt();
		Long[] a = new Long[N];
		for (int i = 0;i < N;++ i) a[i] = fs.nextLong();
		final long INF = IntMath.powLong(10, 18);
		LazySegTree<Long, Long> segtree = new LazySegTree<Long, Long>(a, Math::min, INF, (l, r) -> l + r, (l, r) -> l + r, 0L);
		int Q = fs.nextInt();
		while(Q --> 0) {
			if (fs.nextChar() == '1') {
				int l = fs.nextInt() - 1, r = fs.nextInt();
				long c = fs.nextInt();
				segtree.apply(l, r, c);
			} else {
				int l = fs.nextInt() - 1, r = fs.nextInt();
				long c = fs.nextInt();
				out.println(segtree.prod(l, r));
			}
		}
	}
}

class FastScanner {

	private final java.io.InputStream in = System.in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;

	private boolean hasNextByte() {
		if (ptr < buflen) return true;
		ptr = 0;
		try {
			buflen = in.read(buffer);
		} catch (java.io.IOException e) {
			e.printStackTrace();
		}
		return buflen > 0;
	}

	private byte readByte() {
		return hasNextByte() ? buffer[ptr++ ] : -1;
	}

	private static boolean isPrintableChar(byte c) {
		return 32 < c || c < 0;
	}

	private static boolean isNumber(int c) {
		return '0' <= c && c <= '9';
	}

	public boolean hasNext() {
		while (hasNextByte() && !isPrintableChar(buffer[ptr]))
			ptr++ ;
		return hasNextByte();
	}

	public String next() {
		if (!hasNext()) throw new java.util.NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		byte b;
		while (isPrintableChar(b = readByte()))
			sb.appendCodePoint(b);
		return sb.toString();
	}

	public final char nextChar() {
		if (!hasNext()) throw new java.util.NoSuchElementException();
		return (char)readByte();
	}

	public final long nextLong() {
		if (!hasNext()) throw new java.util.NoSuchElementException();
		long n = 0;
		try {
			byte b = readByte();
			if (b == '-') {
				while (isNumber(b = readByte()))
					n = n * 10 + '0' - b;
				return n;
			} else if (!isNumber(b)) throw new NumberFormatException();
			do
				n = n * 10 + b - '0';
			while (isNumber(b = readByte()));
		} catch (java.util.NoSuchElementException e) {}
		return n;
	}

	public final int nextInt() {
		if (!hasNext()) throw new java.util.NoSuchElementException();
		int n = 0;
		try {
			byte b = readByte();
			if (b == '-') {
				while (isNumber(b = readByte()))
					n = n * 10 + '0' - b;
				return n;
			} else if (!isNumber(b)) throw new NumberFormatException();
			do
				n = n * 10 + b - '0';
			while (isNumber(b = readByte()));
		} catch (java.util.NoSuchElementException e) {}
		return n;
	}

	public double nextDouble() {
		return Double.parseDouble(next());
	}
}

class Arrays {

	public static void sort(final int[] array) {
		int l, min = 0xFFFFFFFF, max = 0;
		for (l = 0; l < array.length; ++l) {
			int i = array[l];
			min &= i;
			max |= i;
			if ((i & 0x80000000) == 0) break;
		}
		for (int r = l + 1; r < array.length; ++r) {
			int i = array[r];
			min &= i;
			max |= i;
			if ((i & 0x80000000) != 0) {
				array[r] = array[l];
				array[l++ ] = i;
			}
		}
		int use = min ^ max, bit = Integer.highestOneBit(use & 0x7FFFFFFF);
		if (bit == 0) return;
		sort(array, 0, l, use, bit);
		sort(array, l, array.length, use, bit);
	}

	private static void sort(final int[] array, final int left, final int right, final int use, int digit) {
		if (right - left <= 96) {
			for (int i = left + 1; i < right; ++i) {
				int tmp = array[i], tmp2, j;
				for (j = i; j > left && (tmp2 = array[j - 1]) > tmp; --j)
					array[j] = tmp2;
				array[j] = tmp;
			}
			return;
		}
		int l = left;
		while (l < right && (array[l] & digit) == 0)
			++l;
		for (int r = l + 1; r < right; ++r) {
			int i = array[r];
			if ((i & digit) == 0) {
				array[r] = array[l];
				array[l++ ] = i;
			}
		}
		if ((digit = Integer.highestOneBit(use & digit - 1)) == 0) return;
		sort(array, left, l, use, digit);
		sort(array, l, right, use, digit);
	}

	public static void sort(final long[] array) {
		int l;
		long min = 0xFFFFFFFFFFFFFFFFL, max = 0;
		for (l = 0; l < array.length; ++l) {
			long i = array[l];
			min &= i;
			max |= i;
			if ((i & 0x8000000000000000L) == 0) break;
		}
		for (int r = l + 1; r < array.length; ++r) {
			long i = array[r];
			min &= i;
			max |= i;
			if ((i & 0x8000000000000000L) != 0) {
				array[r] = array[l];
				array[l++ ] = i;
			}
		}
		long use = min ^ max, bit = Long.highestOneBit(use & 0x7FFFFFFFFFFFFFFFL);
		if (bit == 0) return;
		sort(array, 0, l, use, bit);
		sort(array, l, array.length, use, bit);
	}

	private static void sort(final long[] array, final int left, final int right, final long use, long digit) {
		if (right - left <= 96) {
			for (int i = left + 1, j; i < right; ++i) {
				long tmp = array[i], tmp2;
				for (j = i; j > left && (tmp2 = array[j - 1]) > tmp; --j)
					array[j] = tmp2;
				array[j] = tmp;
			}
			return;
		}
		int l = left;
		while (l < right && (array[l] & digit) == 0)
			++l;
		for (int r = l + 1; r < right; ++r) {
			long i = array[r];
			if ((i & digit) == 0) {
				array[r] = array[l];
				array[l++ ] = i;
			}
		}
		if ((digit = Long.highestOneBit(use & digit - 1)) == 0) return;
		sort(array, left, l, use, digit);
		sort(array, l, right, use, digit);
	}
}

class IntMath {

	public static int gcd(int a, int b) {
		while (a != 0)
			if ((b %= a) != 0) a %= b;
			else return a;
		return b;
	}

	public static int gcd(int... array) {
		int ret = array[0];
		for (int i = 1; i < array.length; ++i)
			ret = gcd(ret, array[i]);
		return ret;
	}

	public static long gcd(long a, long b) {
		while (a != 0)
			if ((b %= a) != 0) a %= b;
			else return a;
		return b;
	}

	public static long gcd(long... array) {
		long ret = array[0];
		for (int i = 1; i < array.length; ++i)
			ret = gcd(ret, array[i]);
		return ret;
	}

	public static long lcm(long a, long b) {
		return a / gcd(a, b) * b;
	}

	public static int pow(int a, int b) {
		int ans = 1;
		for (int mul = a; b > 0; b >>= 1, mul *= mul)
			if ((b & 1) != 0) ans *= mul;
		return ans;
	}

	public static long powLong(long a, long b) {
		long ans = 1;
		for (long mul = a; b > 0; b >>= 1, mul *= mul)
			if ((b & 1) != 0) ans *= mul;
		return ans;
	}

	public static int pow(int a, int b, int mod) {
		if (b < 0) b = b % (mod - 1) + mod - 1;
		long ans = 1;
		for (long mul = a; b > 0; b >>= 1, mul = mul * mul % mod)
			if ((b & 1) != 0) ans = ans * mul % mod;
		return (int)ans;
	}

	public static int floorsqrt(long n) {
		return (int)Math.sqrt(n + 0.1);
	}

	public static int ceilsqrt(long n) {
		return n <= 1 ? (int)n : (int)Math.sqrt(n - 0.1) + 1;
	}
}

/**
 * TODO: verify {@link LazySegTree#maxRight} and {@link LazySegTree#minLeft}
 *
 * @verified https://atcoder.jp/contests/practice2/tasks/practice2_k
 */

class LazySegTree<S, F> {

	private final int MAX;

	private final int N;
	private final int Log;
	private final java.util.function.BinaryOperator<S> Op;
	private final S E;
	private final java.util.function.BiFunction<F, S, S> Mapping;
	private final java.util.function.BinaryOperator<F> Composition;
	private final F Id;

	private final S[] Dat;
	private final F[] Laz;

	@SuppressWarnings("unchecked")
	public LazySegTree(int n, java.util.function.BinaryOperator<S> op, S e, java.util.function.BiFunction<F, S, S> mapping,
			java.util.function.BinaryOperator<F> composition, F id) {
		this.MAX = n;
		int k = 1;
		while (k < n)
			k <<= 1;
		this.N = k;
		this.Log = Integer.numberOfTrailingZeros(N);
		this.Op = op;
		this.E = e;
		this.Mapping = mapping;
		this.Composition = composition;
		this.Id = id;
		this.Dat = (S[])new Object[N << 1];
		this.Laz = (F[])new Object[N];
		java.util.Arrays.fill(Dat, E);
		java.util.Arrays.fill(Laz, Id);
	}

	public LazySegTree(S[] dat, java.util.function.BinaryOperator<S> op, S e, java.util.function.BiFunction<F, S, S> mapping,
			java.util.function.BinaryOperator<F> composition, F id) {
		this(dat.length, op, e, mapping, composition, id);
		build(dat);
	}

	private void build(S[] dat) {
		int l = dat.length;
		System.arraycopy(dat, 0, Dat, N, l);
		for (int i = N - 1; i > 0; i-- ) {
			Dat[i] = Op.apply(Dat[i << 1 | 0], Dat[i << 1 | 1]);
		}
	}

	private void push(int k) {
		if (Laz[k] == Id) return;
		int lk = k << 1 | 0, rk = k << 1 | 1;
		Dat[lk] = Mapping.apply(Laz[k], Dat[lk]);
		Dat[rk] = Mapping.apply(Laz[k], Dat[rk]);
		if (lk < N) Laz[lk] = Composition.apply(Laz[k], Laz[lk]);
		if (rk < N) Laz[rk] = Composition.apply(Laz[k], Laz[rk]);
		Laz[k] = Id;
	}

	private void pushTo(int k) {
		for (int i = Log; i > 0; i-- )
			push(k >> i);
	}

	private void pushTo(int lk, int rk) {
		for (int i = Log; i > 0; i-- ) {
			if (((lk >> i) << i) != lk) push(lk >> i);
			if (((rk >> i) << i) != rk) push(rk >> i);
		}
	}

	private void updateFrom(int k) {
		k >>= 1;
		while (k > 0) {
			Dat[k] = Op.apply(Dat[k << 1 | 0], Dat[k << 1 | 1]);
			k >>= 1;
		}
	}

	private void updateFrom(int lk, int rk) {
		for (int i = 1; i <= Log; i++ ) {
			if (((lk >> i) << i) != lk) {
				int lki = lk >> i;
				Dat[lki] = Op.apply(Dat[lki << 1 | 0], Dat[lki << 1 | 1]);
			}
			if (((rk >> i) << i) != rk) {
				int rki = (rk - 1) >> i;
				Dat[rki] = Op.apply(Dat[rki << 1 | 0], Dat[rki << 1 | 1]);
			}
		}
	}

	public void set(int p, S x) {
		exclusiveRangeCheck(p);
		p += N;
		pushTo(p);
		Dat[p] = x;
		updateFrom(p);
	}

	public S get(int p) {
		exclusiveRangeCheck(p);
		p += N;
		pushTo(p);
		return Dat[p];
	}

	public S prod(int l, int r) {
		if (l > r) { throw new IllegalArgumentException(String.format("Invalid range: [%d, %d)", l, r)); }
		inclusiveRangeCheck(l);
		inclusiveRangeCheck(r);
		if (l == r) return E;
		l += N;
		r += N;
		pushTo(l, r);
		S sumLeft = E, sumRight = E;
		while (l < r) {
			if ((l & 1) == 1) sumLeft = Op.apply(sumLeft, Dat[l++ ]);
			if ((r & 1) == 1) sumRight = Op.apply(Dat[ --r], sumRight);
			l >>= 1;
			r >>= 1;
		}
		return Op.apply(sumLeft, sumRight);
	}

	public S allProd() {
		return Dat[1];
	}

	public void apply(int p, F f) {
		exclusiveRangeCheck(p);
		p += N;
		pushTo(p);
		Dat[p] = Mapping.apply(f, Dat[p]);
		updateFrom(p);
	}

	public void apply(int l, int r, F f) {
		if (l > r) { throw new IllegalArgumentException(String.format("Invalid range: [%d, %d)", l, r)); }
		inclusiveRangeCheck(l);
		inclusiveRangeCheck(r);
		if (l == r) return;
		l += N;
		r += N;
		pushTo(l, r);
		for (int l2 = l, r2 = r; l2 < r2;) {
			if ((l2 & 1) == 1) {
				Dat[l2] = Mapping.apply(f, Dat[l2]);
				if (l2 < N) Laz[l2] = Composition.apply(f, Laz[l2]);
				l2++ ;
			}
			if ((r2 & 1) == 1) {
				r2-- ;
				Dat[r2] = Mapping.apply(f, Dat[r2]);
				if (r2 < N) Laz[r2] = Composition.apply(f, Laz[r2]);
			}
			l2 >>= 1;
			r2 >>= 1;
		}
		updateFrom(l, r);
	}

	public int maxRight(int l, java.util.function.Predicate<S> g) {
		inclusiveRangeCheck(l);
		if (!g.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); }
		if (l == MAX) return MAX;
		l += N;
		pushTo(l);
		S sum = E;
		do {
			l >>= Integer.numberOfTrailingZeros(l);
			if (!g.test(Op.apply(sum, Dat[l]))) {
				while (l < N) {
					push(l);
					l = l << 1;
					if (g.test(Op.apply(sum, Dat[l]))) {
						sum = Op.apply(sum, Dat[l]);
						l++ ;
					}
				}
				return l - N;
			}
			sum = Op.apply(sum, Dat[l]);
			l++ ;
		} while ((l & -l) != l);
		return MAX;
	}

	public int minLeft(int r, java.util.function.Predicate<S> g) {
		inclusiveRangeCheck(r);
		if (!g.test(E)) { throw new IllegalArgumentException("Identity element must satisfy the condition."); }
		if (r == 0) return 0;
		r += N;
		pushTo(r - 1);
		S sum = E;
		do {
			r-- ;
			while (r > 1 && (r & 1) == 1)
				r >>= 1;
			if (!g.test(Op.apply(Dat[r], sum))) {
				while (r < N) {
					push(r);
					r = r << 1 | 1;
					if (g.test(Op.apply(Dat[r], sum))) {
						sum = Op.apply(Dat[r], sum);
						r-- ;
					}
				}
				return r + 1 - N;
			}
			sum = Op.apply(Dat[r], sum);
		} while ((r & -r) != r);
		return 0;
	}

	private void exclusiveRangeCheck(int p) {
		if (p < 0 || p >= MAX) { throw new IndexOutOfBoundsException(String.format("Index %d is not in [%d, %d).", p, 0, MAX)); }
	}

	private void inclusiveRangeCheck(int p) {
		if (p < 0 || p > MAX) { throw new IndexOutOfBoundsException(String.format("Index %d is not in [%d, %d].", p, 0, MAX)); }
	}
}
0