結果

問題 No.469 区間加算と一致検索の問題
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-22 17:09:06
言語 D
(dmd 2.106.1)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,982 bytes
コンパイル時間 2,779 ms
コンパイル使用メモリ 121,048 KB
実行使用メモリ 48,816 KB
最終ジャッジ日時 2024-04-09 03:59:00
合計ジャッジ時間 15,664 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,948 KB
testcase_02 AC 187 ms
7,236 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 AC 2 ms
6,944 KB
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 RE -
testcase_39 RE -
testcase_40 RE -
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
testcase_44 RE -
testcase_45 RE -
testcase_46 RE -
testcase_47 RE -
testcase_48 AC 1 ms
6,944 KB
testcase_49 AC 1 ms
6,948 KB
testcase_50 RE -
testcase_51 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.container, std.math, std.numeric, std.range, 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; }

void chmin(T)(ref T t, in T f) { if (t > f) t = f; }
void chmax(T)(ref T t, in T f) { if (t < f) t = f; }

int binarySearch(T)(in T[] as, in bool delegate(T) test) { int low = -1, upp = cast(int)(as.length); for (; low + 1 < upp; ) { int mid = (low + upp) >> 1; (test(as[mid]) ? low : upp) = mid; } return upp; }
int lowerBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a < val)); }
int upperBound(T)(in T[] as, in T val) { return as.binarySearch((T a) => (a <= val)); }


enum long[] P = [1000000007, 1000000009, 1000000021, 1000000033, 1000000087, 1000000093, 1000000097, 1000000103, 1000000123, 1000000181];

int N, Q;
string[] T;
int[] L, R;
long[] K;

void main() {
  auto BASE = new long[P.length];
  BASE[0] = 314159265L;
  foreach (h; 1 .. P.length) {
    BASE[h] = (BASE[h - 1] * 123456789L) % 999999937L;
  }
  
  try {
    for (; ; ) {
      N = readInt();
      Q = readInt();
      T = new string[Q];
      L = new int[Q];
      R = new int[Q];
      K = new long[Q];
      foreach (q; 0 .. Q) {
        T[q] = readToken();
        if (T[q] == "!") {
          L[q] = readInt();
          R[q] = readInt();
          K[q] = readInt();
        }
      }
      
      auto a = new long[][](P.length, N);
      auto b = new long[][](P.length, N + 1);
      foreach (h; 0 .. P.length) {
        a[h][0] = 1;
        foreach (i; 1 .. N) {
          a[h][i] = (a[h][i - 1] * BASE[h]) % P[h];
        }
        foreach (i; 0 .. N) {
          b[h][i + 1] = (b[h][i] + a[h][i]) % P[h];
        }
      }
      
      auto hs = new long[][](Q + 1, P.length);
      int[][long] qs;
      qs[0] ~= 0;
      foreach (q; 0 .. Q) {
        switch (T[q]) {
          case "!": {
            foreach (h; 0 .. P.length) {
              hs[q + 1][h] = (hs[q][h] + (b[h][R[q]] - b[h][L[q]]) * K[q]) % P[h];
              if (hs[q + 1][h] < 0) {
                hs[q + 1][h] += P[h];
              }
            }
            qs[hs[q + 1][0]] ~= q + 1;
          } break;
          case "?": {
            int ans = -1;
            foreach (qq; qs[hs[q][0]]) {
              if (hs[qq] == hs[q]) {
                ans = qq;
                break;
              }
            }
            writeln(ans);
            hs[q + 1][] = hs[q][];
          } break;
          default: assert(false);
        }
        debug {
          writeln(hs[q + 1]);
        }
      }
    }
  } catch (EOFException e) {
  }
}
0