結果
| 問題 |
No.789 範囲の合計
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-05-31 05:41:16 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 915 ms / 1,000 ms |
| コード長 | 7,808 bytes |
| コンパイル時間 | 2,774 ms |
| コンパイル使用メモリ | 89,860 KB |
| 実行使用メモリ | 86,772 KB |
| 最終ジャッジ日時 | 2024-11-15 21:27:45 |
| 合計ジャッジ時間 | 12,474 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 15 |
ソースコード
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
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) {
IntDynamicSegmentTree st = new IntDynamicSegmentTree(1 << 30, 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);
}
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 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);
}
}
}
static class IntDynamicSegmentTree {
private final long lb;
private final long ub;
private Node root;
private final LongBinaryOperator op;
private final LongBinaryOperator up;
private final long zero;
private final Stack<Node> reusableStack = new Stack<>();
public IntDynamicSegmentTree(long lb, long ub, LongBinaryOperator op, long zero, LongBinaryOperator up) {
if (lb > ub) throw new IllegalArgumentException();
this.lb = lb;
this.ub = ub;
this.root = new Node(zero);
this.op = op;
this.up = up;
this.zero = zero;
}
public IntDynamicSegmentTree(long n, LongBinaryOperator op, long zero, LongBinaryOperator up) {
this(0, n == 1 ? 1 : Long.highestOneBit(n - 1) << 1, op, zero, up);
}
public IntDynamicSegmentTree(LongBinaryOperator op, long zero, LongBinaryOperator up) {
this(Long.MAX_VALUE, op, zero, up);
}
public void update(long i, long x) {
if (i < lb || ub <= i) throw new IndexOutOfBoundsException();
Stack<Node> update = reusableStack;
Node node = root;
long lo = lb, hi = ub;
while (hi - lo > 1) {
update.push(node);
long mid = (lo + hi) >> 1;
if (i < mid) {
if (node.l == null) node.l = new Node(zero);
node = node.l;
hi = mid;
} else {
if (node.r == null) node.r = new Node(zero);
node = node.r;
lo = mid;
}
}
node.v = up.applyAsLong(node.v, x);
while (!update.isEmpty()) update.pop().recalculate();
}
private long queryRec(long l, long r, Node node, long lo, long hi) {
if (l <= lo && hi <= r) return node == null ? zero : node.v;
if (r <= lo || hi <= l) return zero;
long mid = (lo + hi) >> 1;
return op.applyAsLong(
node.l == null ? zero : queryRec(l, r, node.l, lo, mid),
node.r == null ? zero : queryRec(l, r, node.r, mid, hi)
);
}
public long queryRec(long l, long r) {
if (l < lb || ub < r || l > r) throw new IndexOutOfBoundsException();
return queryRec(l, r, root, lb, ub);
}
public String toString() {
System.out.println("Segment Tree [" + lb + ", " + ub + ")");
return root.toString(lb, ub);
}
private static String indent(String s, String indent) {
s = s.replace("\n", "\n" + indent);
return indent + s.substring(0, s.length() - indent.length());
}
private class Node {
Node l;
Node r;
long v;
Node(long v) {
this.v = v;
}
void recalculate() {
if (l == null && r == null) return;
if (l == null) this.v = r.v;
else if (r == null) this.v = l.v;
else this.v = op.applyAsLong(l.v, r.v);
}
public String toString(long lo, long hi) {
long mid = (lo + hi) >> 1;
return String.format("Node[%d, %d) = %d\n%s%s", lo, hi, v, indent(l == null ? "*\n" : l.toString(lo, mid), " "), indent(r == null ? "*\n" : r.toString(mid, hi), " "));
}
}
}
}