結果

問題 No.789 範囲の合計
ユーザー mikitmikit
提出日時 2020-05-31 13:34:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 634 ms / 1,000 ms
コード長 8,549 bytes
コンパイル時間 2,680 ms
コンパイル使用メモリ 85,516 KB
実行使用メモリ 107,696 KB
最終ジャッジ日時 2024-11-17 09:47:58
合計ジャッジ時間 10,837 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 91 ms
93,936 KB
testcase_01 AC 91 ms
93,900 KB
testcase_02 AC 579 ms
107,696 KB
testcase_03 AC 514 ms
107,148 KB
testcase_04 AC 609 ms
107,272 KB
testcase_05 AC 571 ms
107,488 KB
testcase_06 AC 575 ms
107,620 KB
testcase_07 AC 548 ms
107,472 KB
testcase_08 AC 634 ms
107,504 KB
testcase_09 AC 623 ms
107,436 KB
testcase_10 AC 571 ms
107,508 KB
testcase_11 AC 585 ms
107,520 KB
testcase_12 AC 596 ms
107,452 KB
testcase_13 AC 89 ms
86,304 KB
testcase_14 AC 89 ms
86,488 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

    }
}

0