結果

問題 No.469 区間加算と一致検索の問題
ユーザー te-shte-sh
提出日時 2017-12-12 12:46:43
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 542 ms / 5,000 ms
コード長 1,355 bytes
コンパイル時間 3,326 ms
コンパイル使用メモリ 173,268 KB
実行使用メモリ 44,900 KB
最終ジャッジ日時 2023-09-03 17:29:45
合計ジャッジ時間 31,017 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 420 ms
36,468 KB
testcase_01 AC 372 ms
36,728 KB
testcase_02 AC 398 ms
40,980 KB
testcase_03 AC 374 ms
38,736 KB
testcase_04 AC 372 ms
38,500 KB
testcase_05 AC 373 ms
38,324 KB
testcase_06 AC 372 ms
38,760 KB
testcase_07 AC 378 ms
37,928 KB
testcase_08 AC 385 ms
37,936 KB
testcase_09 AC 375 ms
39,252 KB
testcase_10 AC 374 ms
38,476 KB
testcase_11 AC 375 ms
38,740 KB
testcase_12 AC 371 ms
38,768 KB
testcase_13 AC 375 ms
38,376 KB
testcase_14 AC 371 ms
37,968 KB
testcase_15 AC 379 ms
38,516 KB
testcase_16 AC 381 ms
41,624 KB
testcase_17 AC 385 ms
38,892 KB
testcase_18 AC 388 ms
38,900 KB
testcase_19 AC 372 ms
38,960 KB
testcase_20 AC 377 ms
39,096 KB
testcase_21 AC 380 ms
39,020 KB
testcase_22 AC 377 ms
38,468 KB
testcase_23 AC 410 ms
42,716 KB
testcase_24 AC 395 ms
44,900 KB
testcase_25 AC 409 ms
44,776 KB
testcase_26 AC 408 ms
41,120 KB
testcase_27 AC 410 ms
42,356 KB
testcase_28 AC 408 ms
41,152 KB
testcase_29 AC 406 ms
42,024 KB
testcase_30 AC 419 ms
42,940 KB
testcase_31 AC 405 ms
41,640 KB
testcase_32 AC 403 ms
41,604 KB
testcase_33 AC 524 ms
41,820 KB
testcase_34 AC 534 ms
42,620 KB
testcase_35 AC 541 ms
42,020 KB
testcase_36 AC 533 ms
41,852 KB
testcase_37 AC 542 ms
40,580 KB
testcase_38 AC 517 ms
42,060 KB
testcase_39 AC 538 ms
43,256 KB
testcase_40 AC 517 ms
42,624 KB
testcase_41 AC 527 ms
40,548 KB
testcase_42 AC 523 ms
40,740 KB
testcase_43 AC 520 ms
43,520 KB
testcase_44 AC 518 ms
41,824 KB
testcase_45 AC 518 ms
40,572 KB
testcase_46 AC 517 ms
43,296 KB
testcase_47 AC 526 ms
42,548 KB
testcase_48 AC 369 ms
35,588 KB
testcase_49 AC 385 ms
36,392 KB
testcase_50 AC 509 ms
43,024 KB
testcase_51 AC 518 ms
43,060 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.algorithm, std.conv, std.range, std.stdio, std.string;
import std.random;

void main()
{
  auto rd = readln.split.to!(int[]), n = rd[0], q = rd[1];

  auto p = primes!int(5*10^^7)[10000..$];
  auto b = p.randomSample(n).array;

  auto bc = new long[](n+1);
  foreach (i; 0..n) bc[i+1] = bc[i] + b[i];

  int[long] h;
  h[0] = 0;

  auto curr = 0L;
  foreach (i; 0..q) {
    auto line = readln.chomp;
    if (line[0] == '?') {
      writeln(h[curr]);
    } else {
      auto rd2 = line[2..$].splitter;
      auto l = rd2.front.to!int; rd2.popFront();
      auto r = rd2.front.to!int; rd2.popFront();
      auto k = rd2.front.to!int;
      curr += (bc[r] - bc[l]) * k;
      if (curr !in h)
        h[curr] = i+1;
    }
  }
}

pure T[] primes(T)(T n)
{
  import std.algorithm, std.bitmanip, std.conv, std.range;

  auto sieve = BitArray();
  sieve.length((n + 1) / 2);
  sieve = ~sieve;

  foreach (p; 1..((nsqrt(n) - 1) / 2 + 1))
    if (sieve[p])
      for (auto q = p * 3 + 1; q < (n + 1) / 2; q += p * 2 + 1)
        sieve[q] = false;

  auto r = sieve.bitsSet.map!(to!T).map!("a * 2 + 1").array;
  r[0] = 2;

  return r;
}

pure T nsqrt(T)(T n)
{
  import std.algorithm, std.conv, std.range, core.bitop;
  if (n <= 1) return n;
  T m = 1 << (n.bsr / 2 + 1);
  return iota(1, m).map!"a * a".assumeSorted!"a <= b".lowerBound(n).length.to!T;
}
0