結果

問題 No.1347 HS Railway
ユーザー 👑 hos.lyrichos.lyric
提出日時 2021-01-16 16:51:24
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 458 ms / 2,000 ms
コード長 3,524 bytes
コンパイル時間 1,237 ms
コンパイル使用メモリ 131,508 KB
実行使用メモリ 49,828 KB
最終ジャッジ日時 2023-09-04 11:51:08
合計ジャッジ時間 16,278 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 5 ms
5,172 KB
testcase_04 AC 4 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 4 ms
4,380 KB
testcase_08 AC 5 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 108 ms
15,224 KB
testcase_13 AC 114 ms
13,052 KB
testcase_14 AC 49 ms
7,812 KB
testcase_15 AC 27 ms
12,928 KB
testcase_16 AC 140 ms
16,500 KB
testcase_17 AC 40 ms
7,832 KB
testcase_18 AC 265 ms
34,220 KB
testcase_19 AC 306 ms
34,648 KB
testcase_20 AC 141 ms
23,776 KB
testcase_21 AC 22 ms
11,048 KB
testcase_22 AC 227 ms
33,080 KB
testcase_23 AC 262 ms
32,116 KB
testcase_24 AC 85 ms
13,708 KB
testcase_25 AC 176 ms
31,060 KB
testcase_26 AC 243 ms
33,204 KB
testcase_27 AC 149 ms
23,324 KB
testcase_28 AC 163 ms
25,812 KB
testcase_29 AC 245 ms
33,596 KB
testcase_30 AC 197 ms
33,768 KB
testcase_31 AC 184 ms
33,060 KB
testcase_32 AC 331 ms
36,392 KB
testcase_33 AC 318 ms
47,376 KB
testcase_34 AC 360 ms
36,168 KB
testcase_35 AC 290 ms
46,756 KB
testcase_36 AC 341 ms
47,904 KB
testcase_37 AC 344 ms
48,884 KB
testcase_38 AC 390 ms
49,828 KB
testcase_39 AC 424 ms
47,928 KB
testcase_40 AC 283 ms
36,564 KB
testcase_41 AC 306 ms
36,616 KB
testcase_42 AC 447 ms
36,292 KB
testcase_43 AC 275 ms
44,852 KB
testcase_44 AC 333 ms
35,800 KB
testcase_45 AC 302 ms
36,436 KB
testcase_46 AC 332 ms
36,528 KB
testcase_47 AC 425 ms
36,328 KB
testcase_48 AC 458 ms
40,844 KB
testcase_49 AC 367 ms
36,676 KB
testcase_50 AC 424 ms
36,056 KB
testcase_51 AC 371 ms
48,056 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)); }


void bAdd(T)(T[] bit, int pos, T val)
in {
  assert(0 <= pos && pos < bit.length, "bAdd: 0 <= pos < |bit| must hold");
}
do {
  for (int x = pos; x < bit.length; x |= x + 1) {
    bit[x] += val;
  }
}

// sum of [0, pos)
T bSum(T)(T[] bit, int pos)
in {
  assert(0 <= pos && pos <= bit.length, "bSum: 0 <= pos <= |bit| must hold");
}
do {
  T ret = 0;
  for (int x = pos - 1; x >= 0; x = (x & (x + 1)) - 1) {
    ret += bit[x];
  }
  return ret;
}


enum K = 5;

void main() {
  try {
    for (; ; ) {
      const N = readInt();
      auto L = new int[K];
      auto R = new int[K];
      auto A = new long[][](K, N - 1);
      foreach (k; 0 .. K) {
        L[k] = readInt() - 1;
        R[k] = readInt() - 1;
        foreach (i; L[k] .. R[k]) {
          A[k][i] = readLong();
        }
      }
      const M = readInt();
      auto B = new int[M];
      auto S = new long[M];
      foreach (m; 0 .. M) {
        B[m] = readInt() - 1;
        S[m] = readLong();
      }
      
      auto ASum = new long[][](K, N);
      foreach (k; 0 .. K) {
        foreach (i; 0 .. N - 1) {
          ASum[k][i + 1] = ASum[k][i] + A[k][i];
        }
      }
      
      const xs = (L ~ R).sort.uniq.array;
      const xsLen = cast(int)(xs.length);
      long ans;
      foreach (j; 0 .. xsLen) {
        const x = xs[j];
        int[long] cnt;
        foreach (m; 0 .. M) {
          const k = B[m];
          if (L[k] <= x && x <= R[k]) {
            const s = S[m] + ASum[k][x];
            if (s in cnt) {
              ans += cnt[s];
            }
            ++cnt[s];
          }
        }
      }
      foreach (j; 0 .. xsLen - 1) {
        const l = xs[j], r = xs[j + 1];
        alias Entry = Tuple!(long, "s", long, "t");
        Entry[] es;
        foreach (m; 0 .. M) {
          const k = B[m];
          if (L[k] <= l && r <= R[k]) {
            es ~= Entry(S[m] + ASum[k][l], S[m] + ASum[k][r]);
          }
        }
        es.sort;
        long[] ts;
        foreach (ref e; es) {
          ts ~= e.t;
        }
        ts = ts.sort.uniq.array;
        auto bit = new int[ts.length];
        int tot;
        foreach (ref e; es) {
          const pos = ts.lowerBound(e.t);
          ans += tot - bit.bSum(pos + 1);
          bit.bAdd(pos, 1);
          ++tot;
        }
      }
      
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0