結果
問題 | No.1099 Range Square Sum |
ユーザー |
👑 |
提出日時 | 2020-06-26 21:54:31 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 356 ms / 2,000 ms |
コード長 | 3,796 bytes |
コンパイル時間 | 920 ms |
コンパイル使用メモリ | 125,800 KB |
実行使用メモリ | 36,792 KB |
最終ジャッジ日時 | 2024-06-22 07:30:45 |
合計ジャッジ時間 | 6,193 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 30 |
ソースコード
import std.conv, std.functional, std.range, std.stdio, std.string;import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;import core.bitop;class EOFException : Throwable { this() { super("EOF"); } }string[] tokens;string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }int readInt() { return readToken.to!int; }long readLong() { return readToken.to!long; }real readReal() { return readToken.to!real; }bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1;(unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }class SegmentTree(T, S, alias opTT, alias opST, alias opSS) {import std.functional : binaryFun;alias opTTFun = binaryFun!opTT;alias opSTFun = opST;alias opSSFun = binaryFun!opSS;const(T) idT;const(S) idS;int n;T[] ts;S[] ss;this(int n_, const(T) idT, const(S) idS) {this.idT = idT;this.idS = idS;for (n = 1; n < n_; n <<= 1) {}ts = new T[n << 1];ss = new S[n << 1];ts[] = idT;ss[] = idS;}ref T at(int a) {return ts[n + a];}void build() {foreach_reverse (a; 1 .. n) ts[a] = opTTFun(ts[a << 1], ts[a << 1 | 1]);}T query(int a, int b, const(S) s) {return query(1, 0, n, a, b, s);}private:T query(int u, int l, int r, int a, int b, const(S) s) {if (a < l) a = l;if (b > r) b = r;if (a >= b) return idT;if (a == l && b == r) {ts[u] = opSTFun(s, ts[u], r - l);ss[u] = opSSFun(s, ss[u]);return ts[u];}const int uL = u << 1, uR = u << 1 | 1;const int mid = (l + r) >> 1;// speed-up: if (ss[u] != idS){ts[uL] = opSTFun(ss[u], ts[uL], mid - l);ts[uR] = opSTFun(ss[u], ts[uR], r - mid);ss[uL] = opSSFun(ss[u], ss[uL]);ss[uR] = opSSFun(ss[u], ss[uR]);ss[u] = idS;}const T resL = query(uL, l, mid, a, b, s);const T resR = query(uR, mid, r, a, b, s);ts[u] = opTTFun(ts[uL], ts[uR]);return opTTFun(resL, resR);}}void main() {try {for (; ; ) {const N = readInt();auto A = new long[N];foreach (i; 0 .. N) {A[i] = readLong();}const Q = readInt();auto typ = new int[Q];auto L = new int[Q];auto R = new int[Q];auto X = new long[Q];foreach (q; 0 .. Q) {typ[q] = readInt();L[q] = readInt() - 1;R[q] = readInt();if (typ[q] == 1) {X[q] = readLong();}}alias T = Tuple!(long, "sum1", long, "sum2");T opTT(T t0, T t1) {return T(t0.sum1 + t1.sum1, t0.sum2 + t1.sum2);}T opST(long s, T t, int sz) {return T(t.sum1 + s * sz, t.sum2 + 2 * s * t.sum1 + s^^2 * sz);}auto seg = new SegmentTree!(T, long, opTT, opST, (s0, s1) => (s0 + s1))(N, T(0, 0), 0);foreach (i; 0 .. N) {seg.at(i) = T(A[i], A[i]^^2);}seg.build;foreach (q; 0 .. Q) {if (typ[q] == 1) {seg.query(L[q], R[q], X[q]);} else {const res = seg.query(L[q], R[q], 0);writeln(res.sum2);}}}} catch (EOFException e) {}}