結果
問題 | No.789 範囲の合計 |
ユーザー | mikit |
提出日時 | 2020-05-31 13:31:55 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 670 ms / 1,000 ms |
コード長 | 9,992 bytes |
コンパイル時間 | 5,105 ms |
コンパイル使用メモリ | 85,328 KB |
実行使用メモリ | 117,092 KB |
最終ジャッジ日時 | 2024-11-17 09:43:12 |
合計ジャッジ時間 | 13,494 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 97 ms
99,944 KB |
testcase_01 | AC | 98 ms
100,016 KB |
testcase_02 | AC | 664 ms
117,092 KB |
testcase_03 | AC | 555 ms
107,336 KB |
testcase_04 | AC | 670 ms
107,480 KB |
testcase_05 | AC | 591 ms
107,568 KB |
testcase_06 | AC | 602 ms
107,372 KB |
testcase_07 | AC | 543 ms
107,232 KB |
testcase_08 | AC | 621 ms
107,232 KB |
testcase_09 | AC | 602 ms
107,232 KB |
testcase_10 | AC | 569 ms
107,348 KB |
testcase_11 | AC | 588 ms
107,376 KB |
testcase_12 | AC | 592 ms
107,476 KB |
testcase_13 | AC | 92 ms
86,604 KB |
testcase_14 | AC | 92 ms
86,636 KB |
ソースコード
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Arrays; import java.util.function.LongBinaryOperator; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; import java.io.BufferedOutputStream; import java.io.UncheckedIOException; import java.util.Vector; import java.nio.charset.Charset; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author mikit */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; LightScanner in = new LightScanner(inputStream); LightWriter out = new LightWriter(outputStream); No789 solver = new No789(); solver.solve(1, in, out); out.close(); } static class No789 { public void solve(int testNumber, LightScanner in, LightWriter out) { FastIntDynamicSegmentTree st = new FastIntDynamicSegmentTree(1 << 30, 100000, Long::sum, 0, Long::sum); int n = in.ints(); long ans = 0; for (int i = 0; i < n; i++) { if (in.ints() == 0) st.update(in.ints(), in.ints()); else ans += st.query(in.ints(), in.ints() + 1); } //System.out.println(st); out.ans(ans).ln(); } } static class LightWriter implements AutoCloseable { private final Writer out; private boolean autoflush = false; private boolean breaked = true; public LightWriter(Writer out) { this.out = out; } public LightWriter(OutputStream out) { this(new OutputStreamWriter(new BufferedOutputStream(out), Charset.defaultCharset())); } public LightWriter print(char c) { try { out.write(c); breaked = false; } catch (IOException ex) { throw new UncheckedIOException(ex); } return this; } public LightWriter print(String s) { try { out.write(s, 0, s.length()); breaked = false; } catch (IOException ex) { throw new UncheckedIOException(ex); } return this; } public LightWriter ans(String s) { if (!breaked) { print(' '); } return print(s); } public LightWriter ans(long l) { return ans(Long.toString(l)); } public LightWriter ln() { print(System.lineSeparator()); breaked = true; if (autoflush) { try { out.flush(); } catch (IOException ex) { throw new UncheckedIOException(ex); } } return this; } public void close() { try { out.close(); } catch (IOException ex) { throw new UncheckedIOException(ex); } } } static class FastIntDynamicSegmentTree { private final long lb; private final long ub; private final LongBinaryOperator op; private final LongBinaryOperator up; private final long zero; private final Stack<Integer> reusableStack = new Stack<>(); private int used; private int allocated; private int[] l; private int[] r; private long[] v; public FastIntDynamicSegmentTree(long lb, long ub, int cap, LongBinaryOperator op, long zero, LongBinaryOperator up) { if (lb > ub) throw new IllegalArgumentException(); this.lb = lb; this.ub = ub; this.op = op; this.up = up; this.zero = zero; this.used = 1; int height = 64 - Long.numberOfLeadingZeros(ub - lb - 1); this.allocated = Math.max(cap * height, 1); this.l = new int[allocated]; this.r = new int[allocated]; this.v = new long[allocated]; l[0] = r[0] = -1; v[0] = zero; } public FastIntDynamicSegmentTree(long n, int cap, LongBinaryOperator op, long zero, LongBinaryOperator up) { this(0, n == 1 ? 1 : Long.highestOneBit(n - 1) << 1, cap, op, zero, up); } public FastIntDynamicSegmentTree(long n, LongBinaryOperator op, long zero, LongBinaryOperator up) { this(n, 128, op, zero, up); } public FastIntDynamicSegmentTree(LongBinaryOperator op, long zero, LongBinaryOperator up) { this(Integer.MAX_VALUE, op, zero, up); } private int l(int i) { return i == -1 ? -1 : l[i]; } private int r(int i) { return i == -1 ? -1 : r[i]; } private long v(int i) { return i == -1 ? zero : v[i]; } private void propagate(int i) { this.v[i] = op.applyAsLong(v(l(i)), v(r(i))); } private int createNode(long x) { if (used == allocated) { allocated <<= 1; l = Arrays.copyOf(l, allocated); r = Arrays.copyOf(r, allocated); v = Arrays.copyOf(v, allocated); } this.l[used] = this.r[used] = -1; this.v[used] = x; return used++; } public void update(long i, long x) { if (i < lb || ub <= i) throw new IndexOutOfBoundsException(); Stack<Integer> update = reusableStack; int node = 0; long lo = lb, hi = ub; while (hi - lo > 1) { update.push(node); long mid = (lo + hi) >> 1; if (i < mid) { if (l[node] == -1) l[node] = createNode(zero); node = l[node]; hi = mid; } else { if (r[node] == -1) r[node] = createNode(zero); node = r[node]; lo = mid; } } v[node] = up.applyAsLong(v[node], x); while (!update.isEmpty()) propagate(update.pop()); } public long query(long min, long max) { if (min < lb || ub < max || min > max) throw new IndexOutOfBoundsException(); int node = 0; long lo = lb, hi = ub, mid = (lb + ub) >> 1; while (node != -1 && (max < mid || mid < min)) { if (max < mid) { node = this.l[node]; hi = mid; mid = (lo + hi) >> 1; } else { node = this.r[node]; lo = mid; mid = (lo + hi) >> 1; } } if (node == -1) return zero; if (min == lo && hi == max) return v[node]; long left = queryL(min, this.l[node], lo, mid), right = queryR(max, this.r[node], mid, hi); return op.applyAsLong(left, right); } private long queryL(long min, int node, long lo, long hi) { if (lo == min) return v(node); long res = zero; while (node != -1 && min < hi) { long mid = (lo + hi) >> 1; if (mid < min) { lo = mid; node = this.r[node]; } else { res = op.applyAsLong(v(this.r[node]), res); node = this.l[node]; hi = mid; } } return res; } private long queryR(long max, int node, long lo, long hi) { if (max == hi) return v(node); long res = zero; while (node != -1 && lo < max) { long mid = (lo + hi) >> 1; if (max < mid) { hi = mid; node = this.l[node]; } else { res = op.applyAsLong(v(this.l[node]), res); node = this.r[node]; lo = mid; } } return res; } public String toString() { return "IntDynamicSegmentTree(" + "range = [" + lb + ", " + ub + "), zero = " + zero + ", used " + used + " / " + allocated + ")\nl = " + Arrays.toString(Arrays.copyOf(l, used)) + "\nr = " + Arrays.toString(Arrays.copyOf(r, used)) + "\nv = " + Arrays.toString(Arrays.copyOf(v, used)) + '\n'; } } static class LightScanner implements AutoCloseable { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public LightScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); } public String string() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new UncheckedIOException(e); } } return tokenizer.nextToken(); } public int ints() { return Integer.parseInt(string()); } public void close() { try { this.reader.close(); } catch (IOException ex) { throw new UncheckedIOException(ex); } } } }