結果

問題 No.619 CardShuffle
ユーザー 👑 hos.lyrichos.lyric
提出日時 2019-01-21 08:13:32
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 207 ms / 3,000 ms
コード長 3,840 bytes
コンパイル時間 2,828 ms
コンパイル使用メモリ 109,664 KB
実行使用メモリ 24,548 KB
最終ジャッジ日時 2023-09-03 22:47:02
合計ジャッジ時間 6,214 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 205 ms
24,040 KB
testcase_17 AC 191 ms
24,056 KB
testcase_18 AC 198 ms
24,052 KB
testcase_19 AC 192 ms
24,056 KB
testcase_20 AC 198 ms
24,204 KB
testcase_21 AC 207 ms
23,916 KB
testcase_22 AC 199 ms
24,092 KB
testcase_23 AC 207 ms
24,368 KB
testcase_24 AC 198 ms
24,256 KB
testcase_25 AC 200 ms
23,992 KB
testcase_26 AC 193 ms
24,240 KB
testcase_27 AC 193 ms
24,256 KB
testcase_28 AC 194 ms
24,548 KB
testcase_29 AC 195 ms
24,064 KB
testcase_30 AC 192 ms
23,904 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 206 ms
24,100 KB
testcase_33 AC 201 ms
24,144 KB
testcase_34 AC 185 ms
24,288 KB
testcase_35 AC 106 ms
13,436 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 MO = 10L^^9 + 7;

/*
  true: _*a_
  false: _*a + b + c_
*/
struct P {
  bool f;
  long a, b, c;
  this(bool f, long a, long b, long c) {
    this.f = f;
    this.a = a;
    this.b = b;
    this.c = c;
  }
  P opBinary(string op)(P o) if (op == "*") {
    return f ? (o.f ? P(true, (a * o.a) % MO, 0, 0)
                    : P(false, (a * o.a) % MO, o.b, o.c))
             : (o.f ? P(false, a, b, c * o.a)
                    : P(false, a, (b + c * o.a + o.b) % MO, o.c));
  }
}
P makeP(in string[] s) {
  switch (s[0]) {
    case "+": return P(false, 1, 0, s[1].to!long);
    case "*": return P(true, s[1].to!long, 0, 0);
    default: assert(false);
  }
}

class SegmentTree {
  int n;
  P[] prod;
  this(P[] ini) {
    for (n = 1; n < ini.length; n <<= 1) {}
    prod = new P[n << 1];
    foreach (i, val; ini) {
      prod[n + i] = val;
    }
    foreach_reverse (a; 1 .. n) {
      prod[a] = prod[a << 1] * prod[a << 1 | 1];
    }
  }
  void update(int a, P val) {
    prod[a += n] = val;
    for (a >>= 1; a; a >>= 1) {
      prod[a] = prod[a << 1] * prod[a << 1 | 1];
    }
  }
  P rangeProd(int a, int b) {
    P retL = P(true, 1, 0, 0);
    P retR = P(true, 1, 0, 0);
    for (a += n, b += n; a <= b; a >>= 1, b >>= 1) {
      if ( a & 1) retL = retL * prod[a++];
      if (~b & 1) retR = prod[b--] * retR;
    }
    return retL * retR;
  }
}

int N;
string[] C;
int Q;
string[] T;
int[] X, Y;

void main() {
  try {
    for (; ; ) {
      N = readInt() + 1;
      C = new string[N];
      C[0] = "+";
      foreach (i; 1 .. N) {
        C[i] = readToken();
      }
      Q = readInt();
      T = new string[Q];
      X = new int[Q];
      Y = new int[Q];
      foreach (q; 0 .. Q) {
        T[q] = readToken();
        X[q] = readInt();
        Y[q] = readInt();
      }
      auto seg = new SegmentTree(iota(N / 2).map!(i => makeP(C[i * 2 .. i * 2 + 2])).array);
      debug {
        foreach (i; 0 .. N / 2) foreach (j; i .. N / 2) {
          writefln("%s %s: %s", i, j, seg.rangeProd(i, j));
        }
      }
      foreach (q; 0 .. Q) {
        switch (T[q]) {
          case "!": {
            swap(C[X[q]], C[Y[q]]);
            seg.update(X[q] / 2, makeP(C[X[q] / 2 * 2 .. X[q] / 2 * 2 + 2]));
            seg.update(Y[q] / 2, makeP(C[Y[q] / 2 * 2 .. Y[q] / 2 * 2 + 2]));
          } break;
          case "?": {
            long ans;
            const x = C[X[q]].to!long;
            if (X[q] == Y[q]) {
              ans = x;
            } else {
              const res = seg.rangeProd(X[q] / 2 + 1, Y[q] / 2);
              ans = res.f ? (x * res.a) : (x * res.a + res.b + res.c);
            }
            ans %= MO;
            writeln(ans);
          } break;
          default: assert(false);
        }
      }
    }
  } catch (EOFException e) {
  }
}
0