結果
問題 | No.789 範囲の合計 |
ユーザー | mikit |
提出日時 | 2020-05-31 13:34:11 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 659 ms / 1,000 ms |
コード長 | 8,549 bytes |
コンパイル時間 | 2,894 ms |
コンパイル使用メモリ | 85,680 KB |
実行使用メモリ | 113,220 KB |
最終ジャッジ日時 | 2024-04-28 16:16:02 |
合計ジャッジ時間 | 11,294 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 86 ms
93,968 KB |
testcase_01 | AC | 85 ms
94,104 KB |
testcase_02 | AC | 613 ms
113,216 KB |
testcase_03 | AC | 544 ms
113,068 KB |
testcase_04 | AC | 659 ms
112,996 KB |
testcase_05 | AC | 623 ms
113,128 KB |
testcase_06 | AC | 617 ms
112,992 KB |
testcase_07 | AC | 564 ms
113,000 KB |
testcase_08 | AC | 654 ms
113,008 KB |
testcase_09 | AC | 636 ms
112,992 KB |
testcase_10 | AC | 570 ms
113,220 KB |
testcase_11 | AC | 567 ms
113,172 KB |
testcase_12 | AC | 570 ms
113,060 KB |
testcase_13 | AC | 86 ms
93,692 KB |
testcase_14 | AC | 83 ms
94,200 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.queryRec(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()); } private long queryRec(long l, long r, int node, long lo, long hi) { if (l <= lo && hi <= r) return v(node); if (r <= lo || hi <= l || node == -1) return zero; long mid = (lo + hi) >> 1; return op.applyAsLong(queryRec(l, r, l(node), lo, mid), queryRec(l, r, r(node), mid, hi)); } public long queryRec(long l, long r) { if (l < lb || ub < r || l > r) throw new IndexOutOfBoundsException(); return queryRec(l, r, 0, lb, ub); } 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); } } } }