結果

問題 No.1496 不思議な数え上げ
ユーザー 👑 hos.lyrichos.lyric
提出日時 2022-11-13 14:51:16
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 205 ms / 3,000 ms
コード長 3,098 bytes
コンパイル時間 1,538 ms
コンパイル使用メモリ 107,548 KB
実行使用メモリ 60,656 KB
最終ジャッジ日時 2023-09-04 18:56:37
合計ジャッジ時間 16,476 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,388 KB
testcase_01 AC 1 ms
4,388 KB
testcase_02 AC 1 ms
4,508 KB
testcase_03 AC 187 ms
20,024 KB
testcase_04 AC 189 ms
21,216 KB
testcase_05 AC 187 ms
19,960 KB
testcase_06 AC 203 ms
60,632 KB
testcase_07 AC 205 ms
60,656 KB
testcase_08 AC 205 ms
60,608 KB
testcase_09 AC 167 ms
20,200 KB
testcase_10 AC 166 ms
20,032 KB
testcase_11 AC 167 ms
19,936 KB
testcase_12 AC 188 ms
20,260 KB
testcase_13 AC 174 ms
31,760 KB
testcase_14 AC 181 ms
40,988 KB
testcase_15 AC 160 ms
20,024 KB
testcase_16 AC 161 ms
19,940 KB
testcase_17 AC 192 ms
20,092 KB
testcase_18 AC 191 ms
20,136 KB
testcase_19 AC 194 ms
20,092 KB
testcase_20 AC 191 ms
20,024 KB
testcase_21 AC 193 ms
20,028 KB
testcase_22 AC 195 ms
20,152 KB
testcase_23 AC 191 ms
20,168 KB
testcase_24 AC 197 ms
20,068 KB
testcase_25 AC 187 ms
20,144 KB
testcase_26 AC 191 ms
20,120 KB
testcase_27 AC 190 ms
22,028 KB
testcase_28 AC 191 ms
21,972 KB
testcase_29 AC 192 ms
22,040 KB
testcase_30 AC 190 ms
22,048 KB
testcase_31 AC 191 ms
22,036 KB
testcase_32 AC 174 ms
23,284 KB
testcase_33 AC 91 ms
11,004 KB
testcase_34 AC 84 ms
11,664 KB
testcase_35 AC 3 ms
4,380 KB
testcase_36 AC 176 ms
24,228 KB
testcase_37 AC 98 ms
12,352 KB
testcase_38 AC 89 ms
11,336 KB
testcase_39 AC 125 ms
13,748 KB
testcase_40 AC 69 ms
10,296 KB
testcase_41 AC 132 ms
14,164 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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) {
  }
}
0