結果
問題 | No.1496 不思議な数え上げ |
ユーザー | 👑 hos.lyric |
提出日時 | 2022-11-13 14:51:16 |
言語 | D (dmd 2.106.1) |
結果 |
AC
|
実行時間 | 227 ms / 3,000 ms |
コード長 | 3,098 bytes |
コンパイル時間 | 983 ms |
コンパイル使用メモリ | 120,832 KB |
実行使用メモリ | 61,936 KB |
最終ジャッジ日時 | 2024-06-22 16:42:17 |
合計ジャッジ時間 | 12,852 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,376 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 189 ms
19,400 KB |
testcase_04 | AC | 227 ms
19,400 KB |
testcase_05 | AC | 183 ms
21,480 KB |
testcase_06 | AC | 205 ms
61,512 KB |
testcase_07 | AC | 193 ms
61,512 KB |
testcase_08 | AC | 187 ms
61,936 KB |
testcase_09 | AC | 152 ms
20,884 KB |
testcase_10 | AC | 156 ms
20,912 KB |
testcase_11 | AC | 169 ms
20,880 KB |
testcase_12 | AC | 177 ms
21,536 KB |
testcase_13 | AC | 156 ms
32,668 KB |
testcase_14 | AC | 164 ms
41,884 KB |
testcase_15 | AC | 147 ms
20,868 KB |
testcase_16 | AC | 150 ms
21,648 KB |
testcase_17 | AC | 174 ms
20,764 KB |
testcase_18 | AC | 173 ms
20,804 KB |
testcase_19 | AC | 172 ms
20,916 KB |
testcase_20 | AC | 175 ms
20,908 KB |
testcase_21 | AC | 170 ms
20,624 KB |
testcase_22 | AC | 175 ms
20,872 KB |
testcase_23 | AC | 200 ms
20,816 KB |
testcase_24 | AC | 185 ms
21,048 KB |
testcase_25 | AC | 176 ms
21,012 KB |
testcase_26 | AC | 172 ms
21,008 KB |
testcase_27 | AC | 174 ms
23,680 KB |
testcase_28 | AC | 170 ms
23,700 KB |
testcase_29 | AC | 182 ms
23,704 KB |
testcase_30 | AC | 169 ms
23,744 KB |
testcase_31 | AC | 174 ms
23,768 KB |
testcase_32 | AC | 160 ms
24,300 KB |
testcase_33 | AC | 79 ms
10,360 KB |
testcase_34 | AC | 76 ms
11,040 KB |
testcase_35 | AC | 2 ms
6,940 KB |
testcase_36 | AC | 158 ms
24,328 KB |
testcase_37 | AC | 100 ms
10,768 KB |
testcase_38 | AC | 93 ms
11,000 KB |
testcase_39 | AC | 117 ms
17,884 KB |
testcase_40 | AC | 63 ms
11,292 KB |
testcase_41 | AC | 118 ms
17,640 KB |
ソースコード
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)); } // root: min (tie-break: left) class MinCartesianTree { int n, rt; int[] par, lef, rig; void build(T)(T as) { n = cast(int)(as.length); assert(n >= 1); rt = 0; par = new int[n]; par[] = -1; lef = new int[n]; lef[] = -1; rig = new int[n]; rig[] = -1; int top = 0; auto stack = new int[n]; foreach (u; 1 .. n) { if (as[stack[top]] > as[u]) { // > for (; top >= 1 && as[stack[top - 1]] > as[u]; --top) {} // > if (top == 0) { rt = par[lef[u] = stack[top]] = u; } else { par[lef[u] = stack[top]] = u; rig[par[u] = stack[top - 1]] = u; } stack[top] = u; } else { rig[par[u] = stack[top]] = u; stack[++top] = u; } } } }; int N; int[] P; long[] A; long[] PSum; MinCartesianTree ct; long[] ans; void dfs(int u, int l, int r) { if (!~u) return; // [l, u], [u + 1, r] const a = A[P[u]]; long sum; if (u - l + 1 <= r - u) { auto ran = PSum[u + 1 .. r + 1]; foreach (i; l .. u + 1) { sum += ran.upperBound(PSum[i] + a); } } else { auto ran = PSum[l .. u + 1]; foreach (i; u + 1 .. r + 1) { sum += (u - l + 1) - ran.lowerBound(PSum[i] - a); } } ans[P[u]] = sum; dfs(ct.lef[u], l, u); dfs(ct.rig[u], u + 1, r); } void main() { try { for (; ; ) { N = readInt; P = new int[N]; foreach (i; 0 .. N) { P[i] = readInt; } A = new long[N + 1]; foreach (j; 1 .. N + 1) { A[j] = readLong; } PSum = new long[N + 1]; foreach (i; 0 .. N) { PSum[i + 1] = PSum[i] + P[i]; } ct = new MinCartesianTree; ct.build(P); ans = new long[N + 1]; dfs(ct.rt, 0, N); foreach (j; 1 .. N + 1) { writeln(ans[j]); } debug { writeln("========"); } } } catch (EOFException e) { } }