結果

問題 No.469 区間加算と一致検索の問題
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-03-25 07:13:01
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 2,868 ms / 5,000 ms
コード長 2,970 bytes
コンパイル時間 752 ms
コンパイル使用メモリ 107,008 KB
実行使用メモリ 203,088 KB
最終ジャッジ日時 2023-09-03 23:55:36
合計ジャッジ時間 53,697 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 177 ms
18,396 KB
testcase_03 AC 15 ms
8,940 KB
testcase_04 AC 17 ms
10,800 KB
testcase_05 AC 20 ms
11,648 KB
testcase_06 AC 24 ms
12,612 KB
testcase_07 AC 25 ms
12,492 KB
testcase_08 AC 28 ms
13,620 KB
testcase_09 AC 12 ms
6,976 KB
testcase_10 AC 25 ms
11,564 KB
testcase_11 AC 8 ms
4,376 KB
testcase_12 AC 29 ms
12,240 KB
testcase_13 AC 18 ms
10,756 KB
testcase_14 AC 7 ms
4,380 KB
testcase_15 AC 11 ms
4,376 KB
testcase_16 AC 27 ms
12,452 KB
testcase_17 AC 23 ms
11,488 KB
testcase_18 AC 24 ms
12,500 KB
testcase_19 AC 6 ms
4,376 KB
testcase_20 AC 28 ms
12,280 KB
testcase_21 AC 8 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 196 ms
29,336 KB
testcase_24 AC 196 ms
28,436 KB
testcase_25 AC 196 ms
29,288 KB
testcase_26 AC 194 ms
28,248 KB
testcase_27 AC 195 ms
29,192 KB
testcase_28 AC 193 ms
28,744 KB
testcase_29 AC 195 ms
28,420 KB
testcase_30 AC 194 ms
28,896 KB
testcase_31 AC 198 ms
29,232 KB
testcase_32 AC 197 ms
29,216 KB
testcase_33 AC 2,795 ms
200,816 KB
testcase_34 AC 2,794 ms
200,856 KB
testcase_35 AC 2,776 ms
199,992 KB
testcase_36 AC 2,787 ms
199,852 KB
testcase_37 AC 2,787 ms
203,088 KB
testcase_38 AC 2,757 ms
200,084 KB
testcase_39 AC 2,766 ms
199,988 KB
testcase_40 AC 2,759 ms
200,568 KB
testcase_41 AC 2,759 ms
201,548 KB
testcase_42 AC 2,760 ms
199,632 KB
testcase_43 AC 2,770 ms
199,272 KB
testcase_44 AC 2,775 ms
199,312 KB
testcase_45 AC 2,763 ms
199,184 KB
testcase_46 AC 2,770 ms
200,800 KB
testcase_47 AC 2,762 ms
200,172 KB
testcase_48 AC 1 ms
4,376 KB
testcase_49 AC 1 ms
4,380 KB
testcase_50 AC 2,767 ms
201,868 KB
testcase_51 AC 2,868 ms
199,568 KB
権限があれば一括ダウンロードができます

ソースコード

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];

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